let n = read_int()
let a = Array.make (n + 1) 0

let rec f i =
  if i = n + 1 then a.(n)
  else
    let rec g i j k =
      if j < k then j - k / i
      else g i j (k * i) in
    a.(i) <- 1 + min a.(g 6 i 1) a.(g 9 i 1);
    f (i + 1)

let () = Printf.printf "%d\n" (f 1)
