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 Java8 (OpenJDK 1.8.0) (judge verdict: Accepted)

import java.util.Arrays;
import java.util.Scanner;

/* 参考:　貰う DP と配る DP、メモ化再帰、個数制限なしナップサック問題 @drken https://qiita.com/drken/items/ace3142967c4f01d42e9#%E8%A7%A3%E6%B3%95-1-1-%E3%83%88%E3%83%83%E3%83%97%E3%83%80%E3%82%A6%E3%83%B3%E3%81%AB-n-%E3%82%92%E9%99%8D%E4%B8%8B%E3%81%97%E3%81%A6%E3%81%84%E3%81%8F%E3%83%A1%E3%83%A2%E5%8C%96%E5%86%8D%E5%B8%B0 */

public class Main {
	static final int MAX_N = 110000;
	static int[] dp = new int[MAX_N];
	static {
		Arrays.fill(dp, MAX_N); // 初期化 Infinity
		dp[0] = 0;
	}
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		final int N = sc.nextInt();
		calc(N);
		System.out.println(dp[N]);
	}

	static void calc(int N) {
		for (int n = 1; n <= N; n++) {
			for (int pow6 = 1; pow6 <= n; pow6*=6) {
				dp[n] = Math.min(dp[n], dp[n-pow6] + 1);
			}
			for (int pow9 = 1; pow9 <= n; pow9*=9) {
				dp[n] = Math.min(dp[n], dp[n-pow9] + 1);
			}
		}
	}
}