import sys

def f(n):
    if n <= 1:
        return 0
    if n == 2:
        return 1
    return n//3 + f(n//3 + n%3)
    
for line in sys.stdin:
    num = int(line)
    # 输入的 0 表示输入结束
    if num == 0:
        break
    print(f(num))