
#include <bits/stdc++.h>
#define MAX 2000000
using namespace std;

vector <int> arr;
int n;
long long dp[MAX];

long long minOper(int falta){

    if(falta < 0) return 100000000000000LL;
    if(falta < 6) return dp[falta] = falta;

    if(dp[falta] != -1) return dp[falta];

    long long prov = 100000000000000LL;
    for(int l = 0; l < arr.size(); l++){

        prov = min(prov, 1 + minOper(falta - arr[l]));
    }
    return dp[falta] = prov;
}

int main(){

    cin>>n;

    if(n < 6){

        cout<<n<<endl;
        return 0;
    }

    memset(dp, -1, sizeof dp);

    int pot = 1;
    while(pot * 6 < MAX){

        arr.push_back(pot * 6);
        pot *= 6;
    }
    pot = 1;
    while(pot * 9 < MAX){

        arr.push_back(pot * 9);
        pot *= 9;
    }
    sort(arr.begin(), arr.end());

    cout<<minOper(0, n)<<endl;

    return 0;
}
