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.

Examples

Input

127


Output

4


Input

3


Output

3


Input

44852


Output

16
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;

class Main{

	static int ans = Integer.MAX_VALUE;

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);			//文字の入力
		int n = sc.nextInt();
		ArrayList<Integer> data = new ArrayList<Integer>();
		data.add(1);
		for(int i = 1;i < 9;i++){
			data.add((int)(Math.pow(6, i)));
		}
		for(int i = 1;i < 9;i++){
			data.add((int)(Math.pow(9, i)));
		}
		Collections.sort(data);
//		for(int i = 0;i < data.size();i++){
//			System.out.println(data.get(i));
//		}
		int p = 0;
		while(n >= data.get(p)){
			p++;
		}
		p--;
//		System.out.println("p "+p+" data "+data.get(p));
		dfs(n,p,0,data);
		System.out.println(ans);
		
	}
	static void dfs(int n,int p,int count,ArrayList<Integer> data){
	//	System.out.println(n);
		if(n < 0 || count >= ans){
			return;
		}
		if(n == 0){
			ans = Math.min(ans, count);
			return;
		}
		for(int i = p;i >= 0;i--){
			if(n-data.get(i) >= 0){
			dfs(n-data.get(i), i, count+1, data);
			}
		}
		
		
		
		
	}

}

class Pair implements Comparable{
	int from;		//p
	int end;		//y
	int num;
	int bango;
	@Override
	public int compareTo(Object other) {
		Pair otherpair = (Pair)other;

		return from - otherpair.from;
	}
}

