#include <bits/stdc++.h>
using namespace std;
int main()
{
    int t,a,b,x,y,z,n,mx,mn,tmp,res=1;
    scanf("%d",&x);
    int cnt=0,ans=0;
    string s="",s2="";
    vector<int>vc1,vc2,vc3;
    map<int,int>mp;
    map<char,int>mc;
    tmp=1;
    while(1){
        if(tmp*6>100000){
            break;
        }
        tmp=tmp*6;
        vc1.push_back(tmp);
    }
    tmp=1;
    while(1){
        if(tmp*9>100000){
            break;
        }
        tmp=tmp*9;
        vc1.push_back(tmp);
    }
    sort(vc1.begin(),vc1.end());
    //printf("siz=%d\n",vc1.size());
    vc2.push_back(x);
    for(int i=vc1.size()-1;i>=0;i--){
        //printf("x=%d vc1=%d x/vc1=%d x%%vc1=%d\n",x,vc1[i],x/vc1[i],x%vc1[i]);
        if(x/vc1[i]!=0){
            //
            cnt=cnt+x/vc1[i];
            x=x%vc1[i];
            vc2.push_back(x);
            //printf("cnt=%d\n",cnt);
        }
    }
    if(x!=0){
        if(x==5){
            cnt-=2;
        }
        cnt+=x;
    }
    printf("%d\n",cnt);
    for(int i=0;i<vc1.size();i++){
        //printf("%d\n",vc1[i]);
    }
    for(int i=vc2.size()-1;i>=0;i--){
        //printf("%d\n",vc2[i]);
    }



    return 0;
}


