open Batteries

let () =
  let n = Scanf.scanf "%d " (fun a -> a) in

  let rec f res n b =
    if n = 0 then res
    else f (res + (n mod b)) (n / b) b
  in

  let rec loop i ans =
    if i < n then (
      let i9 = i in
      let tmp1 = (n - (9 * i9)) in
      let i6 = tmp1 / 6 in
      let i1 = tmp1 mod 6 in
      let ans_c = (f 0 i9 9) + (f 0 i6 6) + i1 in
      if tmp1 >= 0 then
        loop (i+1) (if ans < ans_c then ans else ans_c)
      else
        ans

    ) else (
      ans
    )
  in
  Printf.printf "%d\n" (loop 0 100000);

