n = int(eval(input()))

INF = 10**5

answer = INF



for i in range(n+1):

    cost = i

    ans = 0

    for p in range(8):

        ans += (cost // 6**p) % 6



    cost = n-i

    for q in range(7):

        ans += (cost // 9**q) % 9

    if answer > ans:

        answer = ans

print(answer)

n = int(eval(input()))

INF = 10**5

answer = INF

for i in range(n+1):

    money = i

    cnt = 0

    while money > 0:

        cnt += money % 6

        money //= 6



    money = n-i

    while money > 0:

        cnt += money % 9

        money //= 9

    if answer > cnt:

        answer = cnt

print(answer)
n = int(eval(input()))
INF = 10**5
answer = INF
for i in range(n + 1):
    cost = i
    ans = 0
    for p in range(8):
        ans += (cost // 6**p) % 6
    cost = n - i
    for q in range(7):
        ans += (cost // 9**q) % 9
    if answer > ans:
        answer = ans
print(answer)

n = int(eval(input()))
INF = 10**5
answer = INF
for i in range(n + 1):
    money = i
    cnt = 0
    while money > 0:
        cnt += money % 6
        money //= 6
    money = n - i
    while money > 0:
        cnt += money % 9
        money //= 9
    if answer > cnt:
        answer = cnt
print(answer)

-    cost = i -    ans = 0 -    for p in range(8): -        ans += (cost // 6**p) % 6 -    cost = n - i -    for q in range(7): -        ans += (cost // 9**q) % 9 -    if answer > ans: -        answer = ans +    money = i +    cnt = 0 +    while money > 0: +        cnt += money % 6 +        money //= 6 +    money = n - i +    while money > 0: +        cnt += money % 9 +        money //= 9 +    if answer > cnt: +        answer = cnt