stablegradients/pie-gem5-bysrc huggingface dataset:
// Original C++ solution (slower):
#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];

}




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

}




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

}

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

}

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

}


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

}


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

}




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

}


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

}


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

int main()

{

	int N;

	scanf("%d",&N);

	int ans=N;

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

	{

		int num=0,t=i;

		while(t) num+=t%6,t/=6;

		t=N-i;

		while(t) num+=t%9,t/=9;

		if(num<ans) ans=num;

	}

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

	return 0;

}

/*#include<cstdio>

int N,C;

int t[3][35],d[35][35];

//t[i][j]:余数为i,颜色为j的数量

int main()

{

	int ans=0x3f3f3f3f;

	scanf("%d %d",&N,&C);

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

		for(int j=1;j<=C;j++)

			scanf("%d",&d[i][j]);

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

		for(int j=1;j<=N;j++)

		{

			int x;

			scanf("%d",&x);

			t[(i+j)%3][x]++;

		}

	int res=0;

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

		for(int j=1;j<=C;j++)//余数为i的格子全部变成颜色j

			for(int k=1;k<=C;k++)

				res+=t[i][k]*d[k][i];

}*/

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

using namespace std;

vector <long long> monedas;

long long memoria [100005];

int main ()

{

    ios_base::sync_with_stdio(0);

    cin.tie(0);

    long long c;

    long long res=0;

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

    cin>>c;

    monedas.push_back(1);

    queue <pair <long long,long long> > que;

    que.push({1,1});

    memoria [0]=0;

    for (long long i=0, a=9,b=6;i<6;++i,a*=9,b*=6)

    {

        if (i==5)

        {

            monedas.push_back(b);

            que.push({b,1});

            continue;

        }

        monedas.push_back(a);

        que.push({a,1});

        monedas.push_back(b);

        que.push({b,1});

    }

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

    while (!que.empty())

    {

        long long valor=que.front().first;

        long long costo=que.front().second;

        que.pop();

        if (valor==c)

        {

             printf("%lld\n",costo);

             return 0;

        }

        if (memoria[valor]!=-1)

            continue;

        memoria[valor]=costo;

        for (int i=0;i<monedas.size();i++)

        {

            if (monedas[i]+valor>c)

            {

                break;

            }

            if ( memoria[monedas[i]+valor]==-1)

            {

               que.push({monedas[i]+valor,costo+1});

            }

        }

 

    }

 

    //cout<<memoria[c]<<endl;

    return 0;

}

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

int main()

{

	int N;

	scanf("%d",&N);

	int ans=N;

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

	{

		int num=0,t=i;

		while(t) num+=t%6,t/=6;

		t=N-i;

		while(t) num+=t%9,t/=9;

		if(num<ans) ans=num;

	}

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

	return 0;

}

/*#include<cstdio>

int N,C;

int t[3][35],d[35][35];

//t[i][j]:余数为i,颜色为j的数量

int main()

{

	int ans=0x3f3f3f3f;

	scanf("%d %d",&N,&C);

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

		for(int j=1;j<=C;j++)

			scanf("%d",&d[i][j]);

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

		for(int j=1;j<=N;j++)

		{

			int x;

			scanf("%d",&x);

			t[(i+j)%3][x]++;

		}

	int res=0;

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

		for(int j=1;j<=C;j++)//余数为i的格子全部变成颜色j

			for(int k=1;k<=C;k++)

				res+=t[i][k]*d[k][i];

}*/

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

}

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

int main()

{

	int N;

	scanf("%d",&N);

	int ans=N;

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

	{

		int num=0,t=i;

		while(t) num+=t%6,t/=6;

		t=N-i;

		while(t) num+=t%9,t/=9;

		if(num<ans) ans=num;

	}

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

	return 0;

}

