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

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

using namespace std;

typedef long long ll;

typedef pair<int,int> P;

typedef vector<vector<int> > Graph;



const int INF = 1<<29;

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

int dp[100010];

int main(){

    int n;

    cin >> n;

    rep(i,100010){

        dp[i]=INF;

    }

    dp[0]=0;

    int k;

    int l;

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

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

        k=1;

        l=1;

        while(i-pow(6,k)>=0){

            chmin(dp[i],dp[i-(int)pow(6,k)]+1);

            k++;

        }

        while(i-pow(9,l)>=0){

            chmin(dp[i],dp[i-(int)pow(9,l)]+1);

            l++;

        }

    }

    cout << dp[n] << endl;

    return 0;

}

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

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

using namespace std;

typedef long long ll;

typedef pair<int,int> P;

typedef vector<vector<int> > Graph;



//a進法でのnの総和

int wa(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 now = wa(i,6) + wa (n-i,9);

        res = min (res,now);

    }

    cout << res << endl;

    return 0;

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

#include<iostream>

using namespace std;

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

int dp[100010];

int main(){

	int N;

	cin>>N;

	dp[0]=0;

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

		int min=1+dp[i-1];

		int j=6;

		while(i>=j){

			//if(i-j<0)break;

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

			j*=6;

		

		}

		j=9;

		while(i>=j){

			

			//if(i-j<0)break;

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

			j*=9;

		

		}

		dp[i]=min;

	}

	cout<<dp[N];

}

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

#include <string.h>

#include <vector>

#include <algorithm>

#include <cmath>

#include <map>

#include <functional>

#include <cassert>

#include <utility>

#include <stack>

#include <queue>

using namespace std;

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

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

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

#define FOR(i,n1,n2) for(int i=n1;i<n2;i++)

#define speed_up    ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);

typedef pair<int,int> Pi;

typedef long long int ll;

const int INF=(ll)(1<<30)-1;

const ll INFl=(ll)9223372036854775807;

const int MAX=10000;

const ll MOD=(ll)1e9+7;

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;



int main(){

	cin>>n;

	int ans=INF;

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

		int t=n;

		int s=i;

		int tans=0;

		t-=6*i;

		while(s>0){

			tans+=(s%6);

			s/=6;

		}

		while(t>0){

			tans+=(t%9);

			t/=9;

		}

		ans=min(tans,ans);

	}

	cout<<ans<<endl;

	return 0;

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

 #include <vector>

 #include <map>

 #include <string>

 #include <algorithm>

 using namespace std;



 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;

}



 typedef long long ll;

 const long long INF = 1LL << 60;







 int main()

 {

   int n;

   cin >> n;



   vector<long long> dp(100010, INF);



   dp.at(0) = 0;

   for(int i = 1; i <= n; i++)

   {

       int num = 1;

       while(num <= i)

       {

           chmin(dp.at(i), dp.at(i-num) + 1);

           num *= 6;

       }



       num = 9;

       while(num <= i)

       {

           chmin(dp.at(i), dp.at(i-num) + 1);

           num *= 9;

       }

   }



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



    return 0;

 }

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

#include <vector>

using namespace std;

int main(){

    int n; cin >> n;

    int INF = 100000;

    vector<int> dp(n+1, INF);

    dp[0] = 0;

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

        dp[i] = 1 + dp[i-1];

        int power = 6;

        while(power <= i){

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

            power *= 6;

        }

        power = 9;

        while(power <= i){

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

            power *= 9;

        }

    }

    cout << dp[n] << endl;

    return 0;

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



#define ft first

#define sd second



using namespace std;



typedef long long ll;

typedef pair<int, int> pii;

typedef pair<ll, ll> pll;





const int MAXT = 9, MAXN = 1000 * 1000 + 3;









int p9[MAXT], p6[MAXT], dp[MAXN], n;





