N = int(eval(input()))

INF = int(1e10)

dp = [INF] * N

dp[0] = 1

dp = [0] + dp



bt = set()

for i in range(1, 100):

    if 6**i > N:

        break

    bt.add(6**i)



for i in range(1, 100):

    if 9**i > N:

        break

    bt.add(9**i)



bt = sorted(bt)



for i in range(1, N+1):

    dp[i] = dp[i-1] + 1

    for j in bt:

        if i - j >= 0:

            dp[i] = min(dp[i], dp[i - j] + 1)



print((dp[N]))

N = int(eval(input()))

inf = float("inf")

dp = [inf for _ in range(N+1)]

l6 = 7

l9 = 6

dp[0] = 0



# DP

for i in range(1, N+1):

    # 1円を引き出す

    dp[i] = min(dp[i], dp[i-1] + 1)

    

    # 6の累乗円を引き出す

    for j in range(1, l6):

        if i - 6**j >= 0:

            dp[i] = min(dp[i], dp[i - 6**j] + 1)

        

    # 9の累乗円を引き出す

    for j in range(1, l9):

        if i - 9**j >= 0:

            dp[i] = min(dp[i], dp[i - 9**j] + 1)



print((dp[N]))
N = int(eval(input()))
INF = int(1e10)
dp = [INF] * N
dp[0] = 1
dp = [0] + dp
bt = set()
for i in range(1, 100):
    if 6**i > N:
        break
    bt.add(6**i)
for i in range(1, 100):
    if 9**i > N:
        break
    bt.add(9**i)
bt = sorted(bt)
for i in range(1, N + 1):
    dp[i] = dp[i - 1] + 1
    for j in bt:
        if i - j >= 0:
            dp[i] = min(dp[i], dp[i - j] + 1)
print((dp[N]))

N = int(eval(input()))
inf = float("inf")
dp = [inf for _ in range(N + 1)]
l6 = 7
l9 = 6
dp[0] = 0
# DP
for i in range(1, N + 1):
    # 1円を引き出す
    dp[i] = min(dp[i], dp[i - 1] + 1)
    # 6の累乗円を引き出す
    for j in range(1, l6):
        if i - 6**j >= 0:
            dp[i] = min(dp[i], dp[i - 6**j] + 1)
    # 9の累乗円を引き出す
    for j in range(1, l9):
        if i - 9**j >= 0:
            dp[i] = min(dp[i], dp[i - 9**j] + 1)
print((dp[N]))

-INF = int(1e10) -dp = [INF] * N -dp[0] = 1 -dp = [0] + dp -bt = set() -for i in range(1, 100): -    if 6**i > N: -        break -    bt.add(6**i) -for i in range(1, 100): -    if 9**i > N: -        break -    bt.add(9**i) -bt = sorted(bt) +inf = float("inf") +dp = [inf for _ in range(N + 1)] +l6 = 7 +l9 = 6 +dp[0] = 0 +# DP -    dp[i] = dp[i - 1] + 1 -    for j in bt: -        if i - j >= 0: -            dp[i] = min(dp[i], dp[i - j] + 1) +    # 1円を引き出す +    dp[i] = min(dp[i], dp[i - 1] + 1) +    # 6の累乗円を引き出す +    for j in range(1, l6): +        if i - 6**j >= 0: +            dp[i] = min(dp[i], dp[i - 6**j] + 1) +    # 9の累乗円を引き出す +    for j in range(1, l9): +        if i - 9**j >= 0: +            dp[i] = min(dp[i], dp[i - 9**j] + 1)