N=int(eval(input()))

l=[1,6,9,36,81,216,729,1296,6561,7776,46656,59049]

l2=[0]*100001



for i in range(1,100001):

  mn=100000000000000000

  for x in l:

    if i-x>=0 and l2[i-x]<mn:

      mn=l2[i-x]+1

  l2[i]=mn

print((l2[N]))
l=[1,6,9,36,81,216,729,1296,6561,7776,46656,59049]

l2=[0]*100001



N=int(eval(input()))



for i in range(1,N+1):

  mn=100000000000000000

  for x in l:

    if i-x>=0 and l2[i-x]<mn:

      mn=l2[i-x]+1

  l2[i]=mn



print((l2[N]))
N = int(eval(input()))
l = [1, 6, 9, 36, 81, 216, 729, 1296, 6561, 7776, 46656, 59049]
l2 = [0] * 100001
for i in range(1, 100001):
    mn = 100000000000000000
    for x in l:
        if i - x >= 0 and l2[i - x] < mn:
            mn = l2[i - x] + 1
    l2[i] = mn
print((l2[N]))

l = [1, 6, 9, 36, 81, 216, 729, 1296, 6561, 7776, 46656, 59049]
l2 = [0] * 100001
N = int(eval(input()))
for i in range(1, N + 1):
    mn = 100000000000000000
    for x in l:
        if i - x >= 0 and l2[i - x] < mn:
            mn = l2[i - x] + 1
    l2[i] = mn
print((l2[N]))

-N = int(eval(input())) -for i in range(1, 100001): +N = int(eval(input())) +for i in range(1, N + 1):