int main() {

	p9[0] = p6[0] = 1;

	for (int i = 1; i < MAXT; i++)

		p9[i] = p9[i - 1] * 9, p6[i] = p6[i - 1] * 6;

	cin >> n;

	memset(dp, 127, sizeof dp);

	dp[0] = 0;

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

		dp[i] = dp[i - 1] + 1;

		for (int j = 0; j < MAXT; j++) {

			if (i < p9[j])

				break;

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

		}

		for (int j = 0; j < MAXT; j++) {

			if (i < p6[j])

				break;

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

		}

	}

	cout << dp[n];

	return 0;

}

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

using namespace std;



int main()

 {

  int n; cin>>n;

  int sum= n;

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

    int cc=0,t=i;

    while(t>0)

 {

      cc += t%6;

      t /= 6;

    }

    t = n-i;

    while(t>0)

{

      cc += t%9;

      t /= 9;

    }

    if(sum>cc) sum = cc;

  }

  cout<<sum;

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

#include <iostream>

#include <vector>

using namespace std;



vector<int> a(100001, -1);



int dp(int b) {

	if (!b) return 0;

	if (a[b] != -1) return a[b];

	vector<int> c;

	if (1 <= b) c.push_back(dp(b - 1) + 1);

	for (int i = 6; i <= b; i *= 6) c.push_back(dp(b - i) + 1);

	for (int i = 9; i <= b; i *= 9) c.push_back(dp(b - i) + 1);

	a[b] = *min_element(c.begin(), c.end());

	return a[b];

}



int main() {

	int N;

	cin >> N;

	cout << dp(N);

}

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

#include <iostream>

#include <vector>

using namespace std;



vector<int> a;



int dp(int b) {

	if (!b) return 0;

	if (a[b] != -1) return a[b];

	vector<int> c;

	c.push_back(dp(b - 1));

	for (int i = 6; i <= b; i *= 6) c.push_back(dp(b - i));

	for (int i = 9; i <= b; i *= 9) c.push_back(dp(b - i));

	a[b] = *min_element(c.begin(), c.end()) + 1;

	return a[b];

}



int main() {

	int N;

	cin >> N;

	a.resize(N + 1, -1);

	cout << dp(N);

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

#include <iostream>

#include <vector>

using namespace std;



vector<int> a(100001, -1);



int dp(int b) {

	if (!b) return 0;

	if (a[b] != -1) return a[b];

	vector<int> c;

	if (1 <= b) c.push_back(dp(b - 1) + 1);

	for (int i = 6; i <= b; i *= 6) c.push_back(dp(b - i) + 1);

	for (int i = 9; i <= b; i *= 9) c.push_back(dp(b - i) + 1);

	a[b] = *min_element(c.begin(), c.end());

	return a[b];

}



int main() {

	int N;

	cin >> N;

	cout << dp(N);

}

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

#include <climits>

#include <iostream>

#include <vector>

using namespace std;



vector<int> a;



int dp(int b) {

	if (a[b] != -1) return a[b];

	int c = INT_MAX;

	if (0 < b) c = dp(b - 1);

	for (int i = 6; i <= b; i *= 6) c = min(dp(b - i), c);

	for (int i = 9; i <= b; i *= 9) c = min(dp(b - i), c);

	a[b] = c + 1;

	return a[b];

}



int main() {

	int N;

	cin >> N;

	a.resize(N + 1, -1);

	a[0] = 0;

	cout << dp(N);

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

using namespace std;



const int MAX_N = 100010;

int memo[MAX_N];



int rec(int n){

    if(n == 0) return 0;

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



    int res = n;

    

    // 1, 6, 36, ...

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

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

    }



    // 1, 9, 81, ...

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

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

    }



    return memo[n] = res;

}



int main(){

    cin.tie(0);

    ios::sync_with_stdio(false);



    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.5927807999650452x):
#include <bits/stdc++.h>

using namespace std;



const int MAX = 100010;

const int INF = 1e9;

int dp[MAX]; // dp[i]: i円を払うのに必要な最小の紙幣の枚数



