n=int(eval(input()))

l6=[]

l9=[]

num6=6

num9=9

while num6<=n:

  l6.append(num6)

  num6*=6

while num9<=n:

  l9.append(num9)

  num9*=9

count=[0,1,2,3,4,5,1,2,3,1,2,3]

for i in range(12,n+1):

  min=100000

  for j in l6:

    if i-j<0:

      pass

    else:

      if min>count[i-j]+1:

        min=count[i-j]+1

  for j in l9:

    if i-j<0:

      pass

    else:

      if min>count[i-j]+1:

        min=count[i-j]+1

  count.append(min)

print((count[n]))
n=int(eval(input()))

num6=6

num9=9

l=[]

while num6<=n:

  l.append(num6)

  num6*=6

while num9<=n:

  l.append(num9)

  num9*=9

count=[0,1,2,3,4,5,1,2,3,1,2,3]

for i in range(12,n+1):

  min=100000

  for j in l:

    if i-j<0:

      pass

    else:

      if min>count[i-j]+1:

        min=count[i-j]+1

  count.append(min)

print((count[n]))

n = int(eval(input()))
l6 = []
l9 = []
num6 = 6
num9 = 9
while num6 <= n:
    l6.append(num6)
    num6 *= 6
while num9 <= n:
    l9.append(num9)
    num9 *= 9
count = [0, 1, 2, 3, 4, 5, 1, 2, 3, 1, 2, 3]
for i in range(12, n + 1):
    min = 100000
    for j in l6:
        if i - j < 0:
            pass
        else:
            if min > count[i - j] + 1:
                min = count[i - j] + 1
    for j in l9:
        if i - j < 0:
            pass
        else:
            if min > count[i - j] + 1:
                min = count[i - j] + 1
    count.append(min)
print((count[n]))

n = int(eval(input()))
num6 = 6
num9 = 9
l = []
while num6 <= n:
    l.append(num6)
    num6 *= 6
while num9 <= n:
    l.append(num9)
    num9 *= 9
count = [0, 1, 2, 3, 4, 5, 1, 2, 3, 1, 2, 3]
for i in range(12, n + 1):
    min = 100000
    for j in l:
        if i - j < 0:
            pass
        else:
            if min > count[i - j] + 1:
                min = count[i - j] + 1
    count.append(min)
print((count[n]))

-l6 = [] -l9 = [] +l = [] -    l6.append(num6) +    l.append(num6) -    l9.append(num9) +    l.append(num9) -    for j in l6: -        if i - j < 0: -            pass -        else: -            if min > count[i - j] + 1: -                min = count[i - j] + 1 -    for j in l9: +    for j in l: