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: Wrong Answer)

use std::io::*;
use std::str::FromStr;
use std::vec::*;

fn read<T: FromStr>() -> T {
    let stdin = stdin();
    let stdin = stdin.lock();
    let token: String = stdin
        .bytes()
        .map(|c| c.expect("failed to read char") as char)
        .skip_while(|c| c.is_whitespace())
        .take_while(|c| !c.is_whitespace())
        .collect();
    token.parse().ok().expect("failed to parse token")
}
fn readChars(size: usize) -> Vec<char> {
    let mut s0: String = read();
    let mut s = Vec::new();
    for _ in 0..size {
        s.push(s0.pop().expect(""));
    }
    s.reverse();
    s
}

use std::cmp::*;

fn main() {
    let mut n: u32 = read();

    let xs: Vec<u32> = (0..6).collect();
    let p9s: Vec<u32> = xs.iter().map(|&x| 9u32.pow(x)).collect();
    let ys: Vec<u32> = (0..7).collect();
    let p6s: Vec<u32> = ys.iter().map(|&x| 6u32.pow(x)).collect();

    // let mut r = 0;
    // let mut i9 = 5;
    // let mut i6 = 6;

    // while n > 0 {
    //     if p9s[i9] > p6s[i6] {
    //         if i9 > 0 {
    //             if n >= p9s[i9] {
    //                 n -= p9s[i9];
    //                 r += 1;
    //             }
    //             else {
    //                 i9 -= 1;
    //             }
    //         }
    //         else if i6 > 0 {
    //             if n >= p6s[i6] {
    //                 n -= p6s[i6];
    //                 r += 1;
    //             }
    //             else {
    //                 i6 -= 1;
    //             }
    //         }
    //         else {
    //             break;
    //         }
    //     }
    //     else {
    //         if i6 > 0 {
    //             if n >= p6s[i6] {
    //                 n -= p6s[i6];
    //                 r += 1;
    //             }
    //             else {
    //                 i6 -= 1;
    //             }
    //         }
    //         else if i9 > 0 {
    //             if n >= p9s[i9] {
    //                 n -= p9s[i9];
    //                 r += 1;
    //             }
    //             else {
    //                 i9 -= 1;
    //             }
    //         }
    //         else {
    //             break;
    //         }
    //     }
    //
    //
    // }


    println!("{:?}, {:?}", p9s, p6s);

    // r += n;

    println!("{}", count(&p9s, &p6s, n, 5, 6));

    // println!("{}", r);
}

fn count(p9s: &Vec<u32>, p6s: &Vec<u32>, n: u32, i9: u32, i6: u32) -> u32 {
    let r9 = if i9 > 0 {
        if n >= p9s[i9 as usize] {
            count(p9s, p6s, n - p9s[i9 as usize], i9, i6) + 1
        }
        else {
            count(p9s, p6s, n, i9 - 1, i6)
        }
    } else {10000};
    let r6 = if i6 > 0 {
        if n >= p6s[i6 as usize] {
            count(p9s, p6s, n - p6s[i6 as usize], i9, i6) + 1
        }
        else {
            count(p9s, p6s, n, i9, i6 - 1)
        }
    } else {10000};

    let r = min(r9, r6);

    min(r, n)
}