#include<bits/stdc++.h>
using namespace std;
int dp[300100];
vector<int> v; 
int n;
int main(){
	//freopen("input.txt","r",stdin);
	//freopen("output.txt","w",stdout);
	ios::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	cin>>n;
	int seed=1;
	v.push_back(seed);
	seed*=6;
	while(seed<=n){
		v.push_back(seed);
		seed*=6;
	}
	seed=9;
	while(seed<=n){
		v.push_back(seed);
		seed*=9;
	}
	memset(dp,63,sizeof(dp));
	dp[0]=0;
	for(int i=0;i<=100000;i++){
		for(int j=0;j<v.size();j++){
			dp[i+v[j]]=min(dp[i+v[j]],dp[i]+1);
		}
	}
	cout<<dp[n]<<endl;
	return 0;
}