/*#include<cstdio>

int N,C;

int t[3][35],d[35][35];

//t[i][j]:余数为i,颜色为j的数量

int main()

{

	int ans=0x3f3f3f3f;

	scanf("%d %d",&N,&C);

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

		for(int j=1;j<=C;j++)

			scanf("%d",&d[i][j]);

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

		for(int j=1;j<=N;j++)

		{

			int x;

			scanf("%d",&x);

			t[(i+j)%3][x]++;

		}

	int res=0;

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

		for(int j=1;j<=C;j++)//余数为i的格子全部变成颜色j

			for(int k=1;k<=C;k++)

				res+=t[i][k]*d[k][i];

}*/

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

#include<cstdio>

#include<cstring>

#include<algorithm>

using namespace std;

int n,dp[100005];

int DP(int x)

{

	if(x<0) return 1e8;

	if(x==0) return 0;

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

	dp[x]=1e8;

	for(int i=6;i<=x;i*=6)

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

	for(int i=9;i<=x;i*=9)

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

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

	return dp[x];

}

int main()

{

	scanf("%d",&n);

	memset(dp,-1,sizeof dp);

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

	return 0;

}


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

int main()

{

	int N;

	scanf("%d",&N);

	int ans=N;

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

	{

		int num=0,t=i;

		while(t) num+=t%6,t/=6;

		t=N-i;

		while(t) num+=t%9,t/=9;

		if(num<ans) ans=num;

	}

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

	return 0;

}

/*#include<cstdio>

int N,C;

int t[3][35],d[35][35];

//t[i][j]:余数为i,颜色为j的数量

int main()

{

	int ans=0x3f3f3f3f;

	scanf("%d %d",&N,&C);

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

		for(int j=1;j<=C;j++)

			scanf("%d",&d[i][j]);

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

		for(int j=1;j<=N;j++)

		{

			int x;

			scanf("%d",&x);

			t[(i+j)%3][x]++;

		}

	int res=0;

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

		for(int j=1;j<=C;j++)//余数为i的格子全部变成颜色j

			for(int k=1;k<=C;k++)

				res+=t[i][k]*d[k][i];

}*/

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

#define ff first

#define ss second

using namespace std;

typedef long long ll;



ll n;

ll ans = 0;



int main(){

    cin >> n;

    ans = n;

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

        ll cnt = 0;

        ll prt = i;

        while(prt > 0){

            cnt += prt%6;

            prt /= 6;

        }



        prt = n-i;

        while(prt > 0){

            cnt += prt%9;

            prt /= 9;

        }

        ans = min(ans, cnt);



    }

    cout<< ans << endl;

    return 0;

}


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

int main()

{

	int N;

	scanf("%d",&N);

	int ans=N;

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

	{

		int num=0,t=i;

		while(t) num+=t%6,t/=6;

		t=N-i;

		while(t) num+=t%9,t/=9;

		if(num<ans) ans=num;

	}

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

	return 0;

}

/*#include<cstdio>

int N,C;

int t[3][35],d[35][35];

//t[i][j]:余数为i,颜色为j的数量

int main()

{

	int ans=0x3f3f3f3f;

	scanf("%d %d",&N,&C);

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

		for(int j=1;j<=C;j++)

			scanf("%d",&d[i][j]);

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

		for(int j=1;j<=N;j++)

		{

			int x;

			scanf("%d",&x);

			t[(i+j)%3][x]++;

		}

	int res=0;

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

		for(int j=1;j<=C;j++)//余数为i的格子全部变成颜色j

			for(int k=1;k<=C;k++)

				res+=t[i][k]*d[k][i];

}*/

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

using namespace std;



const int maxn=1e5+100;



int dp[maxn],n;

vector <int> v;



int main(){

	scanf("%d",&n);

	int u=1;

	while(u*6<=n){

		u*=6;

		v.push_back(u);

	}

	u=1;

	while(u*9<=n){

		u*=9;

		v.push_back(u);

	}

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

	reverse(v.begin(),v.end());

	v.push_back(1);

	for(int i=0;i<maxn;i++) dp[i]=1e9+10;

	dp[n]=0;

	for(int i=0;i<v.size();i++){

		for(int j=n;j>=0;j--){

			if(v[i]<=j){

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

			}

		}

	}

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

	return 0;

}

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



using namespace std;



#define pb push_back

#define fs first

#define sc second

#define sz size

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

