// 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: 2.115795694171815x):
#include <bits/stdc++.h>



using namespace std;



#define pb push_back

#define fs first

#define sc second

#define sz size

#define forn(i, n) for (ll i = 0; i < (ll) (n); ++i)

#define forni(i, n) for (ll i = 1; i <= (ll) (n); ++i)

#define IO ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0)



using ll = long long;

const ll inf = 1e9+7; 



const double PI =  3.141592653589793;



int main(){

	IO;

	ll n;

	cin>>n;

	ll yen[]={1,6,9,36,216,1296,7776,46656,81,729,6561,59049};

	ll dp[n];

	dp[0]=0;

	forni(i,n){

		dp[i]=inf;

		for(auto c : yen){

			if(i-c>=0) dp[i] = min(dp[i],dp[i-c]+1);

		}

	}

	cout<<dp[n]<<"\n";

	return 0;

}



// _   .-')                .-. .-')                 .-')                                               

//( '.( OO )_              \  ( OO )               ( OO ).                                             

// ,--.   ,--.).-'),-----. ,--. ,--.  .-'),-----. (_)---\_) ,--. ,--.     .-----.  ,-.-')  .-'),-----. 

// |   `.'   |( OO'  .-.  '|  .'   / ( OO'  .-.  '/    _ |  |  | |  |    '  .--./  |  |OO)( OO'  .-.  '

// |         |/   |  | |  ||      /, /   |  | |  |\  :` `.  |  | | .-')  |  |('-.  |  |  \/   |  | |  |

// |  |'.'|  |\_) |  |\|  ||     ' _)\_) |  |\|  | '..`''.) |  |_|( OO )/_) |OO  ) |  |(_/\_) |  |\|  |

// |  |   |  |  \ |  | |  ||  .   \    \ |  | |  |.-._)   \ |  | | `-' /||  |`-'| ,|  |_.'  \ |  | |  |

// |  |   |  |   `'  '-'  '|  |\   \    `'  '-'  '\       /('  '-'(_.-'(_'  '--'\(_|  |      `'  '-'  '

// """    """     """""""" """  """"      """""""  """""""  """"""""      """"""   """         """""""