#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define fori(n) for ( int i = 0; i < (n);i++)
#define forj(n) for ( int j = 0; j < (n);j++)
#define fork(n) for ( int k = 0; k < (n);k++)
#define forghj(n) for ( int ghj = 0; ghj < (n);ghj++)
#define fori1(n) for ( int i = (n)-1; i >= 0;i--)
#define all(vector) (vector).begin(),(vector).end()
#define pii pair < int, int >
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
/*
                            |~~~~~~~|
                            |       |
                            |       |
                            |       |
                            |       |
                            |       |
 |~.\\\_\~~~~~~~~~~~~~~xx~~~         ~~~~~~~~~~~~~~~~~~~~~/_//;~|
 |  \  o \_         ,XXXXX),                         _..-~ o /  |
 |    ~~\  ~-.     XXXXX`)))),                 _.--~~   .-~~~   |
  ~~~~~~~`\   ~\~~~XXX' _/ ';))     |~~~~~~..-~     _.-~ ~~~~~~~
           `\   ~~--`_\~\, ;;;\)__.---.~~~      _.-~
             ~-.       `:;;/;; \          _..-~~
                ~-._      `''        /-~-~
                    `\              /  /
                      |         ,   | |
                       |  '        /  |
                        \/;          |
                         ;;          |
                         `;   .       |
                         |~~~-----.....|
                        | \             \
                       | /\~~--...__    |
                       (|  `\       __-\|
                       ||    \_   /~    |
                       |)     \~-'      |
                        |      | \      '
                        |      |  \    :
                         \     |  |    |
                          |    )  (    )
                           \  /;  /\  |
                           |    |/   |
                           |    |   |
                            \  .'  ||
                            |  |  | |
                            (  | |  |
                            |   \ \ |
                            || o `.)|
                            |`\\\\) |
                            |       |
                            |       |
                            |       |
*/
/*vector < int > p,z;
int f(int x) {
    if (p[x] == x) return x;
    else return p[x] = f(p[x]);
}
int kol;
int bridge(int x,int y) {
    x = f(x);
    y = f(y);
    if (x == y) return 0;
    kol--;
    if (z[x] > z[y]) {
        p[y] = x;
        z[x] += z[y];
    } else {
        p[x] = y;
        z[y] += z[x];
    }
}
int main() {
    ios::sync_with_stdio(false);
    srand(time(NULL));
    int n;
    n = rand()%100 + 100;
    int m;
    m = rand()%min(11000,n*(n-1)/2);
    cout << n << " " << m << "\n";
    vector < set < int > > a;
    a.resize(n);
    fori(m) {
        int x;
        while(true) {
            x = rand()%n;
            if (a[x].size() == n-1) continue;
            int y;
            while(true) {
                y = rand()%n;
                if (y == x) continue;
                if (a[x].find(y) != a[x].end()) continue;
                a[x].insert(y);
                break;
            }
            break;
        }
    }
    z.resize(n);
    p.resize(n);
    fori(n) z[i] = 1;
    fori(n) p[i] = i;
    vector < vector < int > > g;
    g.resize(n);
    fori(n) g[i].resize(n);
    fori(n) {
        for( auto c : a[i]) {
            g[i][c] = 1;
            g[c][i] = 1;
        }
    }
    kol = n;
    for ( int i = 0; i < n;i++) {
        for ( int j = i+1; j < n;j++) {
            if (g[i][j] == 1) {
                bridge(i,j);
            }
        }
    }
    cout << kol << " ";
    kol = n;
    fori(n) z[i] = 1;
    fori(n) p[i] = i;
    for ( int i = 0; i < n;i++) {
        for ( int j = i+1; j < n;j++) {
            if (g[i][j] == 0) {
                bridge(i,j);
            }
        }
    }
    cout << kol;
}*/
int main() {
    map < int,int > old;
    vector < int > a;
    a.pb(1);
    int kol = 6;
    while(true) {
        if (kol > 1e5) break;
        a.pb(kol);
        kol*=6;
    }
    kol = 9;
    while(true) {
        if (kol > 1e5) break;
        a.pb(kol);
        kol*=9;
    }
    int n;
    cin >> n;
    vector < int > ans;
    ans.resize(n+1);
    fori(n) ans[i] = INT_MAX;
    for (int i = n; i >= 0;i--) {
        forj(a.size()) {
            if (i-a[j] < 0) continue;
            ans[i-a[j]] = min(ans[i-a[j]],ans[i]+1);
        }
    }
    cout << ans[0];

}



Measured memory_bytes: 640