n = int(eval(input()))



ans = n

for i in range(n+1):

    count = 0

    t = i

    while(t):

        count += t % 6

        t = int(t/6)

    t = n-i

    while(t):

        count += t % 9

        t = int(t/9)

    ans = min(ans, count)

print(ans)

N = int(eval(input()))



dp = [float('inf')] * (N+1)

dp[0] = 0



for n in range(1, 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]))

n = int(eval(input()))
ans = n
for i in range(n + 1):
    count = 0
    t = i
    while t:
        count += t % 6
        t = int(t / 6)
    t = n - i
    while t:
        count += t % 9
        t = int(t / 9)
    ans = min(ans, count)
print(ans)

N = int(eval(input()))
dp = [float("inf")] * (N + 1)
dp[0] = 0
for n in range(1, 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]))

-n = int(eval(input())) -ans = n -for i in range(n + 1): -    count = 0 -    t = i -    while t: -        count += t % 6 -        t = int(t / 6) -    t = n - i -    while t: -        count += t % 9 -        t = int(t / 9) -    ans = min(ans, count) -print(ans) +N = int(eval(input())) +dp = [float("inf")] * (N + 1) +dp[0] = 0 +for n in range(1, 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]))