int main(){

    cin.tie(0);

    ios::sync_with_stdio(false);



    int n;

    cin >> n;



    vector<int> A;

    A.push_back(1);

    int t = 6;

    while(t <= MAX){

        A.push_back(t);

        t *= 6;

    }

    t = 9;

    while(t <= MAX){

        A.push_back(t);

        t *= 9;

    }

    

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



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

        dp[i] = INF;

    }



    dp[0] = 0;

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

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

            if(i - A[j] < 0) continue;

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

        }

    }



    cout << dp[n] << endl;



    return 0;

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

using namespace std;



const int MAX_N = 100010;

int memo[MAX_N];



int rec(int n){

    if(n == 0) return 0;

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



    int res = n;

    

    // 1, 6, 36, ...

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

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

    }



    // 1, 9, 81, ...

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

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

    }



    return memo[n] = res;

}



int main(){

    cin.tie(0);

    ios::sync_with_stdio(false);



    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.6373145788756351x):
#include <bits/stdc++.h>

using namespace std;



int dp[100010];

const int INF = 1e9;



int main(){

    cin.tie(0);

    ios::sync_with_stdio(false);



    int n;

    cin >> n;

    vector<int> A;

    A.push_back(1);

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

        A.push_back(i);

    }

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

        A.push_back(i);

    }



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



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

        dp[i] = INF;

    }



    dp[0] = 0;

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

        for(auto x : A){

            if(i < x) break;

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

        }

    }



    cout << dp[n] << endl;









    return 0;

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

#include <vector>

#include <algorithm>

#include <string>

#include <set>

#include <queue>

#include <stack>

#include <tuple>

#include <cmath>

#include <iomanip>

#include <map>

#include <stdio.h>

#include <cstring> //memset(dp,0,sizeof(dp))

#include <functional>

#define ll long long

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

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

#define fi first

#define se second

#define pb push_back

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

using namespace std;

template<typename T1,typename T2> inline void chmin(T1 &a,T2 b){if(a>b) a=b;}

template<typename T1,typename T2> inline void chmax(T1 &a,T2 b){if(a<b) a=b;}

typedef pair<int,int> P;

typedef pair<ll,ll> Pll;

//小数点以下　cout << fixed << setprecision(10) << num << endl;

//桁数はlog10(mid)でやれ　log(mid)/log(10)はWA

//long*long<0で判定するな

//開区間と閉区間

//アルファベットが何番目かは x=s[i]-'a'

//bfsはmain()内で　rep(i,h)rep(j,w)rep(i,4)でバグ

//切り上げ：(a+(b-1))/b

//32bit以上は 1ull<<j

// map 初期値0 for(auto itr=mp.begin();itr!=mp.end();++itr) itr->fi itr->se で全探索

// for(auto u:mp) u.fi u.se も可能

//n個のgcd,lcmは一つずつみて更新していく

//大きな数を扱うときは素因数分解した形で持っておく

// (l/a[i]) = l*powmod(a[i],MOD-2)%MOD;

//auto itr=lower_bound(ALL(a),key) a[itr-a.begin()]

const ll MOD = 1000000007;

const int MAX = 1001001;



int memo[MAX];

int rec(int n){

    if(n==0) return 0;

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

    int ret=n;

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

        int six=pow(6,i);

        int nine=pow(9,i);

        if(n-six>=0) chmin(ret,rec(n-six)+1);

        if(n-nine>=0) chmin(ret,rec(n-nine)+1);

    }

    return memo[n]=ret;

}



signed main(){

    int n; cin >> n;

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

    cout << rec(n) << endl;

}


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

#include <vector>

#include <algorithm>

#include <string>

#include <set>

#include <queue>

#include <stack>

#include <tuple>

#include <cmath>

#include <iomanip>

#include <map>

#include <cstring> //memset(dp,0,sizeof(dp))

#define ll long long

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

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

#define fi first

#define se second

#define pb push_back

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

using namespace std;

template<typename T1,typename T2> inline void chmin(T1 &a,T2 b){if(a>b) a=b;}

template<typename T1,typename T2> inline void chmax(T1 &a,T2 b){if(a<b) a=b;}

typedef pair<int,int> P;

