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()))

dp=[0]*n

l69=sorted([1]+[6**i for i in range(1,7)]+[9**i for i in range(1,6)])

g=0

for i in range(n):

    for j in range(len(l69)):

        if i+1-l69[j]>=0:

            g=j

    dp[i]=min(dp[i-p]+1 for p in l69[:g+1])



print((dp[n-1]))
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()))
dp = [0] * n
l69 = sorted([1] + [6**i for i in range(1, 7)] + [9**i for i in range(1, 6)])
g = 0
for i in range(n):
    for j in range(len(l69)):
        if i + 1 - l69[j] >= 0:
            g = j
    dp[i] = min(dp[i - p] + 1 for p in l69[: g + 1])
print((dp[n - 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]) -print((dp[n])) +dp = [0] * n +l69 = sorted([1] + [6**i for i in range(1, 7)] + [9**i for i in range(1, 6)]) +g = 0 +for i in range(n): +    for j in range(len(l69)): +        if i + 1 - l69[j] >= 0: +            g = j +    dp[i] = min(dp[i - p] + 1 for p in l69[: g + 1]) +print((dp[n - 1]))