stablegradients/pie-gem5-bysrc huggingface dataset:
// Original C++ solution (slower):
// 個数制限なしナップサック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;

}

// Optimized solution to the same problem (faster):
#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;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#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;

}

// Optimized solution to the same problem (faster):
#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;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#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();

}


// Optimized solution to the same problem (faster):
#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;

}


// Fastest known solution for this problem:
// Original C++ solution (slower):
#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;

}






// Optimized solution to the same problem (faster):
#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;

}






// Fastest known solution for this problem:
// Original C++ solution (slower):
#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;

}

// Optimized solution to the same problem (faster):
#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;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#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;

}

// Optimized solution to the same problem (faster):
#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;

}


// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <algorithm>

#include <complex>

#include <iostream>

#include <string>

#include <vector>

 

using namespace std;

 

vector<int> memo(1000000, -1);

 

int recursion(const int n) {

  if (n < 0) {

    return std::numeric_limits<int>::max();

  }

  if (n == 0) {

    return 0;

  }

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

    return memo[n];

  }

  vector<int> count;

  for (auto draw :

       {1, 6, 9, 36, 81, 216, 729, 1296, 6561, 7776, 46656, 59049}) {

    count.push_back(recursion(n - draw));

  }

  memo[n] = *min_element(count.begin(), count.end()) + 1;

  return memo[n];

}

 

int main() {

  int n;

  cin >> n;

  cout << recursion(n) << endl;

}

// Optimized solution to the same problem (faster):
#include <bits/stdc++.h>

using namespace std;

int main()

{

	int N;

	cin >> N;

	

	int min = N;

	for( int n1 = 0; n1 < 9 && n1 <= N/9 && min > n1; n1++ ) {

	//	cout << "n1=" << n1 << endl;

	for( int n2 = 0; n2 < 9 && n2 <= (N-9*n1)/81 && min > (n1+n2); n2++ ) {

	//	cout << "n2=" << n2 << endl;

	for( int n3 = 0; n3 < 9 && n3 <= (N-9*n1-81*n2)/729 && min > (n1+n2+n3); n3++ ) {

	//	cout << "n3=" << n3 << endl;

	for( int n4 = 0; n4 < 9 && n4 <= (N-9*n1-81*n2-729*n3)/6561 && min > (n1+n2+n3+n4); n4++ ) {

	//	cout << "n4=" << n4 << endl;

	for( int n5 = 0; n5 < 9 && n5 <= (N-9*n1-81*n2-729*n3-6561*n4)/59049 && min > (n1+n2+n3+n4+n5); n5++ ) {

	//	cout << "n5=" << n5 << endl;

		int z = (N-9*n1-81*n2-729*n3-6561*n4-59049*n5);

		//cout << "z=" << z << endl;

		int s6 = (z / 46656); z -= s6*46656;

		int s5 = (z / 7776); z -= s5*7776;

		int s4 = (z / 1296); z -= s4*1296;

		int s3 = (z / 216); z -= s3*216;

		int s2 = (z / 36); z -= s2*36;

		int s1 = (z / 6); z -= s1*6;

		int t = n1+n2+n3+n4+n5+s1+s2+s3+s4+s5+s6+z;

		if( t < min ) min = t;

	}

	}

	}

	}

	}

	cout << min << endl;

}


// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <cstdio>

#include <vector>

#include <algorithm>

using namespace std;



int V[100001] = {};

int n, k;



void base(int n, int k, vector<int>& vM) {

	int a = k;

	while (k < n) {

		vM.push_back(k);

		k *= a;

	}

	vM.push_back(k);

}



int main()

{

	scanf("%d", &n);



	vector<int> vM;

	vM.reserve(20);

	base(n, 6, vM);

	base(n, 9, vM);

	vM.push_back(1);

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



	int s = vM.size();

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

		int min = 100;

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

			k = i - vM[j];

			if (k < 0) break;			

			if (min > V[k])

				min = V[k];

		}

		V[i] = ++min;

	}



	printf("%d", V[n]);

	return 0;

}

// Optimized solution to the same problem (faster):
#include <cstdio>

#include <vector>

#include <algorithm>

using namespace std;



int V[100001] = {};

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

int n, k;



int main()

{

	scanf("%d", &n);



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

		int min = 100;

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

			k = i - M[j];

			if (k < 0) break;			

			if (min > V[k])

				min = V[k];

		}

		V[i] = ++min;

	}



	printf("%d", V[n]);

	return 0;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
/**

 * code generated by JHelper

 * More info: https://github.com/AlexeyDmitriev/JHelper

 * @author

 */



#include <algorithm>

#include <cmath>

#include <fstream>

#include <functional>

#include <iostream>

#include <iomanip>

#include <iterator>

#include <queue>

#include <set>

#include <stack>

#include <string>

#include <tuple>

#include <utility>

#include <vector>



#define ll long long int

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

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



using namespace std;





class CStrangeBank {

public:

    ll N;

    vector<ll> t = vector<ll>(100001, -1);



    void solve(std::istream& in, std::ostream& out) {

        in >> N;

        out << dfs(N) << endl;

    }



    ll dfs(int n) {

        if (n < 0) return 100000;

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

        ll m = n;

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

            m = min(m, dfs(n - pow(9, i)) + 1);

        }

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

            m = min(m, dfs(n - pow(6, i)) + 1);

        }

        t[n] = m; return m;

    }

};





int main() {

    CStrangeBank solver;

    std::istream& in(std::cin);

    std::ostream& out(std::cout);

    solver.solve(in, out);

    return 0;

}


