#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
int main() {
	int N;
	cin >> N;
	int ans = 1 << 30;
	for (int i = 0; i <= N; i++) {
		int cnt = 0;
		int x = i;
		while (x) {
			cnt += x % 6;
			x /= 6;
		}
		x = N - i;
		while (x) {
			cnt += x % 9;
			x /= 9;
		}
		ans = min(ans, cnt);
	}
	cout << ans << endl;
}