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 C# (Mono 4.6.2.0) (judge verdict: Wrong Answer)

using System;
namespace ABC099C
{
    internal class Program
    {
        public static void Main(string[] args)
        {
            long n = long.Parse(Console.ReadLine());
            int[] use =
            {
                59049,
                46656,
                7776,
                6561,
                1296,
                729,
                216,
                81,
                36,
                9,
                6,
                1,
            };


            int ans = int.MaxValue;
            for (int i1 = 0; i1 < 2; i1++)
            {
                for (int i2 = 0; i2 < 2; i2++)
                {
                    for (int i3 = 0; i3 < 4; i3++)
                    {
                        for (int i4 = 0; i4 < 4; i4++)
                        {
                            for (int i5 = 0; i5 < 4; i5++)
                            {
                                for (int i6 = 0; i6 < 4; i6++)
                                {
                                    for (int i7 = 0; i7 < 4; i7++)
                                    {
                                        for (int i8 = 0; i8 < 4; i8++)
                                        {
                                            for (int i9 = 0; i9 < 6; i9++)
                                            {
                                                for (int i10 = 0; i10 < 6; i10++)
                                                {
                                                    for (int i11 = 0; i11 < 6; i11++)
                                                    {
                                                        for (int i12 = 0; i12 < 6; i12++)
                                                        {
                                                            if (i1 * use[0]
                                                                + i2 * use[1]
                                                                + i3 * use[2]
                                                                + i4 * use[3]
                                                                + i5 * use[4]
                                                                + i6 * use[5]
                                                                + i7 * use[6]
                                                                + i8 * use[7]
                                                                + i9 * use[8]
                                                                + i10 * use[9]
                                                                + i11 * use[10]
                                                                + i12 * use[11]
                                                                == n)
                                                            {
                                                                ans = Math.Min(ans,
                                                                    i1 + i2 + i3 + i4 + i5 + i6 + i7 + i8 + i9 + i10 +
                                                                    i11 + i12);
                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
            Console.WriteLine(ans);
        }
    }
}