#include <bits/stdc++.h>
#define rep(i,n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int,int>;
#define fastio ios::sync_with_stdio(0); cin.tie(0); cout.tie(0)

int six(int n){
    int x = 1;
    rep(i,n){
        x *= 6;
    }
    return x;
}

int nine(int n){
    int x = 1;
    rep(i,n){
        x *= 9;
    }
    return x;
}

int main(){
    fastio;
    int n;
    cin >> n;
    vector<int> dp(n+1);
    int m=n;
    dp[1]=1;
    for(int i=2; i<=n; ++i){
        int curl1, curl2, curl3, curl4, curl5, curl6, curl7, curl8, curl9, curl10, curl11, curl12;
        if(i>=nine(5)){
            curl1 = dp[i-nine(5)]+1;
            curl2 = dp[i-six(6)]+1;
            curl3 = dp[i-six(5)]+1; 
            curl4 = dp[i-nine(4)]+1;
            curl5 = dp[i-six(4)]+1;
            curl6 = dp[i-nine(3)]+1;
            curl7 = dp[i-six(3)]+1;
            curl8 = dp[i-nine(2)]+1;
            curl9 = dp[i-six(2)]+1;
            curl10 = dp[i-nine(1)]+1;
            curl11 = dp[i-six(1)]+1;
            curl12 = dp[i-1]+1;
            dp[i] = min(curl1, min(curl2, min(curl3, min(curl4, min(curl5, min(curl6, min(curl7, min(curl8, min(curl9, min(curl10, min(curl11, curl12)))))))))));    
        }else if(i>=six(6)){
            curl2 = dp[i-six(6)]+1;
            curl3 = dp[i-six(5)]+1; 
            curl4 = dp[i-nine(4)]+1;
            curl5 = dp[i-six(4)]+1;
            curl6 = dp[i-nine(3)]+1;
            curl7 = dp[i-six(3)]+1;
            curl8 = dp[i-nine(2)]+1;
            curl9 = dp[i-six(2)]+1;
            curl10 = dp[i-nine(1)]+1;
            curl11 = dp[i-six(1)]+1;
            curl12 = dp[i-1]+1;
            dp[i] = min(curl2, min(curl3, min(curl4, min(curl5, min(curl6, min(curl7, min(curl8, min(curl9, min(curl10, min(curl11, curl12))))))))));
        }else if(i>=six(5)){
            curl3 = dp[i-six(5)]+1; 
            curl4 = dp[i-nine(4)]+1;
            curl5 = dp[i-six(4)]+1;
            curl6 = dp[i-nine(3)]+1;
            curl7 = dp[i-six(3)]+1;
            curl8 = dp[i-nine(2)]+1;
            curl9 = dp[i-six(2)]+1;
            curl10 = dp[i-nine(1)]+1;
            curl11 = dp[i-six(1)]+1;
            curl12 = dp[i-1]+1;
            dp[i] = min(curl3, min(curl4, min(curl5, min(curl6, min(curl7, min(curl8, min(curl9, min(curl10, min(curl11, curl12)))))))));        
        }else if(i>=nine(4)){ 
            curl4 = dp[i-nine(4)]+1;
            curl5 = dp[i-six(4)]+1;
            curl6 = dp[i-nine(3)]+1;
            curl7 = dp[i-six(3)]+1;
            curl8 = dp[i-nine(2)]+1;
            curl9 = dp[i-six(2)]+1;
            curl10 = dp[i-nine(1)]+1;
            curl11 = dp[i-six(1)]+1;
            curl12 = dp[i-1]+1;
            dp[i] = min(curl4, min(curl5, min(curl6, min(curl7, min(curl8, min(curl9, min(curl10, min(curl11, curl12))))))));
        }else if(i>=six(4)){
            curl5 = dp[i-six(4)]+1;
            curl6 = dp[i-nine(3)]+1;
            curl7 = dp[i-six(3)]+1;
            curl8 = dp[i-nine(2)]+1;
            curl9 = dp[i-six(2)]+1;
            curl10 = dp[i-nine(1)]+1;
            curl11 = dp[i-six(1)]+1;
            curl12 = dp[i-1]+1;
            dp[i] = min(curl5, min(curl6, min(curl7, min(curl8, min(curl9, min(curl10, min(curl11, curl12)))))));        
        }else if(i>=nine(3)){
            curl6 = dp[i-nine(3)]+1;
            curl7 = dp[i-six(3)]+1;
            curl8 = dp[i-nine(2)]+1;
            curl9 = dp[i-six(2)]+1;
            curl10 = dp[i-nine(1)]+1;
            curl11 = dp[i-six(1)]+1;
            curl12 = dp[i-1]+1;
            dp[i] = min(curl6, min(curl7, min(curl8, min(curl9, min(curl10, min(curl11, curl12))))));
        }else if(i>=six(3)){
            curl7 = dp[i-six(3)]+1;
            curl8 = dp[i-nine(2)]+1;
            curl9 = dp[i-six(2)]+1;
            curl10 = dp[i-nine(1)]+1;
            curl11 = dp[i-six(1)]+1;
            curl12 = dp[i-1]+1;
            dp[i] = min(curl7, min(curl8, min(curl9, min(curl10, min(curl11, curl12)))));
        }else if(i>=nine(2)){
            curl8 = dp[i-nine(2)]+1;
            curl9 = dp[i-six(2)]+1;
            curl10 = dp[i-nine(1)]+1;
            curl11 = dp[i-six(1)]+1;
            curl12 = dp[i-1]+1;
            dp[i] = min(curl8, min(curl9, min(curl10, min(curl11, curl12))));
        }else if(i>=six(2)){
            curl9 = dp[i-six(2)]+1;
            curl10 = dp[i-nine(1)]+1;
            curl11 = dp[i-six(1)]+1;
            curl12 = dp[i-1]+1;
            dp[i] = min(curl9, min(curl10, min(curl11, curl12)));
        }else if(i>=nine(1)){
            curl10 = dp[i-nine(1)]+1;
            curl11 = dp[i-six(1)]+1;
            curl12 = dp[i-1]+1;
            dp[i] = min(curl10, min(curl11, curl12));
        }else if(i>=six(1)){
            curl11 = dp[i-six(1)]+1;
            curl12 = dp[i-1]+1;
            dp[i] = min(curl11, curl12);
        }else{
            dp[i]=dp[i-1]+1;
        }
    }
    cout << dp[n] << "\n";
    #ifdef TIMER_
    cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
    #endif
    return 0;
}

Measured memory_bytes: 640