N = int(eval(input()))

r = N



for i in range(N+1):

  c=0;t=i

  while t>0:c+=t%6;t//=6

  t=N-i

  while t>0:c+=t%9;t//=9

  r=min(r,c)

  

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

a = N



for n in range(N+1):

  c = 0

  t = n

  while t>0:

    c+=t%6

    t//=6

  t = N-n

  while t>0:

    c+=t%9

    t//=9

  a = min(a,c)

  

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

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

-r = N -for i in range(N + 1): +a = N +for n in range(N + 1): -    t = i +    t = n -    t = N - i +    t = N - n -    r = min(r, c) -print(r) +    a = min(a, c) +print(a)