#define forni(i, n) for (ll i = 1; i <= (ll) (n); ++i)

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



using ll = long long;

const ll inf = 1e9+7; 



const double PI =  3.141592653589793;



int main(){

	IO;

	ll n;

	cin>>n;

	ll yen[]={1,6,9,36,216,1296,7776,46656,81,729,6561,59049};

	ll dp[n];

	dp[0]=0;

	forni(i,n){

		dp[i]=inf;

		for(auto c : yen){

			if(i-c>=0) dp[i] = min(dp[i],dp[i-c]+1);

		}

	}

	cout<<dp[n]<<"\n";

	return 0;

}



// _   .-')                .-. .-')                 .-')                                               

//( '.( OO )_              \  ( OO )               ( OO ).                                             

// ,--.   ,--.).-'),-----. ,--. ,--.  .-'),-----. (_)---\_) ,--. ,--.     .-----.  ,-.-')  .-'),-----. 

// |   `.'   |( OO'  .-.  '|  .'   / ( OO'  .-.  '/    _ |  |  | |  |    '  .--./  |  |OO)( OO'  .-.  '

// |         |/   |  | |  ||      /, /   |  | |  |\  :` `.  |  | | .-')  |  |('-.  |  |  \/   |  | |  |

// |  |'.'|  |\_) |  |\|  ||     ' _)\_) |  |\|  | '..`''.) |  |_|( OO )/_) |OO  ) |  |(_/\_) |  |\|  |

// |  |   |  |  \ |  | |  ||  .   \    \ |  | |  |.-._)   \ |  | | `-' /||  |`-'| ,|  |_.'  \ |  | |  |

// |  |   |  |   `'  '-'  '|  |\   \    `'  '-'  '\       /('  '-'(_.-'(_'  '--'\(_|  |      `'  '-'  '

// """    """     """""""" """  """"      """""""  """""""  """"""""      """"""   """         """""""


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

 

using namespace std;



#define precise(n,k) fixed<<setprecision(k)<<n



typedef long long ll;

typedef long double ld;

typedef pair<int, int> pii;



vector<int> memo;



int solve(vector<int>& coins, int amount) {

    if(amount < 0) return INT_MAX;

    if(amount == 0) return 0;



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



    int minimum = INT_MAX;

    int curr;



    for(int coin: coins) {

        curr = solve(coins, amount - coin);

        minimum = min(minimum, ((curr==INT_MAX)?INT_MAX:curr+1));

    }

    memo[amount] = minimum;

    return memo[amount];

}



int main() {

    int n;

    

    cin >> n;

    

    

    memo.resize(100000+1, -1);

    

    vector<int> coins = {531441, 59049, 6561, 729, 81, 279936, 46656,7776, 1296,216,36,  6,   9, 1};

    

    

    sort(coins.rbegin(), coins.rend());

    





    

    int answer = solve(coins, n);

    

    cout << ((answer==INT_MAX)?-1:answer) << endl;

    

   

    

	return 0;

}

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



using namespace std;



#define pb push_back

#define fs first

#define sc second

#define sz size

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

#define forni(i, n) for (ll i = 1; i <= (ll) (n); ++i)

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



using ll = long long;

const ll inf = 1e9+7; 



const double PI =  3.141592653589793;



int main(){

	IO;

	ll n;

	cin>>n;

	ll yen[]={1,6,9,36,216,1296,7776,46656,81,729,6561,59049};

	ll dp[n];

	dp[0]=0;

	forni(i,n){

		dp[i]=inf;

		for(auto c : yen){

			if(i-c>=0) dp[i] = min(dp[i],dp[i-c]+1);

		}

	}

	cout<<dp[n]<<"\n";

	return 0;

}



// _   .-')                .-. .-')                 .-')                                               

//( '.( OO )_              \  ( OO )               ( OO ).                                             

// ,--.   ,--.).-'),-----. ,--. ,--.  .-'),-----. (_)---\_) ,--. ,--.     .-----.  ,-.-')  .-'),-----. 

// |   `.'   |( OO'  .-.  '|  .'   / ( OO'  .-.  '/    _ |  |  | |  |    '  .--./  |  |OO)( OO'  .-.  '

