import sys



def f(n):

  X6 = []

  X9 = []

  for i in range(0, 10):

    if n >= 6**i:

      X6.append(6**i)

    if n >= 9**i:

      X9.append(9**i)

  return max(X6), max(X9)





def main():

  input = sys.stdin.readline

  N = int(eval(input()))

  dp = [0 for _ in range(N+1)]

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

    can = []

    max6, max9 = f(i)

    X = [1, max6, max9]

    for x in X:

      can.append(dp[i-x] + 1)

    dp[i] = min(can)

  print((dp[N]))



if __name__ == '__main__':

  main()
import sys

from itertools import count, takewhile



def dfs(n, d={}):

  if n < 6:

    return n

  if n in d:

    return d[n]



  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())))

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

  return d[n]





def main():

  input = sys.stdin.readline

  N = int(eval(input()))

  print((dfs(N)))

  

if __name__ == '__main__':

  main()
import sys


def f(n):
    X6 = []
    X9 = []
    for i in range(0, 10):
        if n >= 6**i:
            X6.append(6**i)
        if n >= 9**i:
            X9.append(9**i)
    return max(X6), max(X9)


def main():
    input = sys.stdin.readline
    N = int(eval(input()))
    dp = [0 for _ in range(N + 1)]
    for i in range(1, N + 1):
        can = []
        max6, max9 = f(i)
        X = [1, max6, max9]
        for x in X:
            can.append(dp[i - x] + 1)
        dp[i] = min(can)
    print((dp[N]))


if __name__ == "__main__":
    main()

import sys
from itertools import count, takewhile


def dfs(n, d={}):
    if n < 6:
        return n
    if n in d:
        return d[n]
    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())))
    d[n] = min(dfs(n - max9), dfs(n - max6)) + 1
    return d[n]


def main():
    input = sys.stdin.readline
    N = int(eval(input()))
    print((dfs(N)))


if __name__ == "__main__":
    main()

+from itertools import count, takewhile -def f(n): -    X6 = [] -    X9 = [] -    for i in range(0, 10): -        if n >= 6**i: -            X6.append(6**i) -        if n >= 9**i: -            X9.append(9**i) -    return max(X6), max(X9) +def dfs(n, d={}): +    if n < 6: +        return n +    if n in d: +        return d[n] +    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()))) +    d[n] = min(dfs(n - max9), dfs(n - max6)) + 1 +    return d[n] -    dp = [0 for _ in range(N + 1)] -    for i in range(1, N + 1): -        can = [] -        max6, max9 = f(i) -        X = [1, max6, max9] -        for x in X: -            can.append(dp[i - x] + 1) -        dp[i] = min(can) -    print((dp[N])) +    print((dfs(N)))