n = int(eval(input()))

dp = [0]



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

    q = []

    q.append(1+dp[i-1])



    a = 1

    while True:

        a *= 6

        if a > i:

            break

        q.append(1+dp[i-a])



    b = 1

    while True:

        b *= 9

        if b > i:

            break

        q.append(1+dp[i-b])



    dp.append(min(q))



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

ans = pow(10, 10)



for i in range(n+1):

    cnt = 0

    j = n - i

    while i > 0:

        cnt += i % 6

        i //= 6



    while j > 0:

        cnt += j % 9

        j //= 9



    ans = min(ans, cnt)



print(ans)
n = int(eval(input()))
dp = [0]
for i in range(1, n + 1):
    q = []
    q.append(1 + dp[i - 1])
    a = 1
    while True:
        a *= 6
        if a > i:
            break
        q.append(1 + dp[i - a])
    b = 1
    while True:
        b *= 9
        if b > i:
            break
        q.append(1 + dp[i - b])
    dp.append(min(q))
print((dp[n]))

n = int(eval(input()))
ans = pow(10, 10)
for i in range(n + 1):
    cnt = 0
    j = n - i
    while i > 0:
        cnt += i % 6
        i //= 6
    while j > 0:
        cnt += j % 9
        j //= 9
    ans = min(ans, cnt)
print(ans)

-dp = [0] -for i in range(1, n + 1): -    q = [] -    q.append(1 + dp[i - 1]) -    a = 1 -    while True: -        a *= 6 -        if a > i: -            break -        q.append(1 + dp[i - a]) -    b = 1 -    while True: -        b *= 9 -        if b > i: -            break -        q.append(1 + dp[i - b]) -    dp.append(min(q)) -print((dp[n])) +ans = pow(10, 10) +for i in range(n + 1): +    cnt = 0 +    j = n - i +    while i > 0: +        cnt += i % 6 +        i //= 6 +    while j > 0: +        cnt += j % 9 +        j //= 9 +    ans = min(ans, cnt) +print(ans)