stablegradients/pie-gem5-pairs huggingface dataset:
// Slower C++ solution:
#include <bits/stdc++.h>

typedef long long ll;

#define rep(i,a,b) for(ll i=a;i<(b);++i)

#define erep(i,a,b) for(ll i=a;i<=(int)(b);++i)

#define per(i,a,b) for(int i=(b);i>(a);--i)

#define eper(i,a,b) for(int i=((int)(a));i>=b;--i)

#define pb push_back

#define mp make_pair

#define INF 2000000000

#define MOD 1000000007

using namespace std;

typedef pair<int,int> P;

int dy[]={0, 0, 1, -1};

int dx[]={1, -1, 0, 0};

int gcd(int a,int b){return b?gcd(b,a%b):a;}

int lcm(int a,int b){return a/gcd(a, b)*b;}



int n;

ll dp[600000], val[] = {1, 6, 9, 36, 81, 216, 729, 1296, 6561, 7776, 46656, 59049};



ll solve(ll x) {

    if (dp[x] >= 0) return dp[x];

    ll t = INF;

    for (ll k : val) {

        if (x < k) break;

        t = min(solve(x-k) + 1, t);

    }

    return dp[x] = t;

}

int main() {

 ios::sync_with_stdio ( false );

 cin.tie ( 0 );

    cin >> n;

    memset(dp, -1, sizeof(dp));

    dp[0] = 0;

    cout << solve(n) << endl;

    return 0;

}


// Faster C++ solution for the same problem (measured speedup: 3.3972724645707637x):
#include <bits/stdc++.h>

#define rep(i,a,b) for(int i=a;i<(b);++i)

#define erep(i,a,b) for(int i=a;i<=(int)(b);++i)

#define per(i,a,b) for(int i=(a);i>(b);--i)

#define eper(i,a,b) for(int i=(a);i>=b;--i)

#define fore(x, a) for(auto &x : a)

#define pb push_back

#define mp make_pair

#define INF 100100100100

#define inf 2001001001

#define MOD 1000000007

#define ALL(x) begin(x),end(x)

using namespace std;

using ll = long long;

using pii = pair<int,int>;

using vii = vector<int>;

using vll = vector<ll>;

template<class T>using vv = vector<T>;

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 (b<a) { a = b; return 1; } return 0; }

template<class T>void show(const vector<T> &a) { for (int i = 0; i < (int)a.size(); ++i) cout << a[i] << (i != (int)a.size()-1 ? " " : "\n"); }

template<class T>T gcd(T a, T b){ return b ? gcd(b, a % b) : a; }

template<class T>T lcm(T a, T b){ return a / gcd(a, b) * b; }

int dy[]={0, 1, -1, 0};

int dx[]={1, 0, 0, -1};

int n;

int dp[100005];



int rec(int p) {

  if (p == 0) return 0;

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

  int res = inf;

  for (int i = 1; i <= p; i *= 6) res = min(res, rec(p - i) + 1);

  for (int i = 1; i <= p; i *= 9) res = min(res, rec(p - i) + 1);

  return dp[p] = res;

}



int main() {

  cin.tie(0);

  ios::sync_with_stdio(false);

    cin >> n;

    memset(dp, -1, sizeof(dp));

    cout << rec(n) << endl;

    return 0;

}
// Slower C++ solution:
#include <bits/stdc++.h>

using namespace std;



const int MAX_N = 110000;

int memo[MAX_N];



int rec(int n){

    if (n==0) return 0;

    if (memo[n] != -1) return memo[n];



    int res = n;



    for (int pow6=1;pow6<=n;pow6*=6) res = min(res, rec(n-pow6)+1);

    for (int pow9=1;pow9<=n;pow9*=9) res = min(res, rec(n-pow9)+1);



    return memo[n] = res;

}

int main() {

    int N; cin >> N;



    for (int i=0;i<MAX_N; ++i) memo[i] = -1;

    cout << rec(N) << endl;

    return 0;

}

// Faster C++ solution for the same problem (measured speedup: 1.6300966589520578x):
#include <bits/stdc++.h>

using namespace std;



int func(int n, int a){

    int res = 0;

    while(n>0){

        res += n % a;

        n /= a;

    }

    return res;

}

int main() {

    int N; cin >> N;

    int res = N;

    for (int i=0;i<=N;++i){

        int tmp = func(i,6) + func(N-i, 9);

        res = min(res, tmp);

    }   

    cout << res << endl;

    return 0;

}
// Slower C++ solution:
#include <bits/stdc++.h>

using namespace std;



const int MAX_N = 110000;

int dp[MAX_N];



int main() {

    int N; cin >> N;



    for (int i=0;i<MAX_N;++i) dp[i] = N;

    dp[0] = 0;

    

    for (int n=1;n<=N;++n){

        for (int pow6=1;pow6<=n;pow6*=6) dp[n] = min(dp[n], dp[n-pow6]+1);

        for (int pow9=1;pow9<=n;pow9*=9) dp[n] = min(dp[n], dp[n-pow9]+1);

    }



    cout << dp[N] << endl;

    return 0;

}

// Faster C++ solution for the same problem (measured speedup: 1.2763877112627922x):
#include <bits/stdc++.h>

using namespace std;



int func(int n, int a){

    int res = 0;

    while(n>0){

        res += n % a;

        n /= a;

    }

    return res;

}

int main() {

    int N; cin >> N;

    int res = N;

    for (int i=0;i<=N;++i){

        int tmp = func(i,6) + func(N-i, 9);

        res = min(res, tmp);

    }   

    cout << res << endl;

    return 0;

}
// Slower C++ solution:
#include <bits/stdc++.h>

using namespace std;

using pi = pair<int, int>;

template <class T> bool chmin(T &a, T b) { if (a > b) {a = b; return true; } return false; }



const int MAX_N = 110000;

int dp[MAX_N]; 



int main() {

    int N;  cin >> N;



    for (int i = 0; i < MAX_N; ++i) dp[i] = N; 

    dp[0] = 0;

    priority_queue<pi, vector<pi>, greater<pi>> que;

    que.push(make_pair(0, 0));



    while (!que.empty()) {

        int cur = que.top().first; // 現在の距離

        int v = que.top().second; // 現在のノード

        que.pop();



        if (cur > dp[v]) continue;



        for (int pow6 = 1; v+pow6 <= N; pow6 *= 6) {

            int nv = v + pow6; 

            if (chmin(dp[nv], dp[v] + 1)) que.push(make_pair(dp[nv], nv));

        }

        for (int pow9 = 1; v + pow9 <= N; pow9 *= 9) {

            int nv = v + pow9; 

            if (chmin(dp[nv], dp[v] + 1)) que.push(make_pair(dp[nv], nv));

        }

    }



    cout << dp[N] << endl;

}

// Faster C++ solution for the same problem (measured speedup: 1.8412313459525453x):
#include <bits/stdc++.h>

using namespace std;



int func(int n, int a){

    int res = 0;

    while(n>0){

        res += n % a;

        n /= a;

    }

    return res;

}

int main() {

    int N; cin >> N;

    int res = N;

    for (int i=0;i<=N;++i){

        int tmp = func(i,6) + func(N-i, 9);

        res = min(res, tmp);

    }   

    cout << res << endl;

    return 0;

}
// Slower C++ solution:
// 個数制限なしナップサックDP

#include <bits/stdc++.h>

using namespace std;

template <class T> bool chmin(T &a, T b) { if (a > b) {a = b; return true; } return false; }



const int MAX_N = 15;

const int MAX_W = 110000;

int dp[MAX_N][MAX_W];



int main() {

    int N; cin >> N;

    vector<int> weight;

    for (int i=1;i<=N;i*=6) weight.push_back(i);

    for (int i=9;i<=N;i*=9) weight.push_back(i);

    int n = (int)weight.size();



    for (int i=0;i < MAX_N; ++i) for (int w=0;w<MAX_W;++w) dp[i][w] = 1<<29;

    for (int i=0;i<=n;++i) dp[i][0] = 0;



    for (int i=0;i<n;++i){

        for (int w=0;w<=N;++w){

            if (w>=weight[i]) chmin(dp[i+1][w], dp[i+1][w-weight[i]]+1);

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

        }

    }



    cout << dp[n][N] << endl;

}

// Faster C++ solution for the same problem (measured speedup: 6.982360691347142x):
#include <bits/stdc++.h>

using namespace std;



int func(int n, int a){

    int res = 0;

    while(n>0){

        res += n % a;

        n /= a;

    }

    return res;

}

