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; 
using System.Text; 
using System.Collections.Generic;
using System.Linq; 
using E = System.Linq.Enumerable; 

class Program {
     static List<int> DrawCount;
     static void Main(string[] args) {
     //入力を受け取る
     var N = int.Parse(Console.ReadLine());
     DrawCount = new List<int>();
     DrawStep(N,0);
     
     Console.WriteLine(DrawCount.Min());
     }
     
     
     static void DrawStep(int N,int count){
         if(N <= 0){
             DrawCount.Add(count);
         }else{
             var over6 =
             (int)Math.Pow(6,Over(N,6));
             var cou6  = N/over6;              
             DrawStep(N-over6*cou6,count+cou6);
             var over9 =
             (int)Math.Pow(9,Over(N,9));
             var cou9  = N/over9;
             DrawStep(N-over9*cou9,count+cou9);
         }
     }
     
     static int Over(int N,int M){
         var count = 0;
         while(N/M !=0){
             N /= M;
             count++;
         }
         return count;
     }
}

//進数計算用クラス（マイナスにも対応！！）
static class Base{
          //10進数のMをN進数に変換する
        public static string MConvertToBaseN(long N,long M){
          //0の場合は空文字を返してしまうので0を返すように対応
          if(BaseN(N,M) == ""){
            return "0";
          }else{
            return BaseN(N,M);
          }
        }
  
        static string BaseN(long N,long M){
          //0進数の場合はエラーを返す
          if(N == 0){
            return "0進数にはできません";
          }
          if(M == 0){
            return "";
          }else{
            return BaseN(N,NonMinusDivision(M,N))+(NonMinusRest(M,N)).ToString();
          }
        }
        
        //非負剰余の商　a = b*c + r ( abs(b)>r>= 0 )
        //非負剰余で定義したときの商
  　　　 static long NonMinusDivision(double a , double b){
        	if(a > 0 && b > 0){
              return (long) (Math.Floor(a/b));
            }
            else{
              return (long) (Math.Ceiling(a/b));
            }
        }
        
  　　　//非負剰余の余
        static long NonMinusRest(long m, long n){
          return m-NonMinusDivision(m,n)*n;
        }
}