output_description: If at least x operations are required to withdraw exactly N yen in total,
  print x.
  
  * * *
input_description: Input is given from Standard Input in the following format:
  
      
      
      N
attempt: ###############################################################################
  
  from sys import stdout
  from bisect import bisect_left as binl
  from copy import copy, deepcopy
  from collections import defaultdict
  
  
  mod = 1
  
  
  def intin():
      input_tuple = input().split()
      if len(input_tuple) <= 1:
          return int(input_tuple[0])
      return tuple(map(int, input_tuple))
  
  
  def intina():
      return [int(i) for i in input().split()]
  
  
  def intinl(count):
      return [intin() for _ in range(count)]
  
  
  def modadd(x, y):
      global mod
      return (x + y) % mod
  
  
  def modmlt(x, y):
      global mod
      return (x * y) % mod
  
  
  def lcm(x, y):
      while y != 0:
          z = x % y
          x = y
          y = z
      return x
  
  
  def combination(x, y):
      assert x >= y
  
      if y > x // 2:
          y = x - y
  
      ret = 1
  
      for i in range(0, y):
          j = x - i
          i = i + 1
  
          ret = ret * j
          ret = ret // i
  
      return ret
  
  
  def get_divisors(x):
      retlist = []
      for i in range(1, int(x**0.5) + 3):
          if x % i == 0:
              retlist.append(i)
              retlist.append(x // i)
      return retlist
  
  
  def get_factors(x):
      retlist = []
      for i in range(2, int(x**0.5) + 3):
          while x % i == 0:
              retlist.append(i)
              x = x // i
      retlist.append(x)
      return retlist
  
  
  def make_linklist(xylist):
      linklist = {}
      for a, b in xylist:
          linklist.setdefault(a, [])
          linklist.setdefault(b, [])
          linklist[a].append(b)
          linklist[b].append(a)
      return linklist
  
  
  def calc_longest_distance(linklist, v=1):
      distance_list = {}
      distance_count = 0
      distance = 0
      vlist_previous = []
      vlist = [v]
      nodecount = len(linklist)
  
      while distance_count < nodecount:
          vlist_next = []
          for v in vlist:
              distance_list[v] = distance
              distance_count += 1
              vlist_next.extend(linklist[v])
          distance += 1
          vlist_to_del = vlist_previous
          vlist_previous = vlist
          vlist = list(set(vlist_next) - set(vlist_to_del))
  
      max_distance = -1
      max_v = None
      for v, distance in distance_list.items():
          if distance > max_distance:
              max_distance = distance
              max_v = v
  
      return (max_distance, max_v)
  
  
  def calc_tree_diameter(linklist, v=1):
      _, u = calc_longest_distance(linklist, v)
      distance, _ = calc_longest_distance(linklist, u)
      return distance
  
  
  ###############################################################################
  
  
  def main():
      n = intin()
  
      ans = n
  
      for i in range(n // 6 + 1):
          count = 0
  
          s6 = i * 6
          ii = i
          for j in range(1, n + 1):
              scount = ii % 6
              count += scount
              ii = ii // 6
              s6 -= (6**j) * scount
              if not s6:
                  break
  
          s9 = n - i * 6
          ii = s9
  
          for j in range(0, n + 1):
              scount = ii % 9
              count += scount
              ii = ii // 9
              s9 -= (9**j) * scount
              if not s9:
                  break
  
          ans = min(ans, count)
  
      print(ans)
  
  
  if __name__ == "__main__":
      main()
  
problem_description: Statement
  
  To make it difficult to withdraw money, a certain bank allows its customers to
  withdraw only one of the following amounts in one operation:
  
    * 1 yen (the currency of Japan)
  
    * 6 yen, 6^2(=36) yen, 6^3(=216) yen, ...
  
    * 9 yen, 9^2(=81) yen, 9^3(=729) yen, ...
  
  At least how many operations are required to withdraw exactly N yen in total?
  
  It is not allowed to re-deposit the money you withdrew.
samples: [{"input": "127", "output": "4\n    \n\nBy withdrawing 1 yen, 9 yen, 36(=6^2) yen and 81(=9^2) yen, we can withdraw\n127 yen in four operations.\n\n* * *"}, {"input": "3", "output": "3\n    \n\nBy withdrawing 1 yen three times, we can withdraw 3 yen in three operations.\n\n* * *"}, {"input": "44852", "output": "16"}]