#include <bits/stdc++.h>
using namespace std;

int main(){
    int n; cin>>n;
    int dp[100001];
    dp[0]=0;
    for(int i=1;i<=n;i++){
        int m=dp[i-1]+1;
        for(int j=6;j<=i;j*=6){
            m=min(m,dp[i-j]+1);
        }
        for(int j=9;j<=i;j*=9){
            m=min(m,dp[i-j]+1);
        }
        dp[i]=m;
    }
    cout<<dp[n]<<endl;
}
