n=int(eval(input()))

ll=[6,9,36,81,216,729,1296,6561,7776,46656,59049]

dp=[i for i in range(100010)]



for l in ll:

    for x in range(l,n+1):

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

print((dp[n]))

    

    





n=int(eval(input()))

l9=list(9**i for i in range(1,6))

l6=list(6**j for j in range(1,8))

l=sorted(l9+l6)

dp=[_ for _ in range(100010)]

for k in l:

    for m in range(k,n+1):

        dp[m]=min(dp[m-k]+1,dp[m])

print((dp[n]))
n = int(eval(input()))
ll = [6, 9, 36, 81, 216, 729, 1296, 6561, 7776, 46656, 59049]
dp = [i for i in range(100010)]
for l in ll:
    for x in range(l, n + 1):
        dp[x] = min(dp[x], dp[x - l] + 1)
print((dp[n]))

n = int(eval(input()))
l9 = list(9**i for i in range(1, 6))
l6 = list(6**j for j in range(1, 8))
l = sorted(l9 + l6)
dp = [_ for _ in range(100010)]
for k in l:
    for m in range(k, n + 1):
        dp[m] = min(dp[m - k] + 1, dp[m])
print((dp[n]))

-ll = [6, 9, 36, 81, 216, 729, 1296, 6561, 7776, 46656, 59049] -dp = [i for i in range(100010)] -for l in ll: -    for x in range(l, n + 1): -        dp[x] = min(dp[x], dp[x - l] + 1) +l9 = list(9**i for i in range(1, 6)) +l6 = list(6**j for j in range(1, 8)) +l = sorted(l9 + l6) +dp = [_ for _ in range(100010)] +for k in l: +    for m in range(k, n + 1): +        dp[m] = min(dp[m - k] + 1, dp[m])