# 配るDP

N = int(eval(input()))



INF = 10 ** 5 + 1



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

dp[0] = 0



for n in range(N):

    power = 1

    while n + power <= N:

        dp[n + power] = min(dp[n + power], dp[n] + 1)

        power *= 6



    power = 1

    while n + power <= N:

        dp[n + power] = min(dp[n + power], dp[n] + 1)

        power *= 9



print((dp[N]))

# 貰うDP

N = int(eval(input()))



INF = 10 ** 5 + 1



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

dp[0] = 0



for n in range(N + 1):

    power = 1

    while power <= n:

        dp[n] = min(dp[n], dp[n - power] + 1)

        power *= 6



    power = 1

    while power <= n:

        dp[n] = min(dp[n], dp[n - power] + 1)

        power *= 9



print((dp[N]))

# 配るDP
N = int(eval(input()))
INF = 10**5 + 1
dp = [INF for _ in range(N + 1)]
dp[0] = 0
for n in range(N):
    power = 1
    while n + power <= N:
        dp[n + power] = min(dp[n + power], dp[n] + 1)
        power *= 6
    power = 1
    while n + power <= N:
        dp[n + power] = min(dp[n + power], dp[n] + 1)
        power *= 9
print((dp[N]))

# 貰うDP
N = int(eval(input()))
INF = 10**5 + 1
dp = [INF for _ in range(N + 1)]
dp[0] = 0
for n in range(N + 1):
    power = 1
    while power <= n:
        dp[n] = min(dp[n], dp[n - power] + 1)
        power *= 6
    power = 1
    while power <= n:
        dp[n] = min(dp[n], dp[n - power] + 1)
        power *= 9
print((dp[N]))

-# 配るDP +# 貰うDP -for n in range(N): +for n in range(N + 1): -    while n + power <= N: -        dp[n + power] = min(dp[n + power], dp[n] + 1) +    while power <= n: +        dp[n] = min(dp[n], dp[n - power] + 1) -    while n + power <= N: -        dp[n + power] = min(dp[n + power], dp[n] + 1) +    while power <= n: +        dp[n] = min(dp[n], dp[n - power] + 1)