// Slower C++ solution:
#include<stdio.h>

#include<stdlib.h>

#include<math.h>



#define n 100000





void change(int dat,int ch[],int coin_value[],int vaild_coin)

{



    int i,j,tmp;

    int min = n+1;

    for(j = 0; j < vaild_coin; j++)

    {

        if(dat >= coin_value[j]){

            tmp = ch[dat - coin_value[j]] + 1;

        }else tmp = dat;



        if(min > tmp)//算硬幣數量

                min  = tmp;

    }

    ch[dat] = min;

}



int main()

{



    int i,j,k;

    int dat,six,nine;

    int *coin_value;

    int *ch;



    ch = (int *)malloc(n*sizeof(int));//錢

    /*for(i = 0;; i++) //製造錢幣

        if(pow(6,i) > n)

        {

            six = i;

            break;

        }*/

    for (i = 1, six = 0; i <= n; i *= 6, six++);

    for (i = 1, nine = 0; i <= n; i *= 9, nine++);



    /*for(i = 0;; i++) //製造錢幣

        if(pow(9,i) > n)

        {

            nine = i;

            break;

        }*/



    int total = six + nine;

    coin_value = (int *)malloc((total)*sizeof(int));

    coin_value[0] = 1;



    for (six = 6, nine = 9, i = 1; six <= n || nine <= n; i++) {

        if (six < nine) {

            coin_value[i] = six;

            six *= 6;

        } else {

            coin_value[i] = nine;

            nine *= 9;

        }

    }

    /*for(i = 1,j = 1,k = 1;; k++)

    {

        int tmps = pow(6,i);

        int tmpn = pow(9,j);

        if(tmps > tmpn) //6 > 9

        {

            if(tmpn < n) //9 < 100000 排序

            {

                coin_value[k] = tmpn;

                j++;

            }

        }

        else if(tmps < n)

        {

            if(tmps < n)

            {

                coin_value[k] = tmps;

                i++;

            }

        }

        else break;

    }*/

/*

    for (i = 0; i < total; i++)

        printf("%d: %ld\n", i, coin_value[i]);

    printf("%d\n", total);

*/

    int vaild_coin = total - 1;



    for(i = 0; i <= n; i++){



        if(i == 0){

            ch[i] = 0;

        }else{

            change(i,ch,coin_value,vaild_coin);

        }

    }



    //freopen("testdata2.in","r",stdin);



    while(scanf("%d",&dat) != EOF){

        printf("%d\n",ch[dat]);

    }



    free(coin_value);

    free(ch);



    return 0;

}


// Faster C++ solution for the same problem (measured speedup: 1.2032724466390587x):
#include <bits/stdc++.h>

 

using namespace std;



#define precise(n,k) fixed<<setprecision(k)<<n



typedef long long ll;

typedef long double ld;

typedef pair<int, int> pii;



vector<int> memo;



int solve(vector<int>& coins, int amount) {

    if(amount < 0) return INT_MAX;

    if(amount == 0) return 0;



    if(memo[amount] != -1) return memo[amount];



    int minimum = INT_MAX;

    int curr;



    for(int coin: coins) {

        curr = solve(coins, amount - coin);

        minimum = min(minimum, ((curr==INT_MAX)?INT_MAX:curr+1));

    }

    memo[amount] = minimum;

    return memo[amount];

}



int main() {

    int n;

    

    cin >> n;

    

    

    memo.resize(100000+1, -1);

    

    vector<int> coins = {531441, 59049, 6561, 729, 81, 279936, 46656,7776, 1296,216,36,  6,   9, 1};

    

    

    sort(coins.rbegin(), coins.rend());

    





    

    int answer = solve(coins, n);

    

    cout << ((answer==INT_MAX)?-1:answer) << endl;

    

   

    

	return 0;

}