#include <bits/stdc++.h>
using namespace std;
int main(){
  int n;
  cin >> n;
  int ans = n;
  for(int i = 0; i <= n; i++){
    int cost = 0;
    for(int j = i; j > 0; j /= 6)cost += j % 6;
    for(int j = n - i; j > 0; j /= 9)cost += j % 9;
    ans = min(ans, cost);
  }
  cout << ans << endl;
  return 0;
}