int main() {

    int N; cin >> N;

    int res = N;

    for (int i=0;i<=N;++i){

        int tmp = func(i,6) + func(N-i, 9);

        res = min(res, tmp);

    }   

    cout << res << endl;

    return 0;

}
// Slower C++ solution:
#include<bits/stdc++.h>

using namespace std;



const long long INF = 1e9;



// 入力

int N;



// DP テーブル

long long dp[100010];



int main() {

    cin >> N;



    // 初期化 (最小化問題なので INF に初期化)

    for (int i = 0; i < 100010; ++i) dp[i] = INF;



    // 初期条件

    dp[0] = 0;



    // ループ

    for (int i = 0; i < N; ++i) {

        dp[i+1] = min(dp[i]+1, dp[i+1]);

        int six = 6;

        while(i + six <= N){

        	dp[i+six] = min(dp[i]+1, dp[i+six]);

        	six *= 6;

        }

        int nine = 9;

        while(i + nine <= N){

        	dp[i+nine] = min(dp[i]+1, dp[i+nine]);

        	nine *= 9;

        }

    }



    // 答え

    cout << dp[N] << endl;

    return 0;

}

// Faster C++ solution for the same problem (measured speedup: 1.5723249057693847x):
#include "bits/stdc++.h"

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

using namespace std;

typedef long long int ll;

typedef pair<int, int> P;

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

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



const ll INF = 1e9;



int dp[100005];



int main(){

    cin.tie(0);

    ios::sync_with_stdio(false);

    int n;

    cin >> n;

    vector<ll> v;

    ll six = 6, nine = 9;

    v.push_back(1);

    rep(i,15){

        v.push_back(six);

        v.push_back(nine);

        six *= 6;

        nine *= 9;

    }

    sort(v.begin(), v.end());

    rep(j,n+1) dp[j] = INF;

    dp[0] = 0;

    rep(i,20){

        rep(j,n+1){

            if(j+v[i] <= n) chmin(dp[j+v[i]], dp[j] + 1);

        }

    }

    cout << dp[n] << endl;

    return 0;

}
// Slower C++ solution:
#include <bits/stdc++.h>



using namespace std;



using ll = long long;

using P = pair<ll, ll>;

using edge = pair<ll, P>;

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

#define rep2(i, m, n) for(ll i=m;i<(ll)(n);i++)

#define rrep(i, n, m) for(ll i=n;i>=(ll)(m);i--)

using Graph = vector<vector<ll>>;

const int dx[4] = {1, 0, -1, 0};

const int dy[4] = {0, 1, 0, -1};

const int ddx[8] = {0, 1, 1, 1, 0, -1, -1, -1};

const int ddy[8] = {1, 1, 0, -1, -1, -1, 0, 1};

const ll MOD = 1000000007;

const ll INF = 1000000000000000000L;

#ifdef __DEBUG



#include "cpp-pyprint/pyprint.h"



#endif



ll dp[100005];



void Main() {

    int N;

    cin >> N;

    rep(i, 100005) dp[i] = INF;

    dp[0] = 0;

    rep(i, N) {

        dp[i + 1] = min(dp[i] + 1, dp[i + 1]);

        ll j = 1;

        while (i + (int) pow(6, j) < N + 1) {

            dp[i + (int) pow(6, j)] = min(dp[i +  (int) pow(6, j)], dp[i] + 1);

            j++;

        }

        j = 1;

        while (i + (int) pow(9, j) < N + 1) {

            dp[i + (int) pow(9, j)] = min(dp[i +  (int) pow(9, j)], dp[i] + 1);

            j++;

        }

    }

    cout << dp[N] << endl;

}



int main() {

    cin.tie(0);

    ios::sync_with_stdio(false);

    cout << fixed << setprecision(15);

    Main();

}


// Faster C++ solution for the same problem (measured speedup: 2.852966294917494x):
#include <bits/stdc++.h>



using namespace std;



using ll = long long;

using P = pair<ll, ll>;

using Graph = vector<vector<ll>>;

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

#define rep2(i, m, n) for(ll i=m;i<(ll)(n);i++)

#define rrep(i, n, m) for(ll i=n;i>=(ll)(m);i--)

const int dx[4] = {1, 0, -1, 0};

const int dy[4] = {0, 1, 0, -1};

