//Template
 
// By LD :)
//Includes
#include <vector> 
#include <queue>
#include <map> 
#include <set>
#include <utility> //Pair
#include <algorithm>
#include <sstream> // istringstream>> ostring stream<<
#include <iostream> 
#include <iomanip> 
#include <unordered_map>
//setbase - cout << setbase (16); cout << 100 << endl; Prlls 64
//setfill -   cout << setfill ('x') << setw (5); cout << 77 << endl; prlls xxx77
//setprecision - cout << setprecision (4) << f << endl; Prlls x.xxxx
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <cstring>
using namespace std;
 
//I want to code the solutions as fast as possible)
typedef long long ll;
typedef vector <ll> vi;
typedef vector <vi> vvi;
typedef vector <string> vs;
typedef pair<ll, ll > pii;
typedef vector <ll> vll;
typedef stringstream ss;
#define pb push_back
#define ff first
#define ss second
#define sz size()
#define sc(n) scanf("%d", &n)
#define fast ios_base:: sync_with_stdio(false)
#define llt(arr, x) memset(arr, x, sizeof(arr))
#define rep(i, n) for(ll i = 0; i < n; i++)
#define repp(i, a, b) for(ll i = a; i <= b; i++)
#define revrep(n, i) for(ll i = n - 1; i >= 0; i--)
#define revrepp(i, b, a) for(ll i = b; i >= a; i--)
#define all(a)  a.begin(), a.end() 
#define rall(a)  a.rbegin(), a.rend() 
#define ESP (1e-9)
 
//String Utilities 
bool isl(char c){ 
  return (c >= 'a' && c <= 'z'); 
}
bool isu(char c){ 
  return (c >= 'A' && c <= 'Z'); 
}
bool isa(char c){ 
  return (isl(c) || isu(c)); 
}
bool isn(char c){ 
  return (c >= '0' && c <= '9'); 
}
bool isan(char c){ 
  return (isa(c) || isn(c)); 
}
bool isv(char c){ 
  return (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u'); 
}
string tolow(string s){ 
  rep(i, s.sz) if(isu(s[i])) s[i] = s[i] -'A' + 'a'; return s; 
}
string toup(string s){ 
  rep(i, s.sz) if(isl(s[i])) s[i] = s[i] - 'a' + 'A'; return s;
}
 
//It all starts here ;)
 
//End Template
const ll N = 2e6 + 7;
const ll mod = 1e9 + 7;
const int logN = 20;
//Frequently used functions
ll gcd(ll a,ll b){
  if(a == 0){
    return b;
  }
  return gcd(b % a, a);
}
ll mpow(ll a, ll b){
  ll res = 1;
  while(b > 0){
    if(b & 1){
      res = (res * a) % mod;
    }
    b = b >> 1;
    a = (a * a) % mod;
  }
  return res % mod;
}
bool co(pair<int,pair<int,int>> &a, pair<int,pair<int,int>> &b){
  return a.ff < b.ff;
}
vs tokens(string s){
  stringstream ss;
  vs tt;
  ss.clear();
  ss << s;
  char str[s.sz + 10];
  strcpy(str, s.c_str()); 
  char *token = strtok(str, " -,/");
  while(token != NULL){
    string xy(token);
    tt.pb(xy);
    token = strtok(NULL, " -,/");
  }
  return tt;
}
ll modular_inv(ll a, ll b){
  ll b0 = b, t, q;
  ll x0 = 0, x1 = 1;
  if (b == 1) return 1;
  while (a > 1) {
    q = a / b;
    t = b, b = a % b, a = t;
    t = x0, x0 = x1 - q * x0, x1 = t;
  }
  if (x1 < 0) x1 += b0;
  return x1;
}
//Main program 
vi dp(N,1e9);
int main(){
  /*#ifndef ONLINE_JUDGE
  freopen("k1in.txt","r",stdin);
  freopen("k1out.txt","w",stdout);
  #endif*/
  cout << fixed << showpoint << setprecision(16); fast;
  dp[0] = 0;
  rep(i, 1000000){
    dp[i + 1] = min(dp[i + 1], dp[i] + 1);
    for(int j = 6; j < 1000000; j *= 6){
      dp[i + j] = min(dp[i + j], dp[i] + 1);
    }
    for(int j = 9; j < 1000000; j *= 9){
      dp[i + j] = min(dp[i + j], dp[i] + 1);
    }
  }
  int n;
  cin >> n;
  cout << dp[n];
} 

Measured memory_bytes: 15872