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

for i in range(N + 1):

    cnt = 0

    t = i

    while t > 0:

        cnt += t % 9

        t //= 9

    t = N - i

    while t > 0:

        cnt += t % 6

        t //= 6

    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
for i in range(N + 1):
    cnt = 0
    t = i
    while t > 0:
        cnt += t % 9
        t //= 9
    t = N - i
    while t > 0:
        cnt += t % 6
        t //= 6
    ans = min(ans, cnt)
print(ans)

-for v9 in range(N + 1): +for i in range(N + 1): -    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) +    t = i +    while t > 0: +        cnt += t % 9 +        t //= 9 +    t = N - i +    while t > 0: +        cnt += t % 6 +        t //= 6 +    ans = min(ans, cnt)