Problem:
Score : 300 points

Problem Statement

To make it difficult to withdraw money, a certain bank allows its customers to withdraw only one of the following amounts in one operation:

1 yen (the currency of Japan)

6 yen, 6^2(=36) yen, 6^3(=216) yen, ...

9 yen, 9^2(=81) yen, 9^3(=729) yen, ...

At least how many operations are required to withdraw exactly N yen in total?

It is not allowed to re-deposit the money you withdrew.

Constraints

1 \leq N \leq 100000

N is an integer.

Input

Input is given from Standard Input in the following format:

N

Output

If at least x operations are required to withdraw exactly N yen in total, print x.

Sample Input 1

127

Sample Output 1

4

By withdrawing 1 yen, 9 yen, 36(=6^2) yen and 81(=9^2) yen, we can withdraw 127 yen in four operations.

Sample Input 2

3

Sample Output 2

3

By withdrawing 1 yen three times, we can withdraw 3 yen in three operations.

Sample Input 3

44852

Sample Output 3

16

Solution in Rust (1.15.1) (judge verdict: Accepted)

#![allow(unused_imports)]
#![cfg_attr(feature = "cargo-clippy", allow(redundant_field_names))]

use std::collections::{BTreeMap, BTreeSet, BinaryHeap, HashMap, HashSet, VecDeque};
use std::io::{self, BufWriter, Read, Write};
use std::ops::{Add, Div, Mul, Sub};
use std::str::{self, FromStr};
use std::{cmp, fmt};

fn main() {
    let n: u64 = {
        let mut sc = InputScanOnce::new(io::stdin(), 1024);
        sc.next()
    };
    let mut m = n;
    'a: for d6 in 0..n / 6 + 1 {
        for d9 in 0..n / 9 + 1 {
            let d = d6 * 6 + d9 * 9;
            if n < d {
                continue 'a;
            }
            let c1 = n - d;
            let c6 = f6(d6);
            let c9 = f9(d9);
            m = cmp::min(m, c1 + c6 + c9);
        }
    }
    println!("{}", m);
}

fn f6(mut d: u64) -> u64 {
    let a5 = d / 6u64.pow(5);
    d -= a5 * 6u64.pow(5);
    let a4 = d / 6u64.pow(4);
    d -= a4 * 6u64.pow(4);
    let a3 = d / 6u64.pow(3);
    d -= a3 * 6u64.pow(3);
    let a2 = d / 6u64.pow(2);
    d -= a2 * 6u64.pow(2);
    let a1 = d / 6;
    d -= a1 * 6;
    d + a1 + a2 + a3 + a4 + a5
}

fn f9(mut d: u64) -> u64 {
    let a5 = d / 9u64.pow(5);
    d -= a5 * 9u64.pow(5);
    let a4 = d / 9u64.pow(4);
    d -= a4 * 9u64.pow(4);
    let a3 = d / 9u64.pow(3);
    d -= a3 * 9u64.pow(3);
    let a2 = d / 9u64.pow(2);
    d -= a2 * 9u64.pow(2);
    let a1 = d / 9;
    d -= a1 * 9;
    d + a1 + a2 + a3 + a4 + a5
}

struct InputScanOnce {
    buf: Vec<u8>,
    pos: usize,
}

#[allow(dead_code)]
impl InputScanOnce {
    fn new<R: Read>(mut reader: R, estimated: usize) -> Self {
        let mut buf = Vec::with_capacity(estimated);
        let _ = io::copy(&mut reader, &mut buf).unwrap();
        InputScanOnce { buf: buf, pos: 0 }
    }

    #[inline]
    fn next<T: FromStr>(&mut self) -> T
    where
        T::Err: fmt::Debug,
    {
        let mut start = None;
        loop {
            match (self.buf[self.pos], start.is_some()) {
                (b' ', true) | (b'\n', true) => break,
                (_, true) | (b' ', false) | (b'\n', false) => self.pos += 1,
                (_, false) => start = Some(self.pos),
            }
        }
        let target = &self.buf[start.unwrap()..self.pos];
        unsafe { str::from_utf8_unchecked(target) }.parse().unwrap()
    }

    fn vec<T: FromStr>(&mut self, n: usize) -> Vec<T>
    where
        T::Err: fmt::Debug,
    {
        (0..n).map(|_| self.next()).collect()
    }

    fn pairs<T1: FromStr, T2: FromStr>(&mut self, n: usize) -> Vec<(T1, T2)>
    where
        T1::Err: fmt::Debug,
        T2::Err: fmt::Debug,
    {
        (0..n).map(|_| (self.next(), self.next())).collect()
    }

    fn trios<T1: FromStr, T2: FromStr, T3: FromStr>(&mut self, n: usize) -> Vec<(T1, T2, T3)>
    where
        T1::Err: fmt::Debug,
        T2::Err: fmt::Debug,
        T3::Err: fmt::Debug,
    {
        (0..n)
            .map(|_| (self.next(), self.next(), self.next()))
            .collect()
    }

    fn mat<T: FromStr>(&mut self, m: usize, n: usize) -> Vec<Vec<T>>
    where
        T::Err: fmt::Debug,
    {
        (0..m).map(|_| self.vec(n)).collect()
    }

    fn strings_as_mat<T, F: FnMut(u8) -> T>(&mut self, h: usize, mut f: F) -> Vec<Vec<T>> {
        (0..h)
            .map(|_| {
                let l = self.next::<String>();
                l.as_bytes().iter().cloned().map(&mut f).collect()
            })
            .collect()
    }
}