n=int(eval(input()))



dp=[0]*(n+1)

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

    ans=dp[i-1]+1

    j=1

    while i>=6**j:

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

        j+=1

    j=1

    while i>=9**j:

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

        j+=1

    dp[i]=ans

  

print(ans)

n=int(eval(input()))



l=[1]

l_9=[pow(9,i) for i in range(1,7)]

l_6=[pow(6,i) for i in range(1,8)]

l+=l_9

l+=l_6

l.sort(reverse=True)

dp=[10**9 for i in range(n+1)]



dp[0]=0



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

    for j in l:

        if i-j>=0:

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

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

n = int(eval(input()))
l = [1]
l_9 = [pow(9, i) for i in range(1, 7)]
l_6 = [pow(6, i) for i in range(1, 8)]
l += l_9
l += l_6
l.sort(reverse=True)
dp = [10**9 for i in range(n + 1)]
dp[0] = 0
for i in range(1, n + 1):
    for j in l:
        if i - j >= 0:
            dp[i] = min(dp[i], dp[i - j] + 1)
print((dp[n]))

-dp = [0] * (n + 1) +l = [1] +l_9 = [pow(9, i) for i in range(1, 7)] +l_6 = [pow(6, i) for i in range(1, 8)] +l += l_9 +l += l_6 +l.sort(reverse=True) +dp = [10**9 for i in range(n + 1)] +dp[0] = 0 -    ans = dp[i - 1] + 1 -    j = 1 -    while i >= 6**j: -        ans = min(ans, dp[i - 6**j] + 1) -        j += 1 -    j = 1 -    while i >= 9**j: -        ans = min(ans, dp[i - 9**j] + 1) -        j += 1 -    dp[i] = ans -print(ans) +    for j in l: +        if i - j >= 0: +            dp[i] = min(dp[i], dp[i - j] + 1) +print((dp[n]))