const int ddx[8] = {0, 1, 1, 1, 0, -1, -1, -1};

const int ddy[8] = {1, 1, 0, -1, -1, -1, 0, 1};

const ll MOD = 1000000007;

const ll INF = 1000000000000000000L;

#ifdef __DEBUG



/**

 * For DEBUG

 * https://github.com/ta7uw/cpp-pyprint

 */

#include "cpp-pyprint/pyprint.h"



#endif





void Main() {

    int N;

    cin >> N;



    vector<int> dp(N + 1, MOD);



    dp[0] = 0;

    rep(i, N) {

        dp[i + 1] = min(dp[i + 1], dp[i] + 1);

        for (int j = 1; j + i <= N; j *= 6) {

            dp[i + j] = min(dp[i + j], dp[i] + 1);

        }

        for (int j = 1; j + i <= N; j *= 9) {

            dp[i + j] = min(dp[i + j], dp[i] + 1);

        }

    }

    cout << dp[N] << '\n';

}



int main() {

    cin.tie(nullptr);

    ios::sync_with_stdio(false);

    cout << fixed << setprecision(15);

    Main();

    return 0;

}
// Slower C++ solution:
#include <cstdio>

#include <string>

#include <cmath>

#include <string>

#include <algorithm>

#include <iostream>

#include <map>

#include <vector>

#include <iomanip>

#include <tuple>

#include <queue>

#include <numeric>

using namespace std;

typedef long long ll;

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

#define repeq(i, a, n) for(int i=a;i<=n;++i)

#define repi(m, itr) for (auto itr = m.begin();itr != m.end();++itr)

#define per(i, a, n) for (int i=n-1; i>=a;--i)

#define pb push_back

#define mp make_pair

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



// C - Strange Bank

// メモ化再帰

int N;

int ans = 200000;

int memo[101000];

const int upb = 200000;



int solve(int n){

    int res = 200000;

    if(n == 0) return 0;

    if(memo[n] != upb) return memo[n];



    rep(i, 0, 7){

        if(n >= (int)pow(6, i)){

            res = min(res, solve(n - pow(6, i)) + 1);

            memo[n] = res;

        }

    }

    rep(i, 0, 7){

        if(n >= (int)pow(9, i)){

            res = min(res, solve(n - pow(9, i)) + 1);

            memo[n] = res;

        }

    }

    return res;

}



int main(){

    cin >> N;

    rep(i, 0, 101000){

        memo[i] = upb;

    }

    ans = solve(N);

    cout << ans << endl;

    return 0;

}






// Faster C++ solution for the same problem (measured speedup: 1.514753427232374x):
#include <cstdio>

#include <string>

#include <cmath>

#include <string>

#include <algorithm>

#include <iostream>

#include <map>

#include <vector>

#include <iomanip>

#include <tuple>

#include <queue>

#include <numeric>

using namespace std;

typedef long long ll;

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

#define repeq(i, a, n) for(int i=a;i<=n;++i)

#define repi(m, itr) for (auto itr = m.begin();itr != m.end();++itr)

#define per(i, a, n) for (int i=n-1; i>=a;--i)

#define pb push_back

#define mp make_pair

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



// C - Strange Bank

// 貰うDP

int N;

int ans = 200000;

int dp[101000];

const int upb = 200000;



int main(){

    cin >> N;

    dp[0] = 0;

    rep(i, 1, N+1){

        dp[i] = upb;

        rep(j, 0, 7){

            int tmp = i-(int)pow(6,j);

            if(tmp>=0){

                dp[i] = min(dp[i], dp[tmp]+1);

            }

        }

        rep(j, 0, 7){

            int tmp = i-(int)pow(9,j);

            if(tmp>=0){

                dp[i] = min(dp[i], dp[tmp]+1);

            }

        }

    }

    ans = dp[N];

    cout << ans << endl;

    return 0;

}
// Slower C++ solution:
#include <bits/stdc++.h>

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

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

typedef long long ll;

const int INF = 1<<29;

//const int MODINF = 1000000007;

using namespace std;



int main(){

    int N;

    cin >> N;

    int i = 0, j = 0;

    while(N >= pow(6, i + 1)) i++;

    while(N >= pow(9, j + 1)) j++;

    vector<int> dp(100010, INF);

    dp.at(N) = 0;

    for (int a = N; a >= 0; a--){

        for (int b = 0; b <= i; b++){

            if (a >= pow(6, b)){

                dp.at(a - pow(6, b)) = min(dp.at(a) + 1, dp.at(a - pow(6, b)));

                //cout << "B " << a << " " << pow(6, b) << endl;

            }

        }

        for (int c= 0; c <= i; c++){

            if (a >= pow(9, c)){

                dp.at(a - pow(9, c)) = min(dp.at(a) + 1, dp.at(a - pow(9, c)));

                //cout << "C " << a << " " << pow(9, b) << endl;

            }

        }

    }

    cout << dp.at(0) << endl;

}

// Faster C++ solution for the same problem (measured speedup: 2.4970831206976642x):
#include <bits/stdc++.h>

#define rep(i, z, n) for(int64_t i = z; i < n; i++)

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

typedef long long ll;

#define PI 3.14159265358979

//const int64_t INF = 1LL << 60;

const int INF = 1 << 29;

//const int64_t MOD = 1000000007;

//const int64_t INF = 998244353;

using namespace std;

typedef pair<int64_t, int64_t> P;



int main(){

    int N;

    cin >> N;

    vector<int> dp(N + 100, INF);

    dp.at(0) = 0;

    rep(n, 1, N + 1){

        for(int pow6 = 1; pow6 <= n; pow6 *= 6){

            dp.at(n) = min(dp.at(n), dp.at(n - pow6) + 1);

        }

        for(int pow9 = 1; pow9 <= n; pow9 *= 9){

            dp.at(n) = min(dp.at(n), dp.at(n - pow9) + 1);

        }

    }

    cout << dp.at(N) << endl;

}
// Slower C++ solution:
#include <bits/stdc++.h>

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

#define REPR(i, n) for(int i = n;i >= 0;i--)

#define FOR(i, m, n) for(int i = m;i < n;i++)

#define FORR(i, m, n) for(int i = m;i >= n;i--)

#define SORT(v, n) sort(v, v+n);

#define VSORT(v) sort(v.begin(), v.end());

#define VRSORT(v) sort(v.rbegin(), v.rend());//vectorの降順ソート

#define ll long long

#define pb(a) push_back(a)

#define INF 1000000000

const int MOD = 1000000009;



using namespace std;

typedef pair<int, int> P;

typedef pair<ll, ll> LP;

typedef pair<int, P> PP;

typedef pair<ll, LP> LPP;



int dy[]={0, 0, 1, -1, 0};

int dx[]={1, -1, 0, 0, 0};



const int MAX_N = 15;

const int MAX_W = 110000;

int dp[MAX_N][MAX_W];



int main(){

  cin.tie(0);

  ios::sync_with_stdio(false);

  int W;

  cin >> W;

  

  vector<int> weight;

  for(int i=1;i <=W;i*=6) weight.push_back(i);

  for(int i=9;i <= W; i*=9) weight.push_back(i);

  int n = (int)weight.size();



  //DP初期条件

  REP(i,MAX_N) REP(w,MAX_W) dp[i][w] = INF;

  for(int i=0; i<=n;++i) dp[i][0] = 0;



  //DPループ

  REP(i,n){

      for(int w=0;w<=W;w++){

          dp[i+1][w] = min(dp[i+1][w], dp[i][w]);

          if(w >= weight[i]) dp[i+1][w] = min(dp[i+1][w], dp[i+1][w-weight[i]]+1);

      }

  }



  cout << dp[n][W] << endl;

}

// Faster C++ solution for the same problem (measured speedup: 7.147297006814413x):
#include <bits/stdc++.h>

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

#define SORT(v, n) sort(v, v+n);

#define VSORT(v) sort(v.begin(), v.end())

#define VRSORT(v) sort(v.rbegin(), v.rend())//vectorの降順ソート

#define ll long long

#define pb(a) push_back(a)

#define INF 1000000000

#define MOD 1000000007

using namespace std;

typedef pair<int, int> P;

typedef pair<ll, ll> LP;

typedef pair<int, P> PP;

typedef pair<ll, LP> LPP;



typedef vector<unsigned int>vec;

typedef vector<vec> mat;

//typedef tuple<ll, ll, ll> T;



template<class T> inline bool chmin(T& a, T b) {

    if (a > b) {

        a = b;

        return true;

    }

    return false;

}

template<class T> inline bool chmax(T& a, T b) {

    if (a < b) {

        a = b;

        return true;

    }

    return false;

}



int dy[]={0, 0, 1, -1, 0};

int dx[]={1, -1, 0, 0, 0};



int main(){

    cin.tie(0);

    ios::sync_with_stdio(false);

    int N;

    cin>>N;



    vector<ll> dp(N+1,INF);

    

    dp[0]=0;



    for(int n=1;n<=N;n++){

        for(int pow6=1;pow6<=n;pow6*=6){

            chmin(dp[n],dp[n-pow6]+1);

        }

        for(int pow9=1;pow9<=n;pow9*=9){

            chmin(dp[n],dp[n-pow9]+1);

        }

    }

    cout<<dp[N]<<endl;

}
// Slower C++ solution:
#include <bits/stdc++.h>

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

#define REPR(i, n) for(int i = n;i >= 0;i--)

#define FOR(i, m, n) for(int i = m;i < n;i++)

#define FORR(i, m, n) for(int i = m;i >= n;i--)

#define SORT(v, n) sort(v, v+n);

#define VSORT(v) sort(v.begin(), v.end());

#define VRSORT(v) sort(v.rbegin(), v.rend());//vectorの降順ソート

#define ll long long

#define pb(a) push_back(a)

#define INF 1000000000

const int MOD = 1000000009;



using namespace std;

typedef pair<int, int> P;

typedef pair<ll, ll> LP;

typedef pair<int, P> PP;

typedef pair<ll, LP> LPP;



int dy[]={0, 0, 1, -1, 0};

int dx[]={1, -1, 0, 0, 0};



const int MAX_N = 15;

const int MAX_W = 110000;

int dp[MAX_N][MAX_W];



int main(){

  cin.tie(0);

  ios::sync_with_stdio(false);

  int W;

  cin >> W;

  

  vector<int> weight;

  for(int i=1;i <=W;i*=6) weight.push_back(i);

  for(int i=9;i <= W; i*=9) weight.push_back(i);

  int n = (int)weight.size();



  //DP初期条件

  REP(i,MAX_N) REP(w,MAX_W) dp[i][w] = INF;

  for(int i=0; i<=n;++i) dp[i][0] = 0;



  //DPループ

  REP(i,n){

      for(int w=0;w<=W;w++){

          dp[i+1][w] = min(dp[i+1][w], dp[i][w]);

          if(w >= weight[i]) dp[i+1][w] = min(dp[i+1][w], dp[i+1][w-weight[i]]+1);

      }

  }



  cout << dp[n][W] << endl;

}

// Faster C++ solution for the same problem (measured speedup: 6.856533051386115x):
#include <bits/stdc++.h>

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

#define VSORT(v) sort(v.begin(), v.end())

#define VRSORT(v) sort(v.rbegin(), v.rend())

#define ll long long

using namespace std;

typedef pair<int, int> P;

typedef pair<ll, ll> LP;

typedef pair<int, P> PP;

typedef pair<ll, LP> LPP;

//typedef vector<unsigned int>vec;

typedef vector<ll>vec;

typedef vector<vec> mat;

typedef vector<vector<int>> Graph;



const int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};

const int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};

const int INF = 1000000000;

const ll LINF = 1000000000000000000;//1e18

const ll  MOD = 1000000007;

const double PI = acos(-1.0);

const double EPS = 1e-10;



template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }

template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }

template<class T> inline void add(T &a, T b){a = ((a+b) % MOD + MOD) % MOD;};





ll dp[101010];



int main(){

    cin.tie(0);

    ios::sync_with_stdio(false);

    int N;

    cin >> N;

    vector<ll> six, nine;

    for(int i=0;pow(3,i)<=N;i++) six.push_back(pow(6,i));

    for(int i=0;pow(9,i)<=N;i++) nine.push_back(pow(9,i));



    dp[0] = 0;

    REP(i,N) dp[i+1] = INF;

    

    for(int i=1;i<=N;i++){

        for(int j=0;j<(int)nine.size();j++){

            if(i - nine[j] >= 0) chmin(dp[i], dp[i-nine[j]] + 1);

        }

        for(int j=0;j<(int)six.size();j++){

            if(i - six[j] >= 0) chmin(dp[i], dp[i-six[j]] + 1);

        }

    }

    cout << dp[N] << endl;

}