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 JavaScript (node.js v5.12) (judge verdict: Wrong Answer)

var lines = [];

var readline = require('readline');
var rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout,
})

rl.on('line', function (x) {
    lines.push(x);
});

rl.on("close", function () {
    var N = Number(lines[0]);
    var dp = Array(100000).fill(0);
    dp[Math.pow(6, 6)] = 1;
    dp[Math.pow(9, 5)] = 1, dp[Math.pow(6, 5)] = 1;
    dp[Math.pow(9, 4)] = 1, dp[Math.pow(6, 4)] = 1;
    dp[Math.pow(9, 3)] = 1, dp[Math.pow(6, 3)] = 1;
    dp[Math.pow(9, 2)] = 1, dp[Math.pow(6, 2)] = 1;
    dp[Math.pow(9, 1)] = 1, dp[Math.pow(6, 1)] = 1;
    dp[1] = 1;

    for (var index = 1; index <= N; index++) {
        var a = Infinity; var b = Infinity; var c = Infinity; var d = Infinity;
        var e = Infinity; var f = Infinity; var g = Infinity; var h = Infinity;
        var i = Infinity; var j = Infinity; var k = Infinity; var l = Infinity;
        
        if (0 <= index - Math.pow(6, 6)) b = dp[index - Math.pow(6, 6)] + dp[Math.pow(6, 6)];
        if (0 <= index - Math.pow(9, 5)) b = dp[index - Math.pow(9, 5)] + dp[Math.pow(9, 5)];
        if (0 <= index - Math.pow(6, 5)) c = dp[index - Math.pow(6, 5)] + dp[Math.pow(6, 5)];
        if (0 <= index - Math.pow(9, 4)) d = dp[index - Math.pow(9, 4)] + dp[Math.pow(9, 4)];
        if (0 <= index - Math.pow(6, 4)) e = dp[index - Math.pow(6, 4)] + dp[Math.pow(6, 4)];
        if (0 <= index - Math.pow(9, 3)) f = dp[index - Math.pow(9, 3)] + dp[Math.pow(9, 3)];
        if (0 <= index - Math.pow(6, 3)) g = dp[index - Math.pow(6, 3)] + dp[Math.pow(6, 3)];
        if (0 <= index - Math.pow(9, 2)) h = dp[index - Math.pow(9, 2)] + dp[Math.pow(9, 2)];
        if (0 <= index - Math.pow(6, 2)) i = dp[index - Math.pow(6, 2)] + dp[Math.pow(6, 2)];
        if (0 <= index - Math.pow(9, 1)) j = dp[index - Math.pow(9, 1)] + dp[Math.pow(9, 1)];
        if (0 <= index - Math.pow(6, 1)) k = dp[index - Math.pow(6, 1)] + dp[Math.pow(6, 1)];
        if (0 <= index - 1) l = dp[index - 1] + dp[1]

        dp[index] = Math.min(a, b, c, d, e, f, g, h, i, j, k, l);
    }

    console.log(dp[N]);
});