#include <vector> 
#include <list> 
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>
#include <queue>
#include <complex>
using namespace std;

//conversion
//------------------------------------------
inline int toInt(string s) { int v; istringstream sin(s); sin >> v; return v; }
template<class T> inline string toString(T x) { ostringstream sout; sout << x; return sout.str(); }

//math
//-------------------------------------------
template<class T> inline T sqr(T x) { return x * x; }

//typedef
//------------------------------------------
typedef vector<int> VI;

typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef long long LL;
typedef vector<LL> VLL;

//container util
//------------------------------------------
#define ALL(a)  (a).begin(),(a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define EACH(t,c) for(typeof((c).begin()) t=(c).begin(); t!=(c).end(); ++t)
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define SORT(c) sort((c).begin(),(c).end())

//repetition
//------------------------------------------
#define FOR(t,a,b) for(int t=(a);t<(b);++t)
#define REP(t,n)  FOR(t,0,n)

//constant
//--------------------------------------------
const double EPS = 1e-10;
const double PI = acos(-1.0);

//clear memory
#define CLR(a) memset((a), 0 ,sizeof(a))

//debug
#define dump(x)  cerr << #x << '=' << (x) << endl;
#define debug(x) cerr << #x << '=' << (x) << '('<<'L' << __LINE__ << ')' << ' ' << __FILE__ << endl;



const LL mod = 1000000007;

LL gcd(LL a, LL b) { if (a%b == 0) { return b; } else return gcd(b, a%b); }
LL lcm(LL a, LL b) { return a / gcd(a, b)*b; }

#define Yes_ cout<<"Yes"<<endl; return 0;
#define No_ cout<<"No"<<endl; return 0;


#define YES_ cout<<"YES"<<endl; return 0;
#define NO_ cout<<"NO"<<endl; return 0;

#define POSSIBLE cout<<"POSSIBLE"<<endl; return 0;
#define IMPOSSIBLE cout<<"IMPOSSIBLE"<<endl; return 0;

namespace UnionFind {
	const int MAX_N = 1010101;
	int par[MAX_N];//N番目のnodeの親nodeのindex
	int Rank[MAX_N];//木の深さ

	void init(int n) {
		REP(i, n) {
			par[i] = i;//rootの親は自分に設定しておくことでrootの判別ができる
			Rank[i] = 0;
		}
	}

	//xのrootを求める
	int find(int x) {
		//root
		if (par[x] == x) {
			return x;
		}
		else {
			return par[x] = find(par[x]);//ここで親を貼り直している
		}
	}

	//木の統合
	void unite(int x, int y) {
		//x = find(x), y = find(y);//親ノードの検出
		if (x == y) { return; }//何もしない

							   //併合の際に２つの木の高さが異なれば高さの小さいものが大きい物の根へと辺をはる
		if (Rank[x] < Rank[y]) {
			par[x] = y;
			//Rank[y]のほうが深いのでRankの更新は行わなくて良い
		}
		else {
			par[y] = x;
			//２つの根が同じ深さの場合は１つの根がもうひとつの根につくわけなので深さは1増える
			if (Rank[x] == Rank[y]) { Rank[x]++; }
		}
		/*if (vact[x] > vact[y]) {
			par[y] = x;
		}
		else {
			par[x] = y;
		}*/
	}

	bool same(int x, int y) {
		return find(x) == find(y);
	}
}

VI memo;

LL dfs(LL N) {

	if (N < 6) {
		//cerr << ans+N << endl;

		return N;
	}
	if (memo[N] != -1) { return memo[N]; }
	int a9 = 1,num9=0;
	int a6 = 1,num6=0;

	while (a9 < N) { a9 *= 9; num9++; }
	while (a6 < N) { a6 *= 6; num6++; }

	if (a9 > N) { a9 /= 9; num9--; }
	if (a6 > N) { a6 /= 6; num6--; }
	LL a = 1000001;
	FOR(i, max(num9-1,1), num9+1) {
		//dump((LL)pow(9, i));
		a = min(a, dfs(N - (LL)pow(9, i)));
	}
	FOR(i, max(1,num6-1), num6+1) {
		a = min(a, dfs(N - (LL)pow(6, i)));
	}
	a++;
	memo[N] = a;
	return a;
}

int main() {
	
	LL N;
	cin >> N;
	memo.resize(N+1);
	REP(i, SZ(memo)) { memo[i] = -1; }
	int ans = 0;

	/*while (N != 0) {

		int a9 = 1;
		int a6 = 1;

		while (a9 < N) { a9 *= 9; }
		while (a6 < N) { a6 *= 6; }

		if (a9 > N) { a9 /= 9; }
		if (a6 > N) { a6 /= 6; }

		N -= max(a9, a6);
		dump(max(a9, a6));
		dump(N);
		ans++;
	
	}

	cout << ans << endl;
*/
	cout << dfs(N) << endl;

}	


Measured memory_bytes: 640