N = int(eval(input()))

ans = N

for i in range(N+1):

    cnt = 0

    t = i

    while t>0:

        cnt+=t%6

        t//=6

    j=N-i

    while j>0:

        cnt+=j%9

        j//=9

    ans = min(ans,cnt)

print(ans)
N=int(eval(input()))

ans=N

for i in range(N+1):

    count=0

    t=i

    while t>0:

        count+=t%6

        t//=6

    t=N-i

    while t>0:

        count+=t%9

        t//=9

    ans=min(ans,count)

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

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

-    cnt = 0 +    count = 0 -        cnt += t % 6 +        count += t % 6 -    j = N - i -    while j > 0: -        cnt += j % 9 -        j //= 9 -    ans = min(ans, cnt) +    t = N - i +    while t > 0: +        count += t % 9 +        t //= 9 +    ans = min(ans, count)