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 (GCC 5.4.1) (judge verdict: Wrong Answer)

//q1
// pactice
 
#pragma warning(disable:4996)
#pragma GCC diagnostic ignored "-Wunused-result"
#pragma GCC diagnostic ignored "-Wwrite-strings"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include <math.h>
#define MAX_NUMBER 100000
#define MAX_STRING 10000

	int compare(const int *val1, const int *val2);

int main() {
 
	char* psInput=NULL;
	int nGoalMoney=0;
	int nCount=0;
	int anRadixSet[12];
	int nTemp=0;
	int nIndex=11;

	char sInput[MAX_STRING+1]="";

	int i=0;
	
	anRadixSet[0]=1;
	nTemp=1;
	for(i=1;i<=6;i++){
		nTemp=nTemp*6;
		anRadixSet[i]=nTemp;
	}
	nTemp=1;
	for(i=7;i<=11;i++){
		nTemp=nTemp*9;
		anRadixSet[i]=nTemp;
	}

    qsort(anRadixSet, 12, sizeof(int),
        (int (*)(const void *, const void *))compare
    );
	// read line 1
	gets(sInput);
	nGoalMoney=atoi(sInput);

	if(nGoalMoney<=5){
		nCount=nGoalMoney;
	}else{
		while(nGoalMoney>0 && nIndex>2){
			if(nGoalMoney>=12 && nGoalMoney <=14){
				nIndex=1;
				nGoalMoney=nGoalMoney-anRadixSet[nIndex];
				nCount++;
			} else if(nGoalMoney>=anRadixSet[nIndex]){
				nGoalMoney=nGoalMoney-anRadixSet[nIndex];
	//			printf("left %d ...\n",nGoalMoney);
				nCount++;
			} else {
				nIndex--;
			}
		}
		if(nGoalMoney % 9 >=6 ){
			// 目標値から9ずつ引いていき、最後に6を引く。その分だけ回数が+1。
			nCount=nCount+(nGoalMoney/9)+1+((nGoalMoney % 9)-6);
		} else if(nGoalMoney % 9 >=3 ){
			// 目標値から9ずつ引いていくが、最後は9ではなく12(6+6)を引く。その分だけ回数が+1。
			nCount=nCount+(nGoalMoney/9)+1+((nGoalMoney % 9)-3);
		}else{
			// 目標値から9ずつ引いていけば最適値になる場合
			nCount=nCount+(nGoalMoney/9)+(nGoalMoney % 9);
		}
	}

	printf("%d",nCount);

	return 0;
}
int compare(const int *val1, const int *val2) {
    if ( *val1 < *val2 ) {
        return -1;
    } else if ( *val1 == * val2 ) {
        return 0;
    } else {
        return 1;
    }
}