N=int(eval(input()))

dp=[10**7]*3000000

dp[0]=0



pow6=[6**j for j in range(10) if 6**j <= N]

pow9=[9**j for j in range(10) if 9**j <= N]



for i in range(N):

    for p6 in pow6:

        dp[i+p6]=min(dp[i+p6], dp[i]+1)

    for p9 in pow9:

        dp[i+p9]=min(dp[i+p9], dp[i]+1)



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



ans = 10**10

for i in range(N+1):

    cc = 0



    t = i

    while t > 0:

        cc += t % 6

        t //= 6



    t = N-i

    while t > 0:

        cc += t % 9

        t //= 9



    if ans > cc:

        ans = cc

print(ans)

N = int(eval(input()))
dp = [10**7] * 3000000
dp[0] = 0
pow6 = [6**j for j in range(10) if 6**j <= N]
pow9 = [9**j for j in range(10) if 9**j <= N]
for i in range(N):
    for p6 in pow6:
        dp[i + p6] = min(dp[i + p6], dp[i] + 1)
    for p9 in pow9:
        dp[i + p9] = min(dp[i + p9], dp[i] + 1)
print((dp[N]))

N = int(eval(input()))
ans = 10**10
for i in range(N + 1):
    cc = 0
    t = i
    while t > 0:
        cc += t % 6
        t //= 6
    t = N - i
    while t > 0:
        cc += t % 9
        t //= 9
    if ans > cc:
        ans = cc
print(ans)

-dp = [10**7] * 3000000 -dp[0] = 0 -pow6 = [6**j for j in range(10) if 6**j <= N] -pow9 = [9**j for j in range(10) if 9**j <= N] -for i in range(N): -    for p6 in pow6: -        dp[i + p6] = min(dp[i + p6], dp[i] + 1) -    for p9 in pow9: -        dp[i + p9] = min(dp[i + p9], dp[i] + 1) -print((dp[N])) +ans = 10**10 +for i in range(N + 1): +    cc = 0 +    t = i +    while t > 0: +        cc += t % 6 +        t //= 6 +    t = N - i +    while t > 0: +        cc += t % 9 +        t //= 9 +    if ans > cc: +        ans = cc +print(ans)