#include <iostream>
#define N 100001
using namespace std;

int d[N]={0};

int main(){
 int n,i,m;
 cin >> n;
 for(i=1;i<=n;i++){
   d[i]=d[i-1]+1;
   m=6;
   while(i>=m)
   {
       if(d[i]>d[i-m]){d[i]=d[i-m]+1;}
       m=m*6;
   }
   m=9;
   while(i>=m)
   {
       if(d[i]>d[i-m]){d[i]=d[i-m]+1;}
       m=m*9;
   }
 } 
 cout << d[n] << endl;
 return 0;
}