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

#define mp              make_pair
#define pb              push_back
#define ff              first
#define ss              second
#define inc(A,B)        A=(A+(B))%MOD
#define mul(A,B)        (ll(A)*ll(B)%ll(MOD))
#define sub(a,b)        (((a)-(b)+MOD)%MOD)

typedef pair < int ,int > pii;
typedef long long ll;
typedef complex < ll > pt;

const int MAX_N = 100005;
const int MOD = 1e9 + 7;

int n, exponent;
int memo[MAX_N];

void update ( int &cant, int nextCant ) {
  if ( cant == -1 )
    cant = nextCant;
  else
    cant = min ( cant, nextCant );
}

void fillFor ( int num ) {
  for ( int i = 0; i <= n-num; ++i )
    if ( memo[i] != -1 )
      update ( memo[i+num], memo[i]+1 );
}

void doForExp ( int exponent, int bas ) {
  do {
    bas *= exponent;
    fillFor ( bas );
  } while ( bas <= n/exponent );
}

int main() {

//  freopen ( "in.txt" , "r" , stdin );
//  freopen ( "out.txt" , "w" , stdout );

  memset ( memo, -1, sizeof memo );
  scanf ( "%d", &n );

  memo[0] = 0;
  doForExp ( 6, 1 );
  doForExp ( 9, 1 );
  fillFor ( 1 );

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

  return 0;

}
