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

int main() {
  int n, o = INT_MAX, r, s, t;
  cin >> n;
  for(int i = 0; i <= n; i++) {
    s = n - i;
    t = i;
    r = 0;
    while(s) {
      r += s % 6;
      s /= 6;
    }
    while(t) {
      r += t % 9;
      t /= 9;
    }
    o = min(o, r);
  }
  cout << o << endl;
}