import math

N = int(eval(input()))

dp=[100000]*(100000+1)

dp[0]=0

dp[1:5]=list(range(1,6))

dp[6:9]=list(range(1,4))

for n in range(9,N+1):

  k9=0

  k6=0

  while n//(9**k9)>=9:

    k9+=1

  while n//(6**k6)>=6:

    k6+=1

  dp[n]=min(dp[n],dp[n-6**k6]+1,dp[n-9**k9]+1,dp[n-1]+1)

print((dp[N]))
import math

N = int(eval(input()))

dp=[100000]*(100000+1)

dp[0]=0

dp[1:5]=list(range(1,6))

dp[6:9]=list(range(1,4))

k9=0

k6=0

for n in range(9,N+1):

  while n//(9**k9)>=9:

    k9+=1

  while n//(6**k6)>=6:

    k6+=1

  dp[n]=min(dp[n],dp[n-6**k6]+1,dp[n-9**k9]+1,dp[n-1]+1)

print((dp[N]))

import math

N = int(eval(input()))
dp = [100000] * (100000 + 1)
dp[0] = 0
dp[1:5] = list(range(1, 6))
dp[6:9] = list(range(1, 4))
for n in range(9, N + 1):
    k9 = 0
    k6 = 0
    while n // (9**k9) >= 9:
        k9 += 1
    while n // (6**k6) >= 6:
        k6 += 1
    dp[n] = min(dp[n], dp[n - 6**k6] + 1, dp[n - 9**k9] + 1, dp[n - 1] + 1)
print((dp[N]))

import math

N = int(eval(input()))
dp = [100000] * (100000 + 1)
dp[0] = 0
dp[1:5] = list(range(1, 6))
dp[6:9] = list(range(1, 4))
k9 = 0
k6 = 0
for n in range(9, N + 1):
    while n // (9**k9) >= 9:
        k9 += 1
    while n // (6**k6) >= 6:
        k6 += 1
    dp[n] = min(dp[n], dp[n - 6**k6] + 1, dp[n - 9**k9] + 1, dp[n - 1] + 1)
print((dp[N]))

+k9 = 0 +k6 = 0 -    k9 = 0 -    k6 = 0