// 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: 1.1865815161541506x):
#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();



    vvl dp(n+1, vl(W+1, INF));  // i番目までで重さがjとなるときの価値の総和の最小値

    rep(i, n+1) {

        dp[i][0] = 0;

    }



    rep(i, n) {

        rep(j, W+1) {

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

            if(j >= weight[i]) {

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

            }

        }

    }



    out(dp[n][W]);

    re0;

}