#include<cstdio>
#include<cstring>
int f[100005];
int min(int x,int y) {return x<y?x:y;}
int main()
{
	int n;scanf("%d",&n);
	memset(f,0x3f,sizeof(f));
	f[0]=0;
	for(int i=1;i<=n;i++)
	{
		for(int j=1;j<=i;j*=6) f[i]=min(f[i],f[i-j]+1); 
		for(int j=1;j<=i;j*=9) f[i]=min(f[i],f[i-j]+1); 
	}
	printf("%d\n",f[n]);
	return 0;
}