typedef pair<ll,ll> Pll;



signed main(){

    int n; cin >> n;

    int dp[n+1];

    rep(i,n+1) dp[i]=1001001;

    dp[0]=0;

    rep(i,n+1){

        int k=1;

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

        k=6;

        while(i+k<=n){

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

            k*=6;

        }

        k=9;

        while(i+k<=n){

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

            k*=9;

        }

    }

    cout << dp[n] << endl;

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

#include<iomanip>

#include<algorithm>

#include<cmath>

#include<vector>

#include<string>

#include<bitset>

using namespace std;



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

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

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

#define PER(i,n) ROF(i,n-1,-1)



typedef int i32;

typedef long long int i64;

typedef unsigned long long int ui64;

typedef float f32;

typedef double f64;

typedef long double f128;



constexpr int MAX_N = 1e5+100;

int dp[MAX_N];



int main(){

    int N;cin>>N;

    REP(i,MAX_N){

        dp[i] = N;

    }

    dp[0] = 0;

    REP(i,N+1){

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

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

    }

    cout << dp[N];

}










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

#include<iomanip>

#include<algorithm>

#include<cmath>

#include<vector>

#include<string>

#include<bitset>

#include<queue>

using namespace std;



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

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

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

#define PER(i,n) ROF(i,n-1,-1)



int n_base_sum(int a,int n){

    int res = 0;

    while(a>0){

        res += a%n;

        a /= n;

    }

    return res;

}



int main(){

    int N;cin>>N;

    int ans=N;

    REP(i,N+1){

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

        ans = min(ans,tmp);

    }

    cout << ans;

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

#include<iomanip>

#include<algorithm>

#include<cmath>

#include<vector>

#include<string>

#include<bitset>

using namespace std;



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

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

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

#define PER(i,n) ROF(i,n-1,-1)



typedef int i32;

typedef long long int i64;

typedef unsigned long long int ui64;

typedef float f32;

typedef double f64;

typedef long double f128;



constexpr int MAX_N = 1e5+100;

int dp[MAX_N];



int main(){

    int N;

    cin >> N;

    REP(i,MAX_N){

        dp[i] = i;

    }

    REP(i,N+1){

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

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

    }

    cout << dp[N];

}




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

#include<iomanip>

#include<algorithm>

#include<cmath>

#include<vector>

#include<string>

#include<bitset>

#include<queue>

using namespace std;



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

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

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

#define PER(i,n) ROF(i,n-1,-1)



int n_base_sum(int a,int n){

    int res = 0;

    while(a>0){

        res += a%n;

        a /= n;

    }

    return res;

}



int main(){

    int N;cin>>N;

    int ans=N;

    REP(i,N+1){

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

        ans = min(ans,tmp);

    }

    cout << ans;

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

#include<iomanip>

#include<algorithm>

#include<cmath>

#include<vector>

#include<string>

#include<bitset>

#include<queue>

using namespace std;



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

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

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

#define PER(i,n) ROF(i,n-1,-1)



typedef int i32;

typedef long long int i64;

typedef unsigned long long int ui64;

typedef float f32;

typedef double f64;

typedef long double f128;



constexpr int MAX_N = 1e5+100;

int bfs[MAX_N];



int main(){

    int N;cin>>N;

    queue<int> q;

    REP(i,MAX_N){

        bfs[i] = -1;

    }

    bfs[0] = 0;

    q.push(0);

    while(!q.empty()){

        int n = q.front();q.pop();

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

            if(bfs[n+pow]==-1){

                bfs[n+pow] = bfs[n] + 1;

                q.push(n+pow);

            }

        }

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

            if(bfs[n+pow]==-1){

                bfs[n+pow] = bfs[n] + 1;

                q.push(n+pow);

            }

        }

    }

    cout << bfs[N];

}




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

#include<iomanip>

#include<algorithm>

#include<cmath>

#include<vector>

#include<string>

#include<bitset>

#include<queue>

using namespace std;



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

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

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

#define PER(i,n) ROF(i,n-1,-1)



int n_base_sum(int a,int n){

    int res = 0;

    while(a>0){

        res += a%n;

        a /= n;

    }

    return res;

}



int main(){

    int N;cin>>N;

    int ans=N;

    REP(i,N+1){

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

        ans = min(ans,tmp);

    }

    cout << ans;

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

#include<set>

#include<queue>

using namespace std;



const int limit = 100001;

int memo[limit];



int main() {

    int n;

    cin >> n;



    fill(memo, memo+limit, 1e9+1);



    set<int> st;

    st.insert(1); memo[0] = 0;

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

        st.insert(i);

    }

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

        st.insert(i);

    }



    queue<int> que;

    que.push(0);

    while(!que.empty()) {

        int p = que.front(); que.pop();

        for(auto e : st) {

            if(p + e <= n && memo[p + e] > memo[p] + 1) {

                memo[p + e] = memo[p] + 1;

                que.push(p + e);

            }

        }

    }



    cout << memo[n] << endl;



    return 0;

}

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

