// 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.2947498697875623x):
#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 W;

    cin >> W;



    vl weight;

    weight.pb(1);

    for(ll i = 6; i <= W; i *= 6) {

        weight.pb(i);

    }

    for(ll i = 9; i <= W; i *= 9) {

        weight.pb(i);

    }



    ll n = weight.size();



    vl dp(W+1, INF);  // 重さがjとなるときの価値の総和の最小値

    dp[0] = 0;

    rep(i, n) {

        for(ll j = weight[i]; j <= W; j++) {

            chmin(dp[j], dp[j - weight[i]] + 1);

        }

    }

    

    out(dp[W]);

    re0;

}