main :: IO ()
main = do
  n <- readLn
  print $ solve n

solve :: Int -> Int
solve n = minimum $ map sum $ [(ndigits 6 m) ++ (ndigits 9 (n - m)) | m <- [0..n]]

ndigits :: Int -> Int -> [Int]
ndigits k n = reverse $ inner n k
  where
    inner 0 _ = []
    inner n k = (n `mod` k):inner (n `div` k) k
