#include <iostream>
using namespace std;
int a[100001];
int main(){
  int n;cin>>n;
  for(int i=1;i<=n;i++){
    a[i]=a[i-1]+1;
    for(int j=6;j<=i;j*=6)
      a[i]=min(a[i],a[i-j]+1);
    for(int j=9;j<=i;j*=9)
      a[i]=min(a[i],a[i-j]+1);
  }
  cout<<a[n];
  return 0;
}