main = readLn >>= print . solve

solve n = minimum $ [f6 m + f9 (n-m) | m <- [0..n]]
    where f6 = \k -> sum $ map ((`mod` 6) . (k `div`)) [46656,7776,1296,216,36,6,1]
          f9 = \k -> sum $ map ((`mod` 9) . (k `div`)) [59049,6561,729,81,9,1]
