import numpy as np

N=int(eval(input()))

six=[1]

nine=[]

for i in range(1,N):

  six.append(6**i)

  nine.append(9**i)

  if 6**i>N:

    break

six=np.array(six)

nine=np.array(nine)

L=np.concatenate([six,nine])    

now=N

count=0

dp=np.ones(N+L.max()+1)*10**5

dp[1]=1

dp[0]=0

#dp[i+1]　iをちょうど引き出すのに必要な最小回数

for i in range(1,N):

  dp[i+1]=(dp[i-L+1]+1).min()



  

print((int(dp[N])))

#print(len(L))



from math import log

N=int(eval(input()))



L=[1]

for i in range(1,N):

    if 6**i<=N:

        L.append(6**i)

    else:

        break

for i in range(1,N):

    if 9**i<=N:

        L.append(9**i)

    else:

        break



dp=[100000]*(N+1)

dp[0]=0

dp[1]=1

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

    sub=100000

    for l in L:

        if i-l>=0:

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

    dp[i]=sub



print((dp[N]))
import numpy as np

N = int(eval(input()))
six = [1]
nine = []
for i in range(1, N):
    six.append(6**i)
    nine.append(9**i)
    if 6**i > N:
        break
six = np.array(six)
nine = np.array(nine)
L = np.concatenate([six, nine])
now = N
count = 0
dp = np.ones(N + L.max() + 1) * 10**5
dp[1] = 1
dp[0] = 0
# dp[i+1]　iをちょうど引き出すのに必要な最小回数
for i in range(1, N):
    dp[i + 1] = (dp[i - L + 1] + 1).min()
print((int(dp[N])))
# print(len(L))

from math import log

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

-import numpy as np +from math import log -six = [1] -nine = [] +L = [1] -    six.append(6**i) -    nine.append(9**i) -    if 6**i > N: +    if 6**i <= N: +        L.append(6**i) +    else: -six = np.array(six) -nine = np.array(nine) -L = np.concatenate([six, nine]) -now = N -count = 0 -dp = np.ones(N + L.max() + 1) * 10**5 +for i in range(1, N): +    if 9**i <= N: +        L.append(9**i) +    else: +        break +dp = [100000] * (N + 1) +dp[0] = 0 -dp[0] = 0 -# dp[i+1]　iをちょうど引き出すのに必要な最小回数 -for i in range(1, N): -    dp[i + 1] = (dp[i - L + 1] + 1).min() -print((int(dp[N]))) -# print(len(L)) +for i in range(2, N + 1): +    sub = 100000 +    for l in L: +        if i - l >= 0: +            sub = min(dp[i - l] + 1, sub) +    dp[i] = sub +print((dp[N]))