n=int(eval(input()))

import math

ansa=[0]

ansb=[0]

ans=[]

cnt=0

x=0

y=0

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

    while 9**(x+1)<=i:

        x+=1

    for j in range(x,-1,-1):

        z=(math.floor(i/(9**j))%9)

        i-=(9**j)*z

        cnt+=z

    ansa.append(cnt)

    cnt=0    

    

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

    while 6**(y+1)<=i:

        y+=1

    for j in range(y,-1,-1):

        z=(math.floor(i/(6**j))%6)

        i-=(6**j)*z

        cnt+=z

    ansb.append(cnt)

    cnt=0

    

for i in range(n+1):

    ans.append(ansa[i]+ansb[n-i])

    

print((min(ans)))   



n=int(eval(input()))

li6=[46656,7776,1296,216,36,6,1]

li9=[59049,6561,729,81,9,1]

dp6=[0]*(n+1)

dp9=[0]*(n+1)

dp6[1]=1

dp9[1]=1

ans=[]

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

    for j in range(7):

        if i>=li6[j]:

            k=i-li6[j]

            break

    dp6[i]=1+dp6[k] 

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

    for j in range(6):

        if i>=li9[j]:

            k=i-li9[j]

            break

    dp9[i]=1+dp9[k]  

for i in range(n+1):

    ans.append(dp6[i]+dp9[n-i])                    

print((min(ans)))



n = int(eval(input()))
import math

ansa = [0]
ansb = [0]
ans = []
cnt = 0
x = 0
y = 0
for i in range(1, n + 1):
    while 9 ** (x + 1) <= i:
        x += 1
    for j in range(x, -1, -1):
        z = math.floor(i / (9**j)) % 9
        i -= (9**j) * z
        cnt += z
    ansa.append(cnt)
    cnt = 0
for i in range(1, n + 1):
    while 6 ** (y + 1) <= i:
        y += 1
    for j in range(y, -1, -1):
        z = math.floor(i / (6**j)) % 6
        i -= (6**j) * z
        cnt += z
    ansb.append(cnt)
    cnt = 0
for i in range(n + 1):
    ans.append(ansa[i] + ansb[n - i])
print((min(ans)))

n = int(eval(input()))
li6 = [46656, 7776, 1296, 216, 36, 6, 1]
li9 = [59049, 6561, 729, 81, 9, 1]
dp6 = [0] * (n + 1)
dp9 = [0] * (n + 1)
dp6[1] = 1
dp9[1] = 1
ans = []
for i in range(2, n + 1):
    for j in range(7):
        if i >= li6[j]:
            k = i - li6[j]
            break
    dp6[i] = 1 + dp6[k]
for i in range(2, n + 1):
    for j in range(6):
        if i >= li9[j]:
            k = i - li9[j]
            break
    dp9[i] = 1 + dp9[k]
for i in range(n + 1):
    ans.append(dp6[i] + dp9[n - i])
print((min(ans)))

-import math - -ansa = [0] -ansb = [0] +li6 = [46656, 7776, 1296, 216, 36, 6, 1] +li9 = [59049, 6561, 729, 81, 9, 1] +dp6 = [0] * (n + 1) +dp9 = [0] * (n + 1) +dp6[1] = 1 +dp9[1] = 1 -cnt = 0 -x = 0 -y = 0 -for i in range(1, n + 1): -    while 9 ** (x + 1) <= i: -        x += 1 -    for j in range(x, -1, -1): -        z = math.floor(i / (9**j)) % 9 -        i -= (9**j) * z -        cnt += z -    ansa.append(cnt) -    cnt = 0 -for i in range(1, n + 1): -    while 6 ** (y + 1) <= i: -        y += 1 -    for j in range(y, -1, -1): -        z = math.floor(i / (6**j)) % 6 -        i -= (6**j) * z -        cnt += z -    ansb.append(cnt) -    cnt = 0 +for i in range(2, n + 1): +    for j in range(7): +        if i >= li6[j]: +            k = i - li6[j] +            break +    dp6[i] = 1 + dp6[k] +for i in range(2, n + 1): +    for j in range(6): +        if i >= li9[j]: +            k = i - li9[j] +            break +    dp9[i] = 1 + dp9[k] -    ans.append(ansa[i] + ansb[n - i]) +    ans.append(dp6[i] + dp9[n - i])