n=int(eval(input()))

s_6=[6**(6-i) for  i in range(6)] 

s_9=[9**(5-i) for  i in range(5)]

amari=[0,1,2,3,4,5,1,2,3,1,2,3,2,3]



best=float("inf")

for i in range(n+1):

    count_6=0

    n_6=i

    for j in range(len(s_6)):

        count_6+=n_6//s_6[j]

        n_6=n_6%s_6[j]



    n_9=n-i

    count_9=0

    for j in range(len(s_9)):

        count_9+=n_9//s_9[j]

        n_9=n_9%s_9[j]



    best=min(best, count_6+count_9+ amari[n_6+n_9])



print(best)
n = int(eval(input()))

ans = float("inf")

for n_nine in range(0, n//9 + 1):

    s = n - 9 * n_nine

    n_six = s // 6

    n_one = s - 6 * n_six

    n_nine2 = 0

    n_six2 = 0

    pow_nine = 5

    pow_six = 6

    

    while n_nine >= 9:

        if n_nine >= 9 ** (pow_nine - 1):

            n_nine -= 9 ** (pow_nine - 1)

            n_nine2 += 1

        else:

          pow_nine -= 1



    while n_six >= 6:

        if n_six >= 6 ** (pow_six - 1):

            n_six -= 6 ** (pow_six - 1)

            n_six2 += 1

        else:

          pow_six -= 1          

          

    ans = min(ans, n_nine2 + n_nine + n_six2 + n_six + n_one)

          

print(ans)
n = int(eval(input()))
s_6 = [6 ** (6 - i) for i in range(6)]
s_9 = [9 ** (5 - i) for i in range(5)]
amari = [0, 1, 2, 3, 4, 5, 1, 2, 3, 1, 2, 3, 2, 3]
best = float("inf")
for i in range(n + 1):
    count_6 = 0
    n_6 = i
    for j in range(len(s_6)):
        count_6 += n_6 // s_6[j]
        n_6 = n_6 % s_6[j]
    n_9 = n - i
    count_9 = 0
    for j in range(len(s_9)):
        count_9 += n_9 // s_9[j]
        n_9 = n_9 % s_9[j]
    best = min(best, count_6 + count_9 + amari[n_6 + n_9])
print(best)

n = int(eval(input()))
ans = float("inf")
for n_nine in range(0, n // 9 + 1):
    s = n - 9 * n_nine
    n_six = s // 6
    n_one = s - 6 * n_six
    n_nine2 = 0
    n_six2 = 0
    pow_nine = 5
    pow_six = 6
    while n_nine >= 9:
        if n_nine >= 9 ** (pow_nine - 1):
            n_nine -= 9 ** (pow_nine - 1)
            n_nine2 += 1
        else:
            pow_nine -= 1
    while n_six >= 6:
        if n_six >= 6 ** (pow_six - 1):
            n_six -= 6 ** (pow_six - 1)
            n_six2 += 1
        else:
            pow_six -= 1
    ans = min(ans, n_nine2 + n_nine + n_six2 + n_six + n_one)
print(ans)

-s_6 = [6 ** (6 - i) for i in range(6)] -s_9 = [9 ** (5 - i) for i in range(5)] -amari = [0, 1, 2, 3, 4, 5, 1, 2, 3, 1, 2, 3, 2, 3] -best = float("inf") -for i in range(n + 1): -    count_6 = 0 -    n_6 = i -    for j in range(len(s_6)): -        count_6 += n_6 // s_6[j] -        n_6 = n_6 % s_6[j] -    n_9 = n - i -    count_9 = 0 -    for j in range(len(s_9)): -        count_9 += n_9 // s_9[j] -        n_9 = n_9 % s_9[j] -    best = min(best, count_6 + count_9 + amari[n_6 + n_9]) -print(best) +ans = float("inf") +for n_nine in range(0, n // 9 + 1): +    s = n - 9 * n_nine +    n_six = s // 6 +    n_one = s - 6 * n_six +    n_nine2 = 0 +    n_six2 = 0 +    pow_nine = 5 +    pow_six = 6 +    while n_nine >= 9: +        if n_nine >= 9 ** (pow_nine - 1): +            n_nine -= 9 ** (pow_nine - 1) +            n_nine2 += 1 +        else: +            pow_nine -= 1 +    while n_six >= 6: +        if n_six >= 6 ** (pow_six - 1): +            n_six -= 6 ** (pow_six - 1) +            n_six2 += 1 +        else: +            pow_six -= 1 +    ans = min(ans, n_nine2 + n_nine + n_six2 + n_six + n_one) +print(ans)