open Scanf
open Printf

let rec f n = let n9 = List.fold_left min 100000 @@ f9 n 9 in
  let n6 = List.fold_left min 100000 @@ f6 n 6 in
  min n @@ min n6 n9
and f9 n k = if n < k then [] else (n / k) + f (n mod k) :: f9 n (k*9)
and f6 n k = if n < k then [] else (n / k) + f (n mod k) :: f9 n (k*6)

let () = let ans = scanf "%d\n" f in
  printf "%d\n" ans