// |         |/   |  | |  ||      /, /   |  | |  |\  :` `.  |  | | .-')  |  |('-.  |  |  \/   |  | |  |

// |  |'.'|  |\_) |  |\|  ||     ' _)\_) |  |\|  | '..`''.) |  |_|( OO )/_) |OO  ) |  |(_/\_) |  |\|  |

// |  |   |  |  \ |  | |  ||  .   \    \ |  | |  |.-._)   \ |  | | `-' /||  |`-'| ,|  |_.'  \ |  | |  |

// |  |   |  |   `'  '-'  '|  |\   \    `'  '-'  '\       /('  '-'(_.-'(_'  '--'\(_|  |      `'  '-'  '

// """    """     """""""" """  """"      """""""  """""""  """"""""      """"""   """         """""""


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

typedef long long ll;

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

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

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

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

#define pb push_back

#define mp make_pair

#define INF 2000000000

#define MOD 1000000007

using namespace std;

typedef pair<int,int> P;

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

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

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;

ll dp[600000], val[] = {1, 6, 9, 36, 81, 216, 729, 1296, 6561, 7776, 46656, 59049};



ll solve(ll x) {

    if (dp[x] >= 0) return dp[x];

    ll t = INF;

    for (ll k : val) {

        if (x < k) break;

        t = min(solve(x-k) + 1, t);

    }

    return dp[x] = t;

}

int main() {

 ios::sync_with_stdio ( false );

 cin.tie ( 0 );

    cin >> n;

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

    dp[0] = 0;

    cout << solve(n) << endl;

    return 0;

}


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

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

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

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

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

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

#define pb push_back

#define mp make_pair

#define INF 100100100100

#define inf 2001001001

#define MOD 1000000007

#define ALL(x) begin(x),end(x)

using namespace std;

using ll = long long;

using pii = pair<int,int>;

using vii = vector<int>;

using vll = vector<ll>;

template<class T>using vv = vector<T>;

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

template<class T>void show(const vector<T> &a) { for (int i = 0; i < (int)a.size(); ++i) cout << a[i] << (i != (int)a.size()-1 ? " " : "\n"); }

template<class T>T gcd(T a, T b){ return b ? gcd(b, a % b) : a; }

template<class T>T lcm(T a, T b){ return a / gcd(a, b) * b; }

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

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

int n;

int dp[100005];



int rec(int p) {

  if (p == 0) return 0;

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

  int res = inf;

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

  for (int i = 1; i <= p; i *= 9) res = min(res, rec(p - i) + 1);

  return dp[p] = res;

}



int main() {

  cin.tie(0);

  ios::sync_with_stdio(false);

    cin >> n;

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

    cout << rec(n) << endl;

    return 0;

}




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

using namespace std;



const int MAX_N = 110000;

int memo[MAX_N];



int rec(int n){

    if (n==0) return 0;

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



    int res = n;



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

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



    return memo[n] = res;

}

int main() {

    int N; cin >> N;



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

    cout << rec(N) << endl;

    return 0;

}

// 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 int MAX_N = 110000;

int dp[MAX_N];



int main() {

    int N; cin >> N;



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

    dp[0] = 0;

    

    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;

    return 0;

}

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

using pi = pair<int, int>;

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



const int MAX_N = 110000;

int dp[MAX_N]; 



int main() {

    int N;  cin >> N;



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

    dp[0] = 0;

    priority_queue<pi, vector<pi>, greater<pi>> que;

    que.push(make_pair(0, 0));



    while (!que.empty()) {

        int cur = que.top().first; // 現在の距離

        int v = que.top().second; // 現在のノード

        que.pop();



        if (cur > dp[v]) continue;



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

            int nv = v + pow6; 

            if (chmin(dp[nv], dp[v] + 1)) que.push(make_pair(dp[nv], nv));

        }

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

            int nv = v + pow9; 

            if (chmin(dp[nv], dp[v] + 1)) que.push(make_pair(dp[nv], nv));

        }

    }



    cout << dp[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: