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.42.0) (judge verdict: Accepted)

#[allow(unused_imports)] use proconio::{input, marker::{Bytes, Chars, Usize1, Isize1}};
#[allow(unused_imports)] use std::cmp::{min, max};
#[allow(unused_imports)] use superslice::Ext as _;

#[proconio::fastout]
fn main() {
	input! {
		n: usize,
	}

	// 1
	// 6, 6^2=36, 6^3=216, 6^4=1296, 6^5=7776, 6^6=46656 
	// 9, 9^2=81, 9^3=729, 9^4=6561, 9^5=59049

	let mut ans = 999999usize;
	let mut sum = 0;
	for i1 in 0..=1 {	// 59049
		sum = i1 * 59049;
		for i2 in 0..=2 {	// 46656
			sum += i2 * 46656;
			if n < sum {
				sum -= i2 * 46656;
				break;
			}
			for i3 in 0..6 {	// 7776
				sum += i3 * 7776;
				if n < sum {
					sum -= i3 * 7776;
					break;
				}
				for i4 in 0..9 {	// 6561
					sum += i4 * 6561;
					if n < sum {
						sum -= i4 * 6561;
						break;
					}
					for i5 in 0..6 {	// 1296
						sum += i5 * 1296;
						if n < sum {
							sum -= i5 * 1296;
							break;
						}
						for i6 in 0..9 {	// 729
							sum += i6 * 729;
							if n < sum {
								sum -= i6 * 729;
								break;
							}
							for i7 in 0..6 {	// 216
								sum += i7 * 216;
								if n < sum {
									sum -= i7 * 216;
									break;
								}
								for i8 in 0..9 {	// 81
									sum += i8 * 81;
									if n < sum {
										sum -= i8 * 81;
										break;
									}
									for i9 in 0..6 {	// 36
										sum += i9 * 36;
										if n < sum {
											sum -= i9 * 36;
											break;
										}
										for i10 in 0..9 {	// 9
											sum += i10 * 9;
											if n < sum {
												sum -= i10 * 9;
												break;
											}
											for i11 in 0..6 {	// 6
												sum += i11 * 6;
												if n < sum {
													sum -= i11 * 6;
													break;
												}
												if n - sum < 6 {
													//println!("{}", sum + n - sum);
													//println!("{}", i1+i2+i3+i4+i5+i6+i7+i8+i9+i10+i11+n-sum);
													ans = min(ans, i1+i2+i3+i4+i5+i6+i7+i8+i9+i10+i11+n-sum);
												}
												sum -= i11 * 6;	
											}
											sum -= i10 * 9;
										}
										sum -= i9 * 36;
									}
									sum -= i8 * 81;
								}
								sum -= i7 * 216;
							}
							sum -= i6 * 729;
						}
						sum -= i5 * 1296;
					}
					sum -= i4 * 6561;
				}
				sum -= i3 * 7776;
			}
			sum -= i2 * 46656;
		}
	}
	println!("{}", ans);
}