adic_i::Int->Int->Int
adic_i i n = if n < i then n else (mod n i)+(adic_i  i (div n i))

solver::Int->Int
solver n = minimum [(adic_i 6 (n-i))+(adic_i 9 i)|i<-[0..n]]

main::IO()
main=do
  nc<-getLine
  let n=read nc
  print (solver n)