N=int(eval(input()))

A=[]

for i in range(1,10):

    if 6**i<=100000:

        A.append(6**i)

    if 9**i<=100000:

        A.append(9**i)

L=[i for i in range(100001)]

A.sort()



for i in range(len(A)):

    for j in range(1,100001):

        if j>=A[i]:

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

print((L[N]))
N=int(eval(input()))



C=[1]

for i in range(1,100):

    if 6**i<=100000:

        C.append(6**i)

    else:

        break

    

for i in range(1,100):

    if 9**i<=100000:

        C.append(9**i)

    else:

        break



dp=[10**6 for i in range(N+1)]

dp[0]=0

for i in C:

    for j in range(1,len(dp)):

        if j-i>=0:

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

print((dp[N]))
N = int(eval(input()))
A = []
for i in range(1, 10):
    if 6**i <= 100000:
        A.append(6**i)
    if 9**i <= 100000:
        A.append(9**i)
L = [i for i in range(100001)]
A.sort()
for i in range(len(A)):
    for j in range(1, 100001):
        if j >= A[i]:
            L[j] = min(L[j], L[j - A[i]] + 1)
print((L[N]))

N = int(eval(input()))
C = [1]
for i in range(1, 100):
    if 6**i <= 100000:
        C.append(6**i)
    else:
        break
for i in range(1, 100):
    if 9**i <= 100000:
        C.append(9**i)
    else:
        break
dp = [10**6 for i in range(N + 1)]
dp[0] = 0
for i in C:
    for j in range(1, len(dp)):
        if j - i >= 0:
            dp[j] = min(dp[j], dp[j - i] + 1)
print((dp[N]))

-A = [] -for i in range(1, 10): +C = [1] +for i in range(1, 100): -        A.append(6**i) +        C.append(6**i) +    else: +        break +for i in range(1, 100): -        A.append(9**i) -L = [i for i in range(100001)] -A.sort() -for i in range(len(A)): -    for j in range(1, 100001): -        if j >= A[i]: -            L[j] = min(L[j], L[j - A[i]] + 1) -print((L[N])) +        C.append(9**i) +    else: +        break +dp = [10**6 for i in range(N + 1)] +dp[0] = 0 +for i in C: +    for j in range(1, len(dp)): +        if j - i >= 0: +            dp[j] = min(dp[j], dp[j - i] + 1) +print((dp[N]))