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.*;
import java.io.*;
import java.nio.charset.StandardCharsets;

public class Main {
	int N;
	List<Integer> list;
	int result = 0;

	public static void main(String[] args) throws IOException {
		InputStreamReader reader = new InputStreamReader(System.in, StandardCharsets.UTF_8);
		BufferedReader in = new BufferedReader(reader);
		Main ins = new Main(in);
		ins.calc();
		ins.showResult();
	}

	Main(BufferedReader in) throws IOException {
		N = Integer.parseInt(in.readLine());
		this.list = new ArrayList<>();
		list.add(1);
		for (int i = 6; i <= N; i *= 6) {
			list.add(i);
		}
		for (int i = 9; i <= N; i *= 9) {
			list.add(i);
		}
		Collections.sort(list, new Comparator<Integer>() {
			@Override
			public int compare(Integer o1, Integer o2) {
				return -1 * Integer.compare(o1, o2);
			}
		});
	}

	void calc() {
		result = N;
		for (int i = 0; i < 6; ++i) {
			result = Math.min(result, i + getTimes(N - i, list));
		}
	}

	int getTimes(int val, List<Integer> list) {
		if (list.size() == 1) {
			// 最後の要素は1のはず
			return val;
		}
		int top = list.get(0);
		if (top > val) {
			return getTimes(val, list.subList(1, list.size()));
		}
		int result = val / top + getTimes(val % top, list.subList(1, list.size()));
		if (val >= top * 2) {
			result = Math.min(result, val / top - 1 + getTimes(val % top + top, list.subList(1, list.size())));
		}
		result = Math.min(result, getTimes(val, list.subList(1, list.size())));
		return result;
	}

	void showResult() {
		System.out.println(result);
	}
}
