def s(n,c):

  if n<6:return c+n

  a=b=1

  while a*6<=n:a*=6

  while b*9<=n:b*=9

  return min(s(n-a,c+1),s(n-b,c+1))

print((s(int(eval(input())),0)))
def s(n,p):

  while n>0:yield n%p;n//=p

c=lambda*a:sum(s(*a));n=int(eval(input()));print((min(c(i,6)+c(n-i,9)for i in range(n+1))))
def s(n, c):
    if n < 6:
        return c + n
    a = b = 1
    while a * 6 <= n:
        a *= 6
    while b * 9 <= n:
        b *= 9
    return min(s(n - a, c + 1), s(n - b, c + 1))


print((s(int(eval(input())), 0)))

def s(n, p):
    while n > 0:
        yield n % p
        n //= p


c = lambda *a: sum(s(*a))
n = int(eval(input()))
print((min(c(i, 6) + c(n - i, 9) for i in range(n + 1))))

-def s(n, c): -    if n < 6: -        return c + n -    a = b = 1 -    while a * 6 <= n: -        a *= 6 -    while b * 9 <= n: -        b *= 9 -    return min(s(n - a, c + 1), s(n - b, c + 1)) +def s(n, p): +    while n > 0: +        yield n % p +        n //= p -print((s(int(eval(input())), 0))) +c = lambda *a: sum(s(*a)) +n = int(eval(input())) +print((min(c(i, 6) + c(n - i, 9) for i in range(n + 1))))