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

#include <vector>

using namespace std;



// DPテーブル

const int MAX_N = 15;

const int MAX_W = 110000;

int dp[MAX_N][MAX_W]; // 12 種類だが余裕をもつ



int main() {

	int W;

	cin >> W;



	// 品物を列挙 (価値はすべて 1)

	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); // 1 が重複しないように (しても問題はない)

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



	// DP初期条件: dp[i][0] = 0

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

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



	// DPループ

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

		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: 5.309622762966522x):
#include <iostream>

#include <vector>

using namespace std;



// DPテーブル

const int MAX_W = 110000;

int dp[MAX_W];



int main() {

	int W;

	cin >> W;



	// 品物を列挙 (価値はすべて 1)

	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); // 1 が重複しないように (しても問題はない)

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



	// DP初期条件: dp[i][0] = 0

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

	dp[0] = 0;



	// DPループ

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

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

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

		}

	}



	cout << dp[W] << endl;

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

#include<string>

#include<algorithm>

#include<vector>

#include<queue>

#include<random>

#include<map>

#include<functional>

using namespace std;

#define int long long

#define inf 21000000145141919

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

typedef pair<int, int>P;

struct edge { int to, cost; };

int dp[114514];

signed main() {

	int n; cin >> n;

	vector<int>Q;

	int a = 1;

	rep(i, 10) {

		Q.push_back(a);

		a *= 6;

	}a = 1; rep(i, 10) {

		Q.push_back(a);

		a *= 9;

	}

	int ans = 0;

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

	fill(dp, dp + 114514, 1145141919);

	dp[0] = 0;

	dp[1] = 1;

	rep(i, Q.size()) {

		for (int j= 2; j <= n; j++) {

			int b = j - Q[i];

			if (b >= 0)dp[j] = min(dp[j], dp[b] + 1);

		}

	}

	cout << dp[n] << endl;

}

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

#include<assert.h>

using namespace std;

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

#define int long long

const int inf = 1e17;

const int mod = 1000000007;

typedef pair<int, int> P;

typedef pair<int, P> PP;

int dp[100006];

vector<int>K;

signed main() {

	int n; cin >> n;

	int now = 1;

	rep(i, 7){K.push_back(now); now *= 6;

}

	now = 1;

	rep(i, 7) {

		K.push_back(now); now *= 9;

	}

	fill(dp, dp + n + 1, inf);

	dp[0] = 0;

	rep(i, n) {

		rep(j, K.size()) {

			if (i + K[j] <= n)dp[i + K[j]] = min(dp[i + K[j]], dp[i] + 1);

		}

	}

	cout << dp[n] << endl;

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

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

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

#define int long long

using namespace std;

typedef vector<int>   vint;

typedef pair<int,int> pint;



int memo[110000];



int rec(int N){

    if(N==0) return 0;

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



    int 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;

}



signed main()

{

    int N; cin>>N;

    REP(i,110000) memo[i]=-1;

    cout<<rec(N)<<endl;

}


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

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

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

#define int long long

using namespace std;

typedef vector<int>   vint;

typedef pair<int,int> pint;



signed main()

{

    int N; cin>>N;

    vint dp(N+1,N);



    dp[0]=0;

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

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

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

        }

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

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

        }

    }

    cout<<dp[N]<<endl;

 

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

#include <vector>

using namespace std;



// DPテーブル

const int MAX_N = 15;

const int MAX_W = 110000;

int dp[MAX_N][MAX_W]; // 12 種類だが余裕をもつ



int main() {

    int W;

    cin >> W;



    // 品物を列挙 (価値はすべて 1)

    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); // 1 が重複しないように (しても問題はない)

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



    // DP初期条件: dp[i][0] = 0

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

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



    // DPループ

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

        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: 5.309622762966522x):
#include <iostream>

#include <vector>

using namespace std;



// DPテーブル

const int MAX_W = 110000;

int dp[MAX_W];



int main() {

    int W;

    cin >> W;



    // 品物を列挙 (価値はすべて 1)

    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); // 1 が重複しないように (しても問題はない)

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



    // DP初期条件: dp[i][0] = 0

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

    dp[0] = 0;



    // DPループ

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

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

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

        }

    }



    cout << dp[W] << endl;

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

using namespace std;

const int INF = 100000009;

const int DP_SIZE = 1000006;



int dp[DP_SIZE];



int main(){

	int N;

	cin >> N;

	

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

	dp[0] = 0;

	

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

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

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

	}

	

	cout << dp[N] << endl;

	

	return 0;

}

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

using namespace std;



int main(){

    int n;

    cin >> n;

    int ans = n;

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

        int t = i;

        int cc = 0;

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

        t = n - i;

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

        ans = min(ans, cc);

    }

    cout << ans << endl;

    return 0;

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

using namespace std;



typedef long long ll;

typedef unsigned long long ull;

typedef pair<ll, ll> P;



#define fi first

#define se second

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

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

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

#define dbg(x) cout<<#x"="<<x<<endl

#define mmax(x,y) (x>y?x:y)

#define mmin(x,y) (x<y?x:y)

#define maxch(x,y) x=mmax(x,y)

#define minch(x,y) x=mmin(x,y)

#define uni(x) x.erase(unique(all(x)),x.end())

#define exist(x,y) (find(all(x),y)!=x.end())

#define bcnt __builtin_popcount



#define INF INT_MAX/3

#define mod 1000000007



int n;

int dp[100010];



int main(){

  cin>>n;

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

  dp[0]=0;

  rep(i,n){

    for(int j=1;i+j<=n;j*=6)minch(dp[i+j],dp[i]+1);

    for(int j=1;i+j<=n;j*=9)minch(dp[i+j],dp[i]+1);

  }

  cout<<dp[n]<<endl;

  return 0;

}


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

using namespace std;



typedef long long ll;

typedef unsigned long long ull;

typedef pair<ll, ll> P;



#define fi first

#define se second

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

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

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

#define dbg(x) cout<<#x"="<<x<<endl

#define mmax(x,y) (x>y?x:y)

#define mmin(x,y) (x<y?x:y)

#define maxch(x,y) x=mmax(x,y)

#define minch(x,y) x=mmin(x,y)

#define uni(x) x.erase(unique(all(x)),x.end())

#define exist(x,y) (find(all(x),y)!=x.end())

#define bcnt __builtin_popcount



#define INF INT_MAX/3

#define mod 1000000007



int n;



int main(){

  cin>>n;

  int res=INF;

  rep(i,n+1){

    if(i%6!=0)continue;

    int ni=i,nj=n-i;

    int sum=0;

    ni/=6;

    while(ni>0){

      sum+=ni%6;

      ni/=6;

    }

    while(nj>0){

      sum+=nj%9;

      nj/=9;

    }

    minch(res,sum);

  }

  cout<<res<<endl;

  return 0;

}
// 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.01678946459618x):
#include <iostream>

#include <cmath>

#include <string>

#include <algorithm>

#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++)

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

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

#define INF 10000000000

#define MOD 1000000007LL

using namespace std;

typedef long long ll;

typedef pair<ll, ll> p;



int main(void) {

    ll n, d[100001];

    cin >> n;

    ll i, j;

    ll a[13];

    a[1]=1;

    for(i=2; i<=7; i++)

        a[i]=pow(6, i-1);

    for(i=8; i<=12; i++)

        a[i]=pow(9, i-7);

    

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

        d[i]=INF;

    d[0]=0;

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

        for(j=0; j+a[i]<=n; j++) {

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

        }

    }

    cout << d[n] << endl;

}
// 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: 1.3700052867577746x):
#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;



ll memo[MAX];



ll rec(ll n) {

    if(n==0) return 0;

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

    ll res=n;

    

    for(ll psix=1; psix<=n; psix*=6)

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

    

    for(ll pnin=1; pnin<=n; pnin*=9)

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

    

    return memo[n]=res;

}





int main() {

    ll N; cin >> N;

    for(ll i=0; i<=N; i++) memo[i]=-1;

    

    cout << rec(N) << endl;

}
// 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: 1.8731886625996075x):
#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 N, n;

    cin >> N;

    ll i;

    ll d[MAX];

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

        d[i]=N;

    d[0]=0;

    ll psix, pnin;

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

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

            d[n]=min(d[n], d[n-psix]+1);

        

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

            d[n]=min(d[n], d[n-pnin]+1);

    }

    

    prin(d[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: 1.5192094663269236x):
#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 N;

    cin >> N;

    ll i;

    ll d[MAX];

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

        d[i]=-1;

    d[0]=0;

    queue<ll> q;

    q.push(0);

    ll u;

    ll psix, pnin;

    

    while(q.size()) {

        u=q.front(); q.pop();

        

        for(psix=1; u+psix<=N; psix*=6) {

            if(d[u+psix]==-1) {

                d[u+psix]=d[u]+1;

                q.push(u+psix);

            }

            

            for(pnin=1; u+pnin<=N; pnin*=9) {

                if(d[u+pnin]==-1) {

                    d[u+pnin]=d[u]+1;

                    q.push(u+pnin);

                }

            }

            

        }

        

    }

    

    prin(d[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.0157745770303217x):
#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 <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.6823827109860026x):
#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 <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.088660857021162x):
#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]);

    

}