#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

vector<int> six = {6,36,216,1296,7776,46656};
vector<int> nine = {9,81,729,6561,59049};

int main(){
    int n;
    cin >> n;
    int sum = 0;
    while(n >= 18){
        int nine_max = 1, six_max = 1;
        for(vector<int>::iterator i = nine.begin(); i < nine.end(); i++){
            if(n < *i) break;
            nine_max = *i;
        }
        for(vector<int>::iterator i = six.begin(); i < six.end(); i++){
            if(n < *i) break;
            six_max = *i;
        }
        n -= max(nine_max, six_max);
        sum++;
    }
    if(n < 6)
        cout << sum + n << endl;
    else if(n < 9)
        cout << sum + n - 6 + 1 << endl;
    else if(n < 12)
        cout << sum + n - 9 + 1 << endl;
    else
        cout << sum + n - 12 + 2 << endl;
    return 0;
}
