// Slower C++ solution:
#include <bits/stdc++.h>

using namespace std;



typedef long long   signed int LL;

typedef long long unsigned int LU;



#define incID(i, l, r) for(int i = (l)    ; i <  (r); i++)

#define incII(i, l, r) for(int i = (l)    ; i <= (r); i++)

#define decID(i, l, r) for(int i = (r) - 1; i >= (l); i--)

#define decII(i, l, r) for(int i = (r)    ; i >= (l); i--)

#define  inc(i, n) incID(i, 0, n)

#define inc1(i, n) incII(i, 1, n)

#define  dec(i, n) decID(i, 0, n)

#define dec1(i, n) decII(i, 1, n)



#define inII(v, l, r) ((l) <= (v) && (v) <= (r))

#define inID(v, l, r) ((l) <= (v) && (v) <  (r))



#define PB push_back

#define EB emplace_back

#define MP make_pair

#define FI first

#define SE second

#define PQ priority_queue



#define  ALL(v)  v.begin(),  v.end()

#define RALL(v) v.rbegin(), v.rend()

#define  FOR(it, v) for(auto it =  v.begin(); it !=  v.end(); ++it)

#define RFOR(it, v) for(auto it = v.rbegin(); it != v.rend(); ++it)



template<typename T> bool   setmin(T & a, T b) { if(b <  a) { a = b; return true; } else { return false; } }

template<typename T> bool   setmax(T & a, T b) { if(b >  a) { a = b; return true; } else { return false; } }

template<typename T> bool setmineq(T & a, T b) { if(b <= a) { a = b; return true; } else { return false; } }

template<typename T> bool setmaxeq(T & a, T b) { if(b >= a) { a = b; return true; } else { return false; } }

template<typename T> T gcd(T a, T b) { return (b == 0 ? a : gcd(b, a % b)); }

template<typename T> T lcm(T a, T b) { return a / gcd(a, b) * b; }



// ---- ----



template<typename T> class RPQ : public PQ<T, vector<T>, greater<T>> { };

template<typename Cost> void dijkstra(int start, vector<pair<int, Cost>> g[], Cost dist[]) {

	RPQ<pair<Cost, int>> q;

	dist[start] = 0;

	q.emplace(dist[start], start);

	while(! q.empty()) {

		auto cp = q.top(); q.pop();

		int  p = cp.SE;

		Cost c = cp.FI;

		if(dist[p] != c) { continue; }

		for(auto && e : g[p]) {

			int  ep = e.FI;

			Cost ec = e.SE;

			if(setmin(dist[ep], dist[p] + ec)) { q.emplace(dist[ep], ep); }

		}

	}

	return;

}



int n, d[100001];

vector<pair<int, int>> g[100001];



int main() {

	cin >> n;

	

	incII(i, 0, n) { d[i] = n + 1; }

	

	inc(i, n) {

		g[i].EB(i + 1, 1);

		int p;

		p = 1; inc(j, 10) { p *= 6; if(i + p > n) { break; } g[i].EB(i + p, 1); }

		p = 1; inc(j, 10) { p *= 9; if(i + p > n) { break; } g[i].EB(i + p, 1); }

	}

	

	dijkstra(0, g, d);

	

	cout << d[n] << endl;

	

	// incII(i, 0, n) { cout << i << ": " << d[i] << endl; }

	// for(auto && e: g[0]) { cout << e.FI << endl; }

	

	return 0;

}


// Faster C++ solution for the same problem (measured speedup: 4.307620879709203x):
#include <bits/stdc++.h>

using namespace std;



typedef long long   signed int LL;

typedef long long unsigned int LU;



#define incID(i, l, r) for(int i = (l)    ; i <  (r); i++)

#define incII(i, l, r) for(int i = (l)    ; i <= (r); i++)

#define decID(i, l, r) for(int i = (r) - 1; i >= (l); i--)

#define decII(i, l, r) for(int i = (r)    ; i >= (l); i--)

#define  inc(i, n) incID(i, 0, n)

#define inc1(i, n) incII(i, 1, n)

#define  dec(i, n) decID(i, 0, n)

#define dec1(i, n) decII(i, 1, n)



#define inII(v, l, r) ((l) <= (v) && (v) <= (r))

#define inID(v, l, r) ((l) <= (v) && (v) <  (r))



#define PB push_back

#define EB emplace_back

#define MP make_pair

#define FI first

#define SE second

#define PQ priority_queue



#define  ALL(v)  v.begin(),  v.end()

#define RALL(v) v.rbegin(), v.rend()

#define  FOR(it, v) for(auto it =  v.begin(); it !=  v.end(); ++it)

#define RFOR(it, v) for(auto it = v.rbegin(); it != v.rend(); ++it)



template<typename T> bool   setmin(T & a, T b) { if(b <  a) { a = b; return true; } else { return false; } }

template<typename T> bool   setmax(T & a, T b) { if(b >  a) { a = b; return true; } else { return false; } }

template<typename T> bool setmineq(T & a, T b) { if(b <= a) { a = b; return true; } else { return false; } }

template<typename T> bool setmaxeq(T & a, T b) { if(b >= a) { a = b; return true; } else { return false; } }

template<typename T> T gcd(T a, T b) { return (b == 0 ? a : gcd(b, a % b)); }

template<typename T> T lcm(T a, T b) { return a / gcd(a, b) * b; }



// ---- ----



int n, dp[100001];



int main() {

	cin >> n;

	

	incII(i, 0, n) { dp[i] = n + 1; }

	

	dp[0] = 0;

	inc1(i, n) {

		int p;

		p = 1; while(i - p >= 0) { setmin(dp[i], dp[i - p] + 1); p *= 6; }

		p = 1; while(i - p >= 0) { setmin(dp[i], dp[i - p] + 1); p *= 9; }

	}

	

	cout << dp[n] << endl;

	

	return 0;

}