// Original C++ solution (slower):
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

typedef long double ld;

typedef pair<ll, ll> l_l;

typedef pair<int , int> i_i;

typedef vector<ll> vel;

typedef vector<int> vei;

typedef vector<char> vec;

typedef vector<bool> veb;

typedef vector<string> ves;

typedef vector<vector<ll>> ve_vel;

typedef vector<vector<int>> ve_vei;

typedef vector<vector<char>> ve_vec;

typedef vector<vector<bool>> ve_veb;

typedef vector<vector<string>> ve_ves;

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

#define rep1(i,n) for(int i=1;i<(int)(n);i++)

#define rep2(i,n) for(int i=2;i<(int)(n);i++)

#define repk(i,k,n) for(int i=k;i<(int)(n);i++)

#define fs first

#define sc second

#define pub push_back

#define pob pop_back

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

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

#define maxel(a) *max_element(all(a))

#define minel(a) *min_element(all(a))

#define acc accumulate

#define EPS (1e-7)

#define INF (1e9)

#define PI (acos(-1))

#define mod (1000000007)

template<class T> bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }

template<class T> bool chmin(T &a, const T &b) { if (a>b) { a=b; return 1; } return 0; }



int beki(int a, int n)

{

    if(n == 0){

        return 1;

    }

    int ans = beki(a, n / 2);

    ans = ans * ans;

    if(n % 2 == 1){

        ans = ans * a;

    }

    return ans;

}



int main(){

	cin.tie(0);

	ios::sync_with_stdio(false);



    int N; cin >> N;

    vel dp(1000005);//dp[i] = x はi円引き出す操作の最小値がx回であること





    dp[0] = 0;

    rep1(i, N+1) {

        dp[i] = dp[i - 1] + 1;//一円が最小の時

        rep1(j,8) {

            if(i - pow(6, j) >= 0) {

                dp[i] = min(dp[i], dp[i - beki(6,j)] + 1);//6のべき乗円が最小の時

            }

            if(i - pow(9, j) >= 0) {

                dp[i] = min(dp[i], dp[i - beki(9, j)] + 1);//9のべき乗円が最小の時

            }

        }

    }

    cout << dp[N] << endl;

    return 0;

}

// Optimized solution to the same problem (faster):
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

typedef long double ld;

typedef pair<ll, ll> l_l;

typedef pair<int , int> i_i;

typedef vector<ll> vel;

typedef vector<int> vei;

typedef vector<char> vec;

typedef vector<bool> veb;

typedef vector<string> ves;

typedef vector<vector<ll>> ve_vel;

typedef vector<vector<int>> ve_vei;

typedef vector<vector<char>> ve_vec;

typedef vector<vector<bool>> ve_veb;

typedef vector<vector<string>> ve_ves;

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

#define rep1(i,n) for(int i=1;i<(int)(n);i++)

#define rep2(i,n) for(int i=2;i<(int)(n);i++)

#define repk(i,k,n) for(int i=k;i<(int)(n);i++)

#define fs first

#define sc second

#define pub push_back

#define pob pop_back

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

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

#define maxel(a) *max_element(all(a))

#define minel(a) *min_element(all(a))

#define acc accumulate

#define EPS (1e-7)

#define INF (1e9)

#define PI (acos(-1))

#define mod (1000000007)

template<class T> bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }

template<class T> bool chmin(T &a, const T &b) { if (a>b) { a=b; return 1; } return 0; }



int beki(int a, int n)

{

    if(n == 0){

        return 1;

    }

    int ans = beki(a, n / 2);

    ans = ans * ans;

    if(n % 2 == 1){

        ans = ans * a;

    }

    return ans;

}



int main(){

	cin.tie(0);

	ios::sync_with_stdio(false);



    int N; cin >> N;

    vel dp(100005);//dp[i] = x はi円引き出す操作の最小値がx回であること





    dp[0] = 0;

    rep1(i, N+1) {

        dp[i] = dp[i - 1] + 1;//一円が最小の時

        rep1(j,8) {

            if(i - beki(6, j) >= 0) {

                dp[i] = min(dp[i], dp[i - beki(6,j)] + 1);//6のべき乗円が最小の時

            }

            if(i - beki(9, j) >= 0) {

                dp[i] = min(dp[i], dp[i - beki(9, j)] + 1);//9のべき乗円が最小の時

            }

        }

    }

    cout << dp[N] << endl;

    return 0;

}

// Fastest known solution for this problem: