stablegradients/pie-gem5-pairs huggingface dataset:
// Slower C++ solution:
#include <iostream>

#include <cmath>

#include <string>

#include <algorithm>

#include <set>

#include <vector>

#include <map>

#include <list>

#include <stack>

#include <cstdio>

#include <cstring>

#include <cstdlib>

#include <queue>

#define mkp make_pair

#define pr(num) cout << num << "\n"

#define max(a, b) ((a)>(b) ? (a):(b))

#define min(a, b) ((a)<(b) ? (a):(b))

#define INF 1000000000000000000

#define MOD 1000000007LL

#define MAX 100010

using namespace std;

typedef long long ll;

typedef pair<ll, ll> P;

typedef map<ll, ll> Map;



ll memo[MAX];

ll N;



ll rec(ll n) {

    if(n==0) return 0;

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

    

    ll res=n;

    for(ll i=1; i<=n; i*=6) {

        res=min(res, rec(n-i)+1);

    }

    for(ll i=1; i<=n; i*=9) {

        res=min(res, rec(n-i)+1);

    }

    

    return memo[n]=res;

}





int main() {

    cin >> N;

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

    

    pr(rec(N));

    

    

    

    

}


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

#include <cmath>

#include <string>

#include <algorithm>

#include <set>

#include <vector>

#include <map>

#include <list>

#include <stack>

#include <cstdio>

#include <cstring>

#include <cstdlib>

#include <queue>

#define mkp make_pair

#define pr(num) cout << num << "\n"

#define max(a, b) ((a)>(b) ? (a):(b))

#define min(a, b) ((a)<(b) ? (a):(b))

#define INF 1000000000000000000

#define MOD 1000000007LL

#define MAX 100010

using namespace std;

typedef long long ll;

typedef pair<ll, ll> P;

typedef map<ll, ll> Map;





int main(void) {

    ll N;

    cin >> N;

    ll dp[MAX];

    ll c[]={1, 6, 36, 216, 216*6, 216*36, 216*216, 9, 81, 729, 729*9, 729*81};

    ll i, j;

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

        dp[i]=INF;

    }

    

    dp[0]=0;

    

    for(i=1; i<=12; i++) {

        for(j=0; j<=N; j++) {

            if(j>=c[i-1])

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

        }

    }

    

    pr(dp[N]);

    

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

#include <cmath>

#include <string>

#include <set>

#include <vector>

#include <map>

#include <list>

#include <stack>

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

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

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

using namespace std;

typedef long long ll;

typedef pair<ll, ll> p;



ll memo[100001]={0};



ll rec(ll n);



int main(void) {

    ll n;

    cin >> n;

    

    for(int i=0; i<100001; i++)

        memo[i]=-1;

    cout << rec(n) << endl;

}



ll rec(ll n) {

    if(n==0)

        return 0;

    else if(memo[n]!=-1)

        return memo[n];

    

    ll res=n;

    

    for(int i=1; i<=n; i*=6)

        res=min(res, rec(n-i)+1);

    for(int i=1; i<=n; i*=9)

        res=min(res, rec(n-i)+1);

    

    return memo[n]=res;

}


// Faster C++ solution for the same problem (measured speedup: 2.0204197806519013x):
//もらうdp

#include <iostream>

#include <cmath>

#include <string>

#include <algorithm>

#include <set>

#include <vector>

#include <map>

#include <list>

#include <stack>

#include <cstdio>

#include <cstring>

#include <cstdlib>

#include <queue>

#define mkp make_pair

#define fi first

#define se second

#define pt(num) cout << num << "\n"

#define moC(a, s, b) (a)=((a)s(b)+MOD)%MOD

#define max(a, b) ((a)>(b) ? (a):(b))

#define min(a, b) ((a)<(b) ? (a):(b))

#define chmax(a, b) (a<b ? a=b : 0)

#define chmin(a, b) (a>b ? a=b : 0)

#define INF 1000000000000000000

#define MOD 1000000007LL

#define MAX 101010

using namespace std;

typedef long long ll;

typedef pair<ll, ll> P;

typedef map<ll, ll> Map;





int main(void) {

    ll N;

    cin >> N;

    ll dp[MAX];

    ll i, j;

    

    for(i=1; i<=N; i++) dp[i]=INF;

    

    dp[0]=0;

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

        for(j=i; j<=N; j++) {

            chmin(dp[j], dp[j-i]+1);

        }

    }

    

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

        for(j=i; j<=N; j++) {

            chmin(dp[j], dp[j-i]+1);

        }

    }

    

    pt(dp[N]);

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

#include <cmath>

#include <string>

#include <algorithm>

#include <set>

#include <vector>

#include <map>

#include <list>

#include <stack>

#include <cstdio>

#include <cstring>

#include <cstdlib>

#include <queue>

#define max(a, b) ((a)>(b) ? (a):(b))

#define min(a, b) ((a)<(b) ? (a):(b))

#define pye cout << "Yes" << endl;

#define pno cout << "No" << endl;

#define prin(num) cout << num << endl;

#define sprin(str) cout << "str" << endl;

#define INF 1000000000

#define MOD 1000000007LL

#define MAX 100001

#define NIL -1

#define WHITE 0

#define GRAY 1

#define BLACK 2

using namespace std;

typedef long long ll;

typedef pair<ll, ll> PA;

typedef priority_queue<ll> PQ;

typedef pair<double, double> doup;

//typedef vector<ll> vec;

//typedef vector<vec> mat;





int main() {

    ll W;

    cin >> W;

    vector<ll> weight;

    ll dp[MAX];

    ll i, w;

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

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

    ll n=weight.size();

    

    for(w=0; w<MAX; w++) {

            dp[w]=INF;

    }

    dp[0]=0;

    

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

        for(w=weight[i]; w<=W; w++) {

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

        }

    }

    

    

    prin(dp[W]);

}


// Faster C++ solution for the same problem (measured speedup: 1.6862596376776116x):
//もらうdp

#include <iostream>

#include <cmath>

#include <string>

#include <algorithm>

#include <set>

#include <vector>

#include <map>

#include <list>

#include <stack>

#include <cstdio>

#include <cstring>

#include <cstdlib>

#include <queue>

#define mkp make_pair

#define fi first

#define se second

#define pt(num) cout << num << "\n"

#define moC(a, s, b) (a)=((a)s(b)+MOD)%MOD

#define max(a, b) ((a)>(b) ? (a):(b))

#define min(a, b) ((a)<(b) ? (a):(b))

#define chmax(a, b) (a<b ? a=b : 0)

#define chmin(a, b) (a>b ? a=b : 0)

#define INF 1000000000000000000

#define MOD 1000000007LL

#define MAX 101010

using namespace std;

typedef long long ll;

typedef pair<ll, ll> P;

typedef map<ll, ll> Map;





int main(void) {

    ll N;

    cin >> N;

    ll dp[MAX];

    ll i, j;

    

    for(i=1; i<=N; i++) dp[i]=INF;

    

    dp[0]=0;

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

        for(j=i; j<=N; j++) {

            chmin(dp[j], dp[j-i]+1);

        }

    }

    

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

        for(j=i; j<=N; j++) {

            chmin(dp[j], dp[j-i]+1);

        }

    }

    

    pt(dp[N]);

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

#include <cmath>

#include <string>

#include <algorithm>

#include <set>

#include <vector>

#include <map>

#include <list>

#include <stack>

#include <cstdio>

#include <cstring>

#include <cstdlib>

#include <queue>

#define mkp make_pair

#define pr(num) cout << num << "\n"

#define max(a, b) ((a)>(b) ? (a):(b))

#define min(a, b) ((a)<(b) ? (a):(b))

#define INF 1000000000000000000

#define MOD 1000000007LL

#define MAX 100010

using namespace std;

typedef long long ll;

typedef pair<ll, ll> P;

typedef map<ll, ll> Map;



ll N;

ll memo[MAX];



ll rec(ll m) {

    if(m==0) return 0;

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

    

    ll res=m;

    

    for(ll i=1; i<=m; i*=6) {

        res=min(res, rec(m-i)+1);

    }

    

    for(ll i=1; i<=m; i*=9) {

        res=min(res, rec(m-i)+1);

    }

    

    return memo[m]=res;

}







int main(void) {

    cin >> N;

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

    pr(rec(N));

    

}
















































// Faster C++ solution for the same problem (measured speedup: 2.0939312969595973x):
//もらうdp

#include <iostream>

#include <cmath>

#include <string>

#include <algorithm>

#include <set>

#include <vector>

#include <map>

#include <list>

#include <stack>

#include <cstdio>

#include <cstring>

#include <cstdlib>

#include <queue>

#define mkp make_pair

#define fi first

#define se second

#define pt(num) cout << num << "\n"

#define moC(a, s, b) (a)=((a)s(b)+MOD)%MOD

#define max(a, b) ((a)>(b) ? (a):(b))

#define min(a, b) ((a)<(b) ? (a):(b))

#define chmax(a, b) (a<b ? a=b : 0)

#define chmin(a, b) (a>b ? a=b : 0)

#define INF 1000000000000000000

#define MOD 1000000007LL

#define MAX 101010

using namespace std;

typedef long long ll;

typedef pair<ll, ll> P;

typedef map<ll, ll> Map;





int main(void) {

    ll N;

    cin >> N;

    ll dp[MAX];

    ll i, j;

    

    for(i=1; i<=N; i++) dp[i]=INF;

    

    dp[0]=0;

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

        for(j=i; j<=N; j++) {

            chmin(dp[j], dp[j-i]+1);

        }

    }

    

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

        for(j=i; j<=N; j++) {

            chmin(dp[j], dp[j-i]+1);

        }

    }

    

    pt(dp[N]);

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

#include <cmath>

#include <string>

#include <algorithm>

#include <set>

#include <vector>

#include <map>

#include <list>

#include <stack>

#include <cstdio>

#include <cstring>

#include <cstdlib>

#include <queue>

#define mkp make_pair

#define pr(num) cout << num << "\n"

#define max(a, b) ((a)>(b) ? (a):(b))

#define min(a, b) ((a)<(b) ? (a):(b))

#define INF 1000000000000000000

#define MOD 1000000007LL

#define MAX 100010

using namespace std;

typedef long long ll;

typedef pair<ll, ll> P;

typedef map<ll, ll> Map;



ll memo[MAX];

ll N;



ll rec(ll n) {

    if(n==0) return 0;

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

    

    ll res=n;

    for(ll i=1; i<=n; i*=6) {

        res=min(res, rec(n-i)+1);

    }

    for(ll i=1; i<=n; i*=9) {

        res=min(res, rec(n-i)+1);

    }

    

    return memo[n]=res;

}





int main() {

    cin >> N;

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

    

    pr(rec(N));

    

    

    

    

}


// Faster C++ solution for the same problem (measured speedup: 2.093393105871847x):
//もらうdp

#include <iostream>

#include <cmath>

#include <string>

#include <algorithm>

#include <set>

#include <vector>

#include <map>

#include <list>

#include <stack>

#include <cstdio>

#include <cstring>

#include <cstdlib>

#include <queue>

#define mkp make_pair

#define fi first

#define se second

#define pt(num) cout << num << "\n"

#define moC(a, s, b) (a)=((a)s(b)+MOD)%MOD

#define max(a, b) ((a)>(b) ? (a):(b))

#define min(a, b) ((a)<(b) ? (a):(b))

#define chmax(a, b) (a<b ? a=b : 0)

#define chmin(a, b) (a>b ? a=b : 0)

#define INF 1000000000000000000

#define MOD 1000000007LL

#define MAX 101010

using namespace std;

typedef long long ll;

typedef pair<ll, ll> P;

typedef map<ll, ll> Map;





int main(void) {

    ll N;

    cin >> N;

    ll dp[MAX];

    ll i, j;

    

    for(i=1; i<=N; i++) dp[i]=INF;

    

    dp[0]=0;

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

        for(j=i; j<=N; j++) {

            chmin(dp[j], dp[j-i]+1);

        }

    }

    

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

        for(j=i; j<=N; j++) {

            chmin(dp[j], dp[j-i]+1);

        }

    }

    

    pt(dp[N]);

}
// Slower C++ solution:
//メモ再帰

#include <iostream>

#include <cmath>

#include <string>

#include <algorithm>

#include <set>

#include <vector>

#include <map>

#include <list>

#include <stack>

#include <cstdio>

#include <cstring>

#include <cstdlib>

#include <queue>

#define mkp make_pair

#define fi first

#define se second

#define pt(num) cout << num << "\n"

#define mCalc(a, s, b) (a)=((a)s(b)+MOD)%MOD

#define max(a, b) ((a)>(b) ? (a):(b))

#define min(a, b) ((a)<(b) ? (a):(b))

#define chmax(a, b) (a<b ? a=b : 0)

#define chmin(a, b) (a>b ? a=b : 0)

#define INF 1000000000000000000

#define MOD 1000000007LL

#define MAX 101010

using namespace std;

typedef long long ll;

typedef pair<ll, ll> P;

typedef map<ll, ll> Map;



ll N;

ll dp[MAX];



ll rec(ll n) {

    if(n==0) return 0;

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

    

    ll i;

    ll res=INF;

    for(i=1; i<=n; i*=6)

        res=min(res, rec(n-i)+1);

    

    for(i=1; i<=n; i*=9)

        res=min(res, rec(n-i)+1);

    

    

    return dp[n]=res;

}





int main(void) {;

    cin >> N;

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

    

    pt(rec(N));

}




// Faster C++ solution for the same problem (measured speedup: 2.1027066603522213x):
//もらうdp

#include <iostream>

#include <cmath>

#include <string>

#include <algorithm>

#include <set>

#include <vector>

#include <map>

#include <list>

#include <stack>

#include <cstdio>

#include <cstring>

#include <cstdlib>

#include <queue>

#define mkp make_pair

#define fi first

#define se second

#define pt(num) cout << num << "\n"

#define moC(a, s, b) (a)=((a)s(b)+MOD)%MOD

#define max(a, b) ((a)>(b) ? (a):(b))

#define min(a, b) ((a)<(b) ? (a):(b))

#define chmax(a, b) (a<b ? a=b : 0)

#define chmin(a, b) (a>b ? a=b : 0)

#define INF 1000000000000000000

#define MOD 1000000007LL

#define MAX 101010

using namespace std;

typedef long long ll;

typedef pair<ll, ll> P;

typedef map<ll, ll> Map;





int main(void) {

    ll N;

    cin >> N;

    ll dp[MAX];

    ll i, j;

    

    for(i=1; i<=N; i++) dp[i]=INF;

    

    dp[0]=0;

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

        for(j=i; j<=N; j++) {

            chmin(dp[j], dp[j-i]+1);

        }

    }

    

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

        for(j=i; j<=N; j++) {

            chmin(dp[j], dp[j-i]+1);

        }

    }

    

    pt(dp[N]);

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

#include <iostream>

using namespace std;



const int MAXN = 100001;

int dp[MAXN];



int rec_bank(int n) {

  int ret = n;

  if (n == 0)

    return 0;

  if (dp[n] != -1)

    return dp[n];



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

    ret = min(ret, rec_bank(n - pow6) + 1);

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

    ret = min(ret, rec_bank(n - pow9) + 1);

  return dp[n] = ret;

}



int main() {

  int N;

  cin >> N;

  for (int i = 0; i < MAXN; i++)

    dp[i] = -1;

  cout << rec_bank(N) << endl;

  return 0;

}


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



using namespace std;



int f(int n, int base) {

  int ret = 0;

  while (n > 0) {

    int r = n % base;

    n /= base;

    ret += r;

  }

  return ret;

}



int main() {

  ios::sync_with_stdio(false);

  cin.tie(0);

  int N;

  cin >> N;

  int minn = N;

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

    minn = min(f(N - i, 6) + f(i, 9), minn);

  cout << minn << '\n';

  return 0;

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



using namespace std;



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

#define rep(i,n) REP(i,0,n)

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

#define zero(a) memset(a, 0, sizeof a)

#define minus(a) memset(a, -1, sizeof a)

#define watch(a) { cout << #a << " = " << a << endl; }

template<class T1, class T2> inline bool minimize(T1 &a, T2 b) { return b < a && (a = b, 1); }

template<class T1, class T2> inline bool maximize(T1 &a, T2 b) { return a < b && (a = b, 1); }

template<class T> void operator>> (istream& ist, vector<T>& vs) { for(auto& e: vs) cin >> e; }



typedef long long ll;

int const inf = 1<<29;



constexpr int MaxK = 50;

int dp[100001];



int main() {



  rep(i, 100001) dp[i] = inf;

  dp[0] = 0;



  set<int> st;

  st.insert(1);

  rep(i, 8) st.insert(pow(6LL, i + 1));

  rep(i, 7) st.insert(pow(9LL, i + 1));



  int N; cin >> N;

  rep(i, N + 1) for (auto e: st) {

    if (i + e > N) continue;

    if (dp[i] == inf) continue;

    minimize(dp[i + e], dp[i] + 1);

  }

  cout << dp[N] << endl;

}


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

int N ;

int main ()

{

scanf ("%d" ,&N );

int res = N ;

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

{

int cc =0;

int t = i ;

while (t >0) cc += t %6 , t /=6;

t=N - i ;

while (t >0) cc += t %9 , t /=9;

if ( res > cc ) res = cc ;

}

printf ("%d\n" , res );

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

using namespace std;

using ll=long long;



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;

}

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

using namespace std;

using ll=long long;

typedef pair<ll,ll> P;

#define fi first

#define se second

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

const ll mod=1000000007;

const ll mod2=998244353;

const ll inf=100000000000000000;

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

ll lcm(ll c,ll d){return c/gcd(c,d)*d;}

vector<int> dy={-1,0,1,0},dx={0,-1,0,1};

ll dp[1000010];

int main(){

    ios_base::sync_with_stdio(false);

    cin.tie(NULL);

    int n;

    cin>>n;

    fill(dp,dp+n,inf);

    dp[n]=0;

    vector<ll> tmp;

    tmp.push_back(1);

    ll six=6,nine=9;

    while(six<=n){

        tmp.push_back(six);

        six*=6;

    }

     while(nine<=n){

        tmp.push_back(nine);

        nine*=9;

    }

    sort(all(tmp));

    for(int i=n;i>=1;i--){

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

            if(i<tmp[j])break;

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

        }

    }

    cout<<dp[0]<<endl;

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

#include <cstdio>

#include <string>

#include <algorithm>

#include <utility>

#include <cmath>

#include <vector>

#include <queue>

#include <set>

#include <map>

#include <functional>

using namespace std;

typedef long long ll;

typedef vector<ll> vl;

typedef vector<vector<ll>> vvl;

typedef pair<ll, ll> P;

typedef vector<vector<P>> vvP;

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

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

#define out(x) cout << x << endl

#define exout(x) printf("%.10f\n", x)

#define chmax(x, y) x = max(x, y)

#define chmin(x, y) x = min(x, y)

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

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

#define pb push_back

#define re0 return 0

const ll mod = 1000000007;

const ll INF = 1e16;

const ll MAX_N = 100010;



ll dp[MAX_N];



ll f(ll i) {

    if(i == 0) {

        return 0;

    }

    if(dp[i] != -1) {

        return dp[i];

    }

    ll res = i;

    for(ll pow6 = 1; pow6 <= i; pow6 *= 6) {

        chmin(res, f(i - pow6) + 1);

    }

    for(ll pow9 = 1; pow9 <= i; pow9 *= 9) {

        chmin(res, f(i - pow9) + 1);

    }

    return dp[i] = res;

}



int main() {

    ll n;

    cin >> n;



    rep(i, MAX_N) {

        dp[i] = -1;

    }

    

    out(f(n));

    re0;

}

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

#include <cstdio>

#include <string>

#include <algorithm>

#include <utility>

#include <cmath>

#include <vector>

#include <queue>

#include <set>

#include <map>

#include <functional>

using namespace std;

typedef long long ll;

typedef vector<ll> vl;

typedef vector<vector<ll>> vvl;

typedef pair<ll, ll> P;

typedef vector<vector<P>> vvP;

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

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

#define out(x) cout << x << endl

#define exout(x) printf("%.10f\n", x)

#define chmax(x, y) x = max(x, y)

#define chmin(x, y) x = min(x, y)

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

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

#define pb push_back

#define re0 return 0

const ll mod = 1000000007;

const ll INF = 1e16;

const ll MAX_N = 100010;



int main() {

    ll n;

    cin >> n;



    vl dp(n+1, INF);  // dp[i] : i円払うのに必要な操作回数の最小値

    dp[0] = 0;

    exrep(i, 1, n) {

        for(ll pow6 = 1; pow6 <= i; pow6 *= 6) {

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

        }

        for(ll pow9 = 1; pow9 <= i; pow9 *= 9) {

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

        }

    }



    out(dp[n]);

    re0;

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

#include <cstdio>

#include <string>

#include <algorithm>

#include <utility>

#include <cmath>

#include <vector>

#include <queue>

#include <set>

#include <map>

#include <functional>

using namespace std;

typedef long long ll;

typedef vector<ll> vl;

typedef vector<vector<ll>> vvl;

typedef pair<ll, ll> P;

typedef vector<vector<P>> vvP;

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

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

#define out(x) cout << x << endl

#define exout(x) printf("%.10f\n", x)

#define chmax(x, y) x = max(x, y)

#define chmin(x, y) x = min(x, y)

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

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

#define pb push_back

#define re0 return 0

const ll mod = 1000000007;

const ll INF = 1e16;

const ll MAX_N = 100010;



ll dp[MAX_N];



ll f(ll i) {

    if(i == 0) {

        return 0;

    }

    if(dp[i] != -1) {

        return dp[i];

    }

    ll res = i;

    for(ll pow6 = 1; pow6 <= i; pow6 *= 6) {

        chmin(res, f(i - pow6) + 1);

    }

    for(ll pow9 = 1; pow9 <= i; pow9 *= 9) {

        chmin(res, f(i - pow9) + 1);

    }

    return dp[i] = res;

}



int main() {

    ll n;

    cin >> n;



    rep(i, MAX_N) {

        dp[i] = -1;

    }

    

    out(f(n));

    re0;

}

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

#include <cstdio>

#include <string>

#include <algorithm>

#include <utility>

#include <cmath>

#include <vector>

#include <queue>

#include <set>

#include <map>

#include <functional>

using namespace std;

typedef long long ll;

typedef vector<ll> vl;

typedef vector<vector<ll>> vvl;

typedef pair<ll, ll> P;

typedef vector<vector<P>> vvP;

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

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

#define out(x) cout << x << endl

#define exout(x) printf("%.10f\n", x)

#define chmax(x, y) x = max(x, y)

#define chmin(x, y) x = min(x, y)

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

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

#define pb push_back

#define re0 return 0

const ll mod = 1000000007;

const ll INF = 1e16;

const ll MAX_N = 100010;



int main() {

    ll n;

    cin >> n;



    // BFS。計算量はO(V+E)

    vl dist(n+1, -1);  // dist[v] : 頂点svから頂点vへの最短距離(-1のとき未訪問)

    queue<ll> Q;

    const ll sv = 0;  // BFSを始める頂点(デフォルトは0)

    dist[sv] = 0;

    Q.push(sv);

    while(!Q.empty()) {

        ll v = Q.front();

        Q.pop();

        for(ll pow6 = 1; v + pow6 <= n; pow6 *= 6) {

            ll u = v + pow6;

            if(dist[u] == -1) {

                dist[u] = dist[v] + 1;

                Q.push(u);   

            }

        }

        for(ll pow9 = 1; v + pow9 <= n; pow9 *= 9) {

            ll u = v + pow9;

            if(dist[u] == -1) {

                dist[u] = dist[v] + 1;

                Q.push(u);   

            }

        }

    }



    out(dist[n]);

    re0;

}