stablegradients/pie-gem5-pairs huggingface dataset:
// 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.242657644747646x):
/*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;

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

}

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

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

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

}

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

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

}


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

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

}

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

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

}


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

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

}

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

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

}

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

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

}

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

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

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

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

}

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

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

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

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

}

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

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

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

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