from itertools import count



n = int(eval(input()))



dp = [0]*(n+1)



def it(n):

  yield 1

  m = 6

  while m <= n:

    yield m

    m *= 6

  m = 9

  while m <= n:

    yield m

    m *= 9



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

  dp[i] = min(dp[i-t] for t in it(i))+1



print((dp[n]))



def it(n,p):

  while n > 0:

    yield n%p

    n //= p

cost = lambda *a: sum(it(*a))



n = int(eval(input()))

print((min(cost(x,6)+cost(n-x,9) for x in range(n+1))))
from itertools import count

n = int(eval(input()))
dp = [0] * (n + 1)


def it(n):
    yield 1
    m = 6
    while m <= n:
        yield m
        m *= 6
    m = 9
    while m <= n:
        yield m
        m *= 9


for i in range(1, n + 1):
    dp[i] = min(dp[i - t] for t in it(i)) + 1
print((dp[n]))

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


cost = lambda *a: sum(it(*a))
n = int(eval(input()))
print((min(cost(x, 6) + cost(n - x, 9) for x in range(n + 1))))

-from itertools import count - -n = int(eval(input())) -dp = [0] * (n + 1) +def it(n, p): +    while n > 0: +        yield n % p +        n //= p -def it(n): -    yield 1 -    m = 6 -    while m <= n: -        yield m -        m *= 6 -    m = 9 -    while m <= n: -        yield m -        m *= 9 - - -for i in range(1, n + 1): -    dp[i] = min(dp[i - t] for t in it(i)) + 1 -print((dp[n])) +cost = lambda *a: sum(it(*a)) +n = int(eval(input())) +print((min(cost(x, 6) + cost(n - x, 9) for x in range(n + 1))))