#include<bits/stdc++.h>
#define FOR(i,a,b) for(int i=a;i<b;++i)
#define ROF(i,a,b) for(int i=a;i>=b;--i)
#define vi vector<int>
#define vlli vector<long long int>
#define pb push_back
#define mp make_pair
#define all(c)  ((c).begin()), ((c).end())
using namespace std;
int main(){
	ios::sync_with_stdio(0);
	int n;
	cin>>n;
	vector<int> t;
	t.pb(1);
	int i=1;
	vector<int> dp(n+1);
	dp[1]=1;
	while(pow(6,i)<=n){
		int x=pow(6,i);
		dp[x]=1;
		t.pb(x);
		i++;
	}
	i=1;
	while(pow(9,i)<=n){
		int x=pow(9,i);
		dp[x]=1;
		t.pb(x);
		i++;
	}
	int m=t.size();
	FOR(i,2,n+1){
		if(!dp[i]){
			int min_change=INT_MAX;
			FOR(j,0,m){
				if(t[j]<i)
					min_change=min(min_change,dp[i-t[j]]);
			}
			dp[i]=min_change+1;
		}
		//cout<<i<<" "<<dp[i]<<"\n";
	}
	cout<<dp[n]<<"\n";
}
