N = int(eval(input()))



ans = N + 1

for t in range(N + 1):

    cnt = 0

    v6 = t

    while v6 > 0:

        cnt += v6 % 6

        v6 //= 6

    v9 = N - t

    while v9 > 0:

        cnt += v9 % 9

        v9 //= 9

    ans = min(ans, cnt)



print(ans)

N = int(eval(input()))



ans = N

for v9 in range(N + 1):

    cnt = 0

    t = v9

    w = 9 ** 5

    while t >= 9:

        if t >= w:

            cnt9, t = divmod(t, w)

            cnt += cnt9

        w //= 9

    t = N - (v9 - t)

    w = 6 ** 7

    while t >= 6:

        if t >= w:

            cnt6, t = divmod(t, w)

            cnt += cnt6

        w //= 6

    ans = min(ans, cnt + t)



print(ans)

N = int(eval(input()))
ans = N + 1
for t in range(N + 1):
    cnt = 0
    v6 = t
    while v6 > 0:
        cnt += v6 % 6
        v6 //= 6
    v9 = N - t
    while v9 > 0:
        cnt += v9 % 9
        v9 //= 9
    ans = min(ans, cnt)
print(ans)

N = int(eval(input()))
ans = N
for v9 in range(N + 1):
    cnt = 0
    t = v9
    w = 9**5
    while t >= 9:
        if t >= w:
            cnt9, t = divmod(t, w)
            cnt += cnt9
        w //= 9
    t = N - (v9 - t)
    w = 6**7
    while t >= 6:
        if t >= w:
            cnt6, t = divmod(t, w)
            cnt += cnt6
        w //= 6
    ans = min(ans, cnt + t)
print(ans)

-ans = N + 1 -for t in range(N + 1): +ans = N +for v9 in range(N + 1): -    v6 = t -    while v6 > 0: -        cnt += v6 % 6 -        v6 //= 6 -    v9 = N - t -    while v9 > 0: -        cnt += v9 % 9 -        v9 //= 9 -    ans = min(ans, cnt) +    t = v9 +    w = 9**5 +    while t >= 9: +        if t >= w: +            cnt9, t = divmod(t, w) +            cnt += cnt9 +        w //= 9 +    t = N - (v9 - t) +    w = 6**7 +    while t >= 6: +        if t >= w: +            cnt6, t = divmod(t, w) +            cnt += cnt6 +        w //= 6 +    ans = min(ans, cnt + t)