#include<iostream>

using namespace std;

int main() {
	long n, ans;
	
	cin >> n;

	ans = n;

	for (int i = 0; i <= n; i++) {
		int x = 0;
		int k = i;

		while (k > 0) {
			x += k % 6;
			k /= 6;
		}
		k = n - i;
		while (k > 0) {
			x += k % 9;
			k /= 9;
		}
		if (ans > x) {
			ans = x;
		}
	}

	cout << ans << endl;

	return 0;
}

