#include <stdio.h>
#include <iostream>
#include <algorithm> 
using namespace std;
int main(){
	int i,j,k,l,m,n,ans=1000000;
	scanf("%d",&n);
	for(i=0;i<=n;i++){
	j=i;
	k=n-i;
	l=0;
	while(j){
	l+=j%6;
	j/=6;
	}
		while(k){
	l+=k%9;
	k/=9;
	}
	if(l<ans)ans=l;
	}
	printf("%d",ans);
	return 0;
	
} 