open Scanf
open Printf

let f9 n = let k = 9. ** (floor @@ log (float_of_int n) /. log 9.) in int_of_float k
let f6 n = let k = 6. ** (floor @@ log (float_of_int n) /. log 6.) in int_of_float k

let rec f n = if n < 6
  then n
  else let a = f9 n in
    let b = f6 n in
    min (n / a + f (n mod a)) (n / b + f (n mod b))

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