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

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

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

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

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

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

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

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

#define ll long long

#define pb(a) push_back(a)

#define INF 1000000000

const int MOD = 1000000009;



using namespace std;

typedef pair<int, int> P;

typedef pair<ll, ll> LP;

typedef pair<int, P> PP;

typedef pair<ll, LP> LPP;



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

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



const int MAX_N = 15;

const int MAX_W = 110000;

int dp[MAX_N][MAX_W];



int main(){

  cin.tie(0);

  ios::sync_with_stdio(false);

  int W;

  cin >> W;

  

  vector<int> weight;

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

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

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



  //DP初期条件

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

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



  //DPループ

  REP(i,n){

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

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

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

      }

  }



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

}

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

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

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

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

#define ll long long

using namespace std;

typedef pair<int, int> P;

typedef pair<ll, ll> LP;

typedef pair<int, P> PP;

typedef pair<ll, LP> LPP;

//typedef vector<unsigned int>vec;

typedef vector<ll>vec;

typedef vector<vec> mat;

typedef vector<vector<int>> Graph;



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

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

const int INF = 1000000000;

const ll LINF = 1000000000000000000;//1e18

const ll  MOD = 1000000007;

//const ll MOD = 998244353;

const double PI = acos(-1.0);

const double EPS = 1e-10;



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

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

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





int main(){

    cin.tie(0);

    ios::sync_with_stdio(false);

    int N;

    cin >> N;

    vector<int> six, nine;

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

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



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

    dp[0] = 0;

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

        REP(j, six.size()){

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

        }

        REP(j, nine.size()){

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

        }

    }

    cout << dp[N] << endl;

}
// Slower C++ solution:
#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;

}

// Faster C++ solution for the same problem (measured speedup: 6.136010575545811x):
#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;

}
// Slower C++ solution:
#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;

}

// Faster C++ solution for the same problem (measured speedup: 4.008297009694004x):
#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;

}
// Slower C++ solution:
/**

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

}


// Faster C++ solution for the same problem (measured speedup: 1.7713096378596687x):
/**

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

}
// Slower C++ solution:
#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);

}

// Faster C++ solution for the same problem (measured speedup: 1.3493692736343386x):
#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);

}
// Slower C++ solution:
#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;

}

// Faster C++ solution for the same problem (measured speedup: 2.4574553986578827x):
#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;

}
// Slower C++ solution:
#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;



}


// Faster C++ solution for the same problem (measured speedup: 1.2419413496361904x):
#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++)



int dp[100010];



int rec(int x){

  if (x == 0) return 0;

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



  int res = x;

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

    res = min(res,rec(x-pow6)+1);

  }

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

    res = min(res,rec(x-pow9) + 1);

  }

  return dp[x] = res;

}



int main(){

  int n;

  cin >> n;

  int ans;

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

  cout << rec(n) << endl;

  

  



  

  return 0;

  

}
// Slower C++ solution:
#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;



}


// Faster C++ solution for the same problem (measured speedup: 0.972079387890016x):
#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++)



int dp[100010];



int rec(int x){

  if (x == 0) return 0;

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



  int res = x;

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

    res = min(res,rec(x-pow6)+1);

  }

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

    res = min(res,rec(x-pow9) + 1);

  }

  return dp[x] = res;

}



int main(){

  int n;

  cin >> n;

  int ans;

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

  cout << rec(n) << endl;

  

  



  

  return 0;

  

}
// Slower C++ solution:
#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;



}


// Faster C++ solution for the same problem (measured speedup: 1.6754988737942726x):
#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;

  

}
// Slower C++ solution:
#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;



}


// Faster C++ solution for the same problem (measured speedup: 1.3114290140395584x):
#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;

  

}
// Slower C++ solution:
#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;



}


// Faster C++ solution for the same problem (measured speedup: 1.6347297939389043x):
#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 <= n;pow6*=6){

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

    }

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

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

    }

  }

  cout << dp[n] << endl;

  

  



  

  return 0;

  

}
// Slower C++ solution:
#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;



}


// Faster C++ solution for the same problem (measured speedup: 1.2795186648090935x):
#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 <= n;pow6*=6){

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

    }

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

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

    }

  }

  cout << dp[n] << endl;

  

  



  

  return 0;

  

}
// Slower C++ solution:
#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;

}


// Faster C++ solution for the same problem (measured speedup: 1.6300966589520578x):
#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;

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

using namespace std;



const int MAX_N = 110000;



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



int main() {

	int N;

	cin >> N;



	// 初期化

	for (int i = 0; i < MAX_N; ++i) dp[i] = N; // INF の気持ち

	dp[0] = 0;



	// 集める DP --- dp[n] に遷移を集める

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

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

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

		}

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

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

		}

	}



	cout << dp[N] << endl;

}

// Faster C++ solution for the same problem (measured speedup: 1.2763877112627922x):
#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;

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

#include <queue>

using namespace std;



const int MAX_N = 110000;



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



int main() {

	int N;

	cin >> N;



	// 初期化

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

	dp[0] = 0;

	queue<int> que;

	que.push(0);



	// BFS

	while (!que.empty()) {

		int v = que.front();

		que.pop();



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

			if (dp[v + pow6] == -1) {

				dp[v + pow6] = dp[v] + 1;

				que.push(v + pow6);

			}

		}

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

			if (dp[v + pow9] == -1) {

				dp[v + pow9] = dp[v] + 1;

				que.push(v + pow9);

			}

		}

	}



	cout << dp[N] << endl;

}

// Faster C++ solution for the same problem (measured speedup: 1.3601501507757778x):
#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;

}
// 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) {

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

			else dp[i + 1][w] = dp[i][w];

		}

	}



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

}

// Faster C++ solution for the same problem (measured speedup: 5.066689808831119x):
#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;

}