N = int(eval(input()))

t = list(range(N+1))

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

    x = t[i-1] + 1

    p6, p9 = 6, 9

    while i-p6 >= 0:

        x = min(x, t[i-p6] + 1)

        p6 *= 6

    while i-p9 >= 0:

        x = min(x, t[i-p9] + 1)

        p9 *= 9

    t[i] = x

print((t[N]))

N=int(eval(input()));t=[0]+[N]*N

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

 x,a,b=t[i-1],6,9

 while a<=i:x=min(x,t[i-a],t[i-b]if b<=i else N);a*=6;b*=9

 t[i]=x+1

print((t[N]))
N = int(eval(input()))
t = list(range(N + 1))
for i in range(2, N + 1):
    x = t[i - 1] + 1
    p6, p9 = 6, 9
    while i - p6 >= 0:
        x = min(x, t[i - p6] + 1)
        p6 *= 6
    while i - p9 >= 0:
        x = min(x, t[i - p9] + 1)
        p9 *= 9
    t[i] = x
print((t[N]))

N = int(eval(input()))
t = [0] + [N] * N
for i in range(1, N + 1):
    x, a, b = t[i - 1], 6, 9
    while a <= i:
        x = min(x, t[i - a], t[i - b] if b <= i else N)
        a *= 6
        b *= 9
    t[i] = x + 1
print((t[N]))

-t = list(range(N + 1)) -for i in range(2, N + 1): -    x = t[i - 1] + 1 -    p6, p9 = 6, 9 -    while i - p6 >= 0: -        x = min(x, t[i - p6] + 1) -        p6 *= 6 -    while i - p9 >= 0: -        x = min(x, t[i - p9] + 1) -        p9 *= 9 -    t[i] = x +t = [0] + [N] * N +for i in range(1, N + 1): +    x, a, b = t[i - 1], 6, 9 +    while a <= i: +        x = min(x, t[i - a], t[i - b] if b <= i else N) +        a *= 6 +        b *= 9 +    t[i] = x + 1