using namespace std;



int main() {

    int n;

    cin >> n;

    int ans = 1e9+1;

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

        int a = i, b = n - i;

        int cnt = 0;

        while(a > 0) cnt += a%6, a /= 6; 

        while(b > 0) cnt += b%9, b /= 9;

        ans = min(ans, cnt);

    }



    cout << ans << endl;



    return 0;

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

#include <algorithm>



using namespace std;



static const int MAX_N = 100010;



int memo[MAX_N];



int f(int n)

{



    if(n == 0) {

        return 0;

    }



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

        return memo[n];

    }



    int res = f(n-1) + 1;



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

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

    }



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

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

    }



    memo[n] = res;



    return res;

}



int main()

{

    int n;

    cin >> n;



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

        memo[i] = -1;

    }



    int ans = f(n);

    cout << ans << endl;



    return 0;

}


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

#include <algorithm>



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;



    // inf扱いでnを使用

    int ans = n;



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

        ans = min(ans, func(i, 6) + func(n-i, 9));

    }



    cout << ans << endl;



    return 0;

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

using namespace std;



int main()

{

  int N;

  cin >> N;

  vector<int> dp(100010,100010);

  dp[N] = 0;

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

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

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

      int a = (int)pow(6.0,(double)j);

      if(a<=i){

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

      }

    }

    for(int j=1;j<=5;j++){

      int a = (int)pow(9.0,(double)j);

      if(a<=i){

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

      }

    }

  }

  cout << dp[0] << endl;

  return 0;

}




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

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

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

//#define int long long

typedef long long ll;

const int MOD = 1000000007;

const int INF = 1010000000;

const double EPS = 1e-10;

using namespace std;

const pair<int,int> fd[] = {make_pair(1,0),make_pair(-1,0),make_pair(0,1),make_pair(0,-1)};



signed main() {

  int n;cin>>n;

  int dp[100001];

  dp[0] = 0;

  repd(i,1,n+1){

    dp[i] = dp[i-1]+1;

    int a = 6;

    while(a <= i){

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

      a *= 6;

    }

    a = 9;

    while(a <= i){

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

      a *= 9;

    }

  }

  cout << dp[n] << endl;

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

#include<cstdio>

#include<algorithm>

#include<cmath>

#include<map>

#include<cstring>

#include<climits>

#include<queue>

using namespace std;

#define MAXN 100000

int A,ans;

int main()

{

    scanf("%d",&A);

    int ans=A;

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

    {

        int cont=0;

        int t=i;

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

        t=A-i;

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

        ans=min(ans,cont);

    }

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

}


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

#include<cstring>

#include<algorithm>

using namespace std;

 

#define MAXN 100000

const int M[15]={0,1,6,9,36,81,216,729,1296,6561,7776,46656,59049};

int N,MaxD;

int dp[MAXN+5];

 

int main(){

    scanf("%d",&N);

    memset(dp,0x3f,sizeof dp);

    dp[0]=0;

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

        for(int j=M[i];j<=N;j++)

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

    printf("%d",dp[N]);

}