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

vector <int> money ;
int dp [ 100001 ][ 20 ];

int main ( ) {
    int a = 1 ;
    while (a*6 < 100001) {
        money.push_back ( a*6 );
        a=a*6;
    }
    a = 1 ;
    money.push_back (a);
    while ( a*9 < 100001) {
        money.push_back( a*9 );
        a=a*9;
    }
    int n;
    cin >> n;

    cout << "-1";
    return 0;
    for ( int i = money.size()-1 ; i>= 0 ;i-- ) {
        for ( int j = 0 ; j <= n ; j++ ) {
            if ( j-money[i] < 0 && i == money.size()-1) dp[j][i%2] = 1100000;
            else if ( j-money[i]==0 ) dp[j][i%2] = 1;
            else if ( j-money[i] < 0 ) dp[j][i%2] = dp[j][(i+1)%2];
            else dp[j][i%2] = min ( dp[j-money[i]][i%2]+1, min ( dp[j-money[i]][(i+1)%2]+1, dp [j][(i+1)%2] ) );
        }
    }
    cout << dp [n][0] << '\n';
    return 0;
}
