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

int main() {
  int N;
  cin >> N;
  int ans = 100000;
  int term = 0;
  int s6,s9;
  for (int i=0; i<=N; i++) {
    s6 = i;
    s9 = N-i;
    term = 0;
    for (int j=0; j<8; j++) {
      term += s6%6 + s9%9;
      s6 /= 6;
      s9 /= 9;
    }
    ans = min(ans,term);
  }
  cout << ans << endl;
}