SixNum = [i for i in range(0,100001)]

for i in range(1,7):

        for j in range(1,100001):

                SixNum[j] = min(SixNum[j],j//(6**i) + SixNum[j%(6**i)])



NineNum = [i for i in range(0,100001)]

for i in range(1,6):

        for j in range(1,100001):

                NineNum[j] = min(NineNum[j],j//(9**i) + NineNum[j%(9**i)])



N = int(eval(input()))

ans = 100001

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

        ans = min(ans,SixNum[i]+NineNum[N-i])

print(ans)
n=int(eval(input()))

dp=[10**10]*(n+1)

dp[0]=0

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

  dp[i]=dp[i-1]+1

  x=0

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

    x+=1

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

  x=0

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

    x+=1

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

print((dp[n]))

  

    

    

SixNum = [i for i in range(0, 100001)]
for i in range(1, 7):
    for j in range(1, 100001):
        SixNum[j] = min(SixNum[j], j // (6**i) + SixNum[j % (6**i)])
NineNum = [i for i in range(0, 100001)]
for i in range(1, 6):
    for j in range(1, 100001):
        NineNum[j] = min(NineNum[j], j // (9**i) + NineNum[j % (9**i)])
N = int(eval(input()))
ans = 100001
for i in range(0, N + 1):
    ans = min(ans, SixNum[i] + NineNum[N - i])
print(ans)

n = int(eval(input()))
dp = [10**10] * (n + 1)
dp[0] = 0
for i in range(1, n + 1):
    dp[i] = dp[i - 1] + 1
    x = 0
    while 9 ** (x + 1) <= i:
        x += 1
    dp[i] = min(dp[i], dp[i - 9**x] + 1)
    x = 0
    while 6 ** (x + 1) <= i:
        x += 1
    dp[i] = min(dp[i], dp[i - 6**x] + 1)
print((dp[n]))

-SixNum = [i for i in range(0, 100001)] -for i in range(1, 7): -    for j in range(1, 100001): -        SixNum[j] = min(SixNum[j], j // (6**i) + SixNum[j % (6**i)]) -NineNum = [i for i in range(0, 100001)] -for i in range(1, 6): -    for j in range(1, 100001): -        NineNum[j] = min(NineNum[j], j // (9**i) + NineNum[j % (9**i)]) -N = int(eval(input())) -ans = 100001 -for i in range(0, N + 1): -    ans = min(ans, SixNum[i] + NineNum[N - i]) -print(ans) +n = int(eval(input())) +dp = [10**10] * (n + 1) +dp[0] = 0 +for i in range(1, n + 1): +    dp[i] = dp[i - 1] + 1 +    x = 0 +    while 9 ** (x + 1) <= i: +        x += 1 +    dp[i] = min(dp[i], dp[i - 9**x] + 1) +    x = 0 +    while 6 ** (x + 1) <= i: +        x += 1 +    dp[i] = min(dp[i], dp[i - 6**x] + 1) +print((dp[n]))