radixConvert b n
  | n < b = [n]
  | otherwise = (mod n b) : (radixConvert b (div n b))

main = do
  n <- readLn :: IO Int
  print $ minimum [sum (radixConvert 6 i) + sum (radixConvert 9 (n-i))| i<-[0..n]]
  
