from itertools import count, takewhile

from functools import lru_cache

 

@lru_cache(maxsize=None)

def dfs(n):

  if n == 0: return 0

  

  max9 = max(takewhile(lambda x: x<=n, (9**i for i in count())))

  max6 = max(takewhile(lambda x: x<=n, (6**i for i in count())))

  return min(dfs(n-max9), dfs(n-max6))+1

 

print((dfs(int(eval(input())))))
from itertools import count, takewhile, dropwhile



def dfs(n, max9, max6, d={0:0}):

  if n in d: return d[n]

  

  max9 = next(dropwhile(lambda x: x>n, (max9 / 9**i for i in count())))

  max6 = next(dropwhile(lambda x: x>n, (max6 / 6**i for i in count())))

  d[n] = min(dfs(n-max9, max9, max6), dfs(n-max6, max9, max6)) + 1

  return d[n]



n = int(eval(input()))



max9 = max(takewhile(lambda x: x<=n, (9**i for i in count())))

max6 = max(takewhile(lambda x: x<=n, (6**i for i in count())))



print((dfs(n, max9, max6)))
from itertools import count, takewhile
from functools import lru_cache


@lru_cache(maxsize=None)
def dfs(n):
    if n == 0:
        return 0
    max9 = max(takewhile(lambda x: x <= n, (9**i for i in count())))
    max6 = max(takewhile(lambda x: x <= n, (6**i for i in count())))
    return min(dfs(n - max9), dfs(n - max6)) + 1


print((dfs(int(eval(input())))))

from itertools import count, takewhile, dropwhile


def dfs(n, max9, max6, d={0: 0}):
    if n in d:
        return d[n]
    max9 = next(dropwhile(lambda x: x > n, (max9 / 9**i for i in count())))
    max6 = next(dropwhile(lambda x: x > n, (max6 / 6**i for i in count())))
    d[n] = min(dfs(n - max9, max9, max6), dfs(n - max6, max9, max6)) + 1
    return d[n]


n = int(eval(input()))
max9 = max(takewhile(lambda x: x <= n, (9**i for i in count())))
max6 = max(takewhile(lambda x: x <= n, (6**i for i in count())))
print((dfs(n, max9, max6)))

-from itertools import count, takewhile -from functools import lru_cache +from itertools import count, takewhile, dropwhile -@lru_cache(maxsize=None) -def dfs(n): -    if n == 0: -        return 0 -    max9 = max(takewhile(lambda x: x <= n, (9**i for i in count()))) -    max6 = max(takewhile(lambda x: x <= n, (6**i for i in count()))) -    return min(dfs(n - max9), dfs(n - max6)) + 1 +def dfs(n, max9, max6, d={0: 0}): +    if n in d: +        return d[n] +    max9 = next(dropwhile(lambda x: x > n, (max9 / 9**i for i in count()))) +    max6 = next(dropwhile(lambda x: x > n, (max6 / 6**i for i in count()))) +    d[n] = min(dfs(n - max9, max9, max6), dfs(n - max6, max9, max6)) + 1 +    return d[n] -print((dfs(int(eval(input()))))) +n = int(eval(input())) +max9 = max(takewhile(lambda x: x <= n, (9**i for i in count()))) +max6 = max(takewhile(lambda x: x <= n, (6**i for i in count()))) +print((dfs(n, max9, max6)))