// Optimized solution to the same problem (faster):
/**

 * code generated by JHelper

 * More info: https://github.com/AlexeyDmitriev/JHelper

 * @author toshibablack

 */



#include <algorithm>

#include <bitset>

#include <cctype>

#include <cmath>

#include <fstream>

#include <functional>

#include <iostream>

#include <iomanip>

#include <iterator>

#include <queue>

#include <set>

#include <stack>

#include <string>

#include <tuple>

#include <utility>

#include <vector>



#include "string.h"



#define ll long long int

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

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

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

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

#define COUT(x) out << (x) << endl;



const int INF = 1e9;

const int MOD = 1e9+7;

const ll LINF = 1e18;



using namespace std;





class CStrangeBank {

public:

    int N;

    int dp[150000];



    void solve(std::istream& in, std::ostream& out) {

        in >> N;

        memset(dp, 0x3F, sizeof(dp));

        dp[0] = 0;

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

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

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

        }

        COUT(dp[N]);

    }

};





int main() {

    CStrangeBank solver;

    std::istream& in(std::cin);

    std::ostream& out(std::cout);

    solver.solve(in, out);

    return 0;

}


// Fastest known solution for this problem:
// Original C++ solution (slower):
#include<cstdio>

#include<cmath>

int m=99,n,w[12]={1,6,9,36,81,216,729,1296,6561,7776,46656,59049};

void dfs(int p,int s,int c)

{

	if(!(s^n))m=fmin(m,c);

	else if(p--)for(int i=0;i<6&&s+w[p]*i<=n;++i)dfs(p,s+w[p]*i,c+i);

}

int main()

{

	scanf("%d",&n);

	dfs(12,0,0);

	printf("%d",m);

}

// Optimized solution to the same problem (faster):
#include<cstdio>

int m=99,n,w[12]={1,6,9,36,81,216,729,1296,6561,7776,46656,59049};

void dfs(int p,int s,int c)

{

	if(!(s^n))m=m<c?m:c;

	else if(p--)for(int i=0;i<6&&s+w[p]*i<=n;++i)dfs(p,s+w[p]*i,c+i);

}

int main()

{

	scanf("%d",&n);

	dfs(12,0,0);

	printf("%d",m);

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <bits/stdc++.h>

using namespace std;



int main() {

  int N; cin >> N;

  vector<int> A(100010, 1000000000);

  A.at(0) = 0;

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

    A.at(i+1) = min(A.at(i+1), A.at(i) + 1);

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

      A.at(i + pow(6, j)) = min(A.at(i + pow(6, j)), A.at(i) + 1);

    }

    for (int k = 1; i + pow(9, k) <= N; k++) {

      A.at(i + pow(9, k)) = min(A.at(i + pow(9, k)), A.at(i) + 1);

    }

  }

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

}

// Optimized solution to the same problem (faster):
#include <bits/stdc++.h>

using namespace std;



int main() {

  int N; cin >> N;

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

  dp.at(0) = 0;

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

    dp.at(i+1) = min(dp.at(i+1), dp.at(i) + 1);

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

      dp.at(i + j) = min(dp.at(i + j), dp.at(i) + 1);

    }

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

      dp.at(i + k) = min(dp.at(i + k), dp.at(i) + 1);

    }

  }

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

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <iostream>

#include <string>

#include <vector>

#include <algorithm>

#include <utility>



using namespace std;

typedef long long ll;

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

int memo[100001];



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 < 100001;i++) memo[i] = -1;

    

    cout << rec(n) << endl;

    



    return 0;



}


// Optimized solution to the same problem (faster):
#include <iostream>

#include <string>

#include <vector>

#include <algorithm>

#include <utility>

#include <map>

using namespace std;

typedef long long ll;

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

const int INF = 1e9;

int dp[100010];



int main(){

  int n;

  cin >> n;

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

  dp[0] = 0;

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

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

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

    }

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

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

    }

  }

  cout << dp[n] << endl;

  

  



  

  return 0;

  

}


// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <iostream>

#include <string>

#include <vector>

#include <algorithm>

#include <utility>



using namespace std;

typedef long long ll;

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



int dp[100001];



int main(){

    int n;

    cin >> n;

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

    dp[0] = 0;

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

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

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

    }

    cout << dp[n] << endl;

    



    return 0;



}


// Optimized solution to the same problem (faster):
#include <iostream>

#include <string>

#include <vector>

#include <algorithm>

#include <utility>

#include <map>

using namespace std;

typedef long long ll;

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

const int INF = 1e9;

int dp[100010];



int main(){

  int n;

  cin >> n;

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

  dp[0] = 0;

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

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

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

    }

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

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

    }

  }

  cout << dp[n] << endl;

  

  



  

  return 0;

  

}


// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <iostream>

using namespace std;



const int MAX_N = 110000;



int memo[MAX_N]; // memo[i] が i についての答え

int rec(int n) {

	if (n == 0) return 0; // 終端条件

	if (memo[n] != -1) return memo[n]; // すでに探索済みならリターン



	int res = n; // INF の気持ち



	// 1, 6, 6^2, ..., を試す (n を超えない範囲で)

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



	// 1, 9, 9^2, ..., を試す (n を超えない範囲で)

	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;



	// テーブルを -1 で初期化

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

	cout << rec(N) << endl;

}


// Optimized solution to the same problem (faster):
#include <iostream>

using namespace std;



// a 進法で n の桁和

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; // INF

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

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

		res = min(res, tmp);

	}



	cout << res << endl;

}

// Fastest known solution for this problem: