N=int(eval(input()))

L=[1]

for i in range(1,100):

    a=6**i

    if a<=100000:

        L.append(a)

    else:

        break

for i in range(1,100):

    a=9**i

    if a<=100000:

        L.append(a)

    else:

        break

dp=[1000000]*(N+1)

dp[0]=0

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

    sub=10**5

    for l in L:

        if i-l>=0:

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

    dp[i]=sub



print((dp[N]))
N=int(eval(input()))

L=[1]

now=6

while now<=10**6:

    L.append(now)

    now*=6

now=9

while now<=10**6:

    L.append(now)

    now*=9

#print(L)

dp=[float("INF")]*(N+1)

dp[0]=0

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

    for x in L:

        if i-x>=0:

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

print((dp[N]))

#print(dp)
N = int(eval(input()))
L = [1]
for i in range(1, 100):
    a = 6**i
    if a <= 100000:
        L.append(a)
    else:
        break
for i in range(1, 100):
    a = 9**i
    if a <= 100000:
        L.append(a)
    else:
        break
dp = [1000000] * (N + 1)
dp[0] = 0
for i in range(1, N + 1):
    sub = 10**5
    for l in L:
        if i - l >= 0:
            sub = min(sub, dp[i - l] + 1)
    dp[i] = sub
print((dp[N]))

N = int(eval(input()))
L = [1]
now = 6
while now <= 10**6:
    L.append(now)
    now *= 6
now = 9
while now <= 10**6:
    L.append(now)
    now *= 9
# print(L)
dp = [float("INF")] * (N + 1)
dp[0] = 0
for i in range(1, N + 1):
    for x in L:
        if i - x >= 0:
            dp[i] = min(dp[i], dp[i - x] + 1)
print((dp[N]))
# print(dp)

-for i in range(1, 100): -    a = 6**i -    if a <= 100000: -        L.append(a) -    else: -        break -for i in range(1, 100): -    a = 9**i -    if a <= 100000: -        L.append(a) -    else: -        break -dp = [1000000] * (N + 1) +now = 6 +while now <= 10**6: +    L.append(now) +    now *= 6 +now = 9 +while now <= 10**6: +    L.append(now) +    now *= 9 +# print(L) +dp = [float("INF")] * (N + 1) -    sub = 10**5 -    for l in L: -        if i - l >= 0: -            sub = min(sub, dp[i - l] + 1) -    dp[i] = sub +    for x in L: +        if i - x >= 0: +            dp[i] = min(dp[i], dp[i - x] + 1) +# print(dp)