N = int(eval(input()))

A=[]

A.append(1)

for i in range(1,7):

    A.append(6**i)

for i in range(1,6):

    A.append(9**i)

A.sort()

dp=[10*5+5]*(N+1)

dp[0]=0

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

    for s in range(len(A)):

        if i-A[s]>=0:

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

print((dp[N]))

N = int(eval(input()))

def shin(n,q):

    s = n

    a = 0

    while(s>0):

        t=s%q

        a +=t

        s=(s-t)//q

    return a

ans = N

for i in range(N+1):

    c=shin(i,6)+shin(N-i,9)

    ans = min(ans,c)

print(ans)
N = int(eval(input()))
A = []
A.append(1)
for i in range(1, 7):
    A.append(6**i)
for i in range(1, 6):
    A.append(9**i)
A.sort()
dp = [10 * 5 + 5] * (N + 1)
dp[0] = 0
for i in range(1, N + 1):
    for s in range(len(A)):
        if i - A[s] >= 0:
            dp[i] = min(dp[i - A[s]] + 1, dp[i])
print((dp[N]))

N = int(eval(input()))


def shin(n, q):
    s = n
    a = 0
    while s > 0:
        t = s % q
        a += t
        s = (s - t) // q
    return a


ans = N
for i in range(N + 1):
    c = shin(i, 6) + shin(N - i, 9)
    ans = min(ans, c)
print(ans)

-A = [] -A.append(1) -for i in range(1, 7): -    A.append(6**i) -for i in range(1, 6): -    A.append(9**i) -A.sort() -dp = [10 * 5 + 5] * (N + 1) -dp[0] = 0 -for i in range(1, N + 1): -    for s in range(len(A)): -        if i - A[s] >= 0: -            dp[i] = min(dp[i - A[s]] + 1, dp[i]) -print((dp[N])) + + +def shin(n, q): +    s = n +    a = 0 +    while s > 0: +        t = s % q +        a += t +        s = (s - t) // q +    return a + + +ans = N +for i in range(N + 1): +    c = shin(i, 6) + shin(N - i, 9) +    ans = min(ans, c) +print(ans)