// Slower C++ solution:
#include <iostream>

#include <cstdio>

#include <string>

#include <algorithm>

#include <utility>

#include <cmath>

#include <vector>

#include <queue>

#include <set>

#include <map>

#include <functional>

using namespace std;

typedef long long ll;

typedef vector<ll> vl;

typedef vector<vector<ll>> vvl;

typedef pair<ll, ll> P;

typedef vector<vector<P>> vvP;

#define rep(i, n) for(ll i = 0; i < n; i++)

#define exrep(i, a, b) for(ll i = a; i <= b; i++)

#define out(x) cout << x << endl

#define exout(x) printf("%.10f\n", x)

#define chmax(x, y) x = max(x, y)

#define chmin(x, y) x = min(x, y)

#define all(a) a.begin(), a.end()

#define rall(a) a.rbegin(), a.rend()

#define pb push_back

#define re0 return 0

const ll mod = 1000000007;

const ll INF = 1e16;

const ll MAX_N = 100010;



int main() {

    ll n;

    cin >> n;



    // ダイクストラ法。計算量はO((V+E)*log(V))

    const ll sv = 0;  // 始点となる頂点。デフォルトでは0

    vl dist(MAX_N, INF);  // dist[v] : 頂点svから頂点vへの最短距離

    priority_queue<P, vector<P>, greater<P>> prQ;

    dist[sv] = 0;

    prQ.push(make_pair(0, sv));  // (頂点svからの最短距離, 頂点番号)

    while(!prQ.empty()) {

        auto p1 = prQ.top();

        prQ.pop();

        ll v = p1.second;

        if(dist[v] < p1.first) {

            continue;

        }

        for(ll pow6 = 1; v + pow6 <= n; pow6 *= 6) {

            ll u = v + pow6;

            ll cost = 1;

            if(dist[u] > dist[v] + cost) {

                dist[u] = dist[v] + cost;

                prQ.push(make_pair(dist[u], u));

            }

        }

        for(ll pow9 = 1; v + pow9 <= n; pow9 *= 9) {

            ll u = v + pow9;

            ll cost = 1;

            if(dist[u] > dist[v] + cost) {

                dist[u] = dist[v] + cost;

                prQ.push(make_pair(dist[u], u));

            }

        }

    }



    out(dist[n]);

    re0;

}

// Faster C++ solution for the same problem (measured speedup: 2.205011615208086x):
#include <iostream>

#include <cstdio>

#include <string>

#include <algorithm>

#include <utility>

#include <cmath>

#include <vector>

#include <queue>

#include <set>

#include <map>

#include <functional>

using namespace std;

typedef long long ll;

typedef vector<ll> vl;

typedef vector<vector<ll>> vvl;

typedef pair<ll, ll> P;

typedef vector<vector<P>> vvP;

#define rep(i, n) for(ll i = 0; i < n; i++)

#define exrep(i, a, b) for(ll i = a; i <= b; i++)

#define out(x) cout << x << endl

#define exout(x) printf("%.10f\n", x)

#define chmax(x, y) x = max(x, y)

#define chmin(x, y) x = min(x, y)

#define all(a) a.begin(), a.end()

#define rall(a) a.rbegin(), a.rend()

#define pb push_back

#define re0 return 0

const ll mod = 1000000007;

const ll INF = 1e16;

const ll MAX_N = 100010;



int main() {

    ll n;

    cin >> n;



    // BFS。計算量はO(V+E)

    vl dist(n+1, -1);  // dist[v] : 頂点svから頂点vへの最短距離(-1のとき未訪問)

    queue<ll> Q;

    const ll sv = 0;  // BFSを始める頂点(デフォルトは0)

    dist[sv] = 0;

    Q.push(sv);

    while(!Q.empty()) {

        ll v = Q.front();

        Q.pop();

        for(ll pow6 = 1; v + pow6 <= n; pow6 *= 6) {

            ll u = v + pow6;

            if(dist[u] == -1) {

                dist[u] = dist[v] + 1;

                Q.push(u);   

            }

        }

        for(ll pow9 = 1; v + pow9 <= n; pow9 *= 9) {

            ll u = v + pow9;

            if(dist[u] == -1) {

                dist[u] = dist[v] + 1;

                Q.push(u);   

            }

        }

    }



    out(dist[n]);

    re0;

}