let () = Scanf.scanf "%d" @@ fun n ->
  let counts = Array.init (n + 1) @@ fun i -> i in
  for p = 0 to int_of_float (ceil (log (float_of_int n) /. log 6.)) do
    for i = int_of_float (6. ** float_of_int p) to n do
      counts.(i) <- min counts.(i) (counts.(i - int_of_float (6. ** float_of_int p)) + 1)
    done
  done;
  for p = 0 to int_of_float (ceil (log (float_of_int n) /. log 9.)) do
    for i = int_of_float (9. ** float_of_int p) to n do
      counts.(i) <- min counts.(i) (counts.(i - int_of_float (9. ** float_of_int p)) + 1)
    done
  done;
  Printf.printf "%d\n" counts.(n)
