import std.stdio;
import std.algorithm;
import std.range;
import std.conv;
import std.format;
import std.array;
import std.math;
import std.string;
import std.container;

int[100001] dp;

int solve(int n) {
  if (n == 0)
    return 0;

  if (dp[n] != -1)
    return dp[n];

  int ret = solve(n - 1) + 1;

  for (int d = 6; n - d >= 0; d *= 6)
    ret = min(solve(n - d) + 1, ret);

  for (int d = 9; n - d >= 0; d *= 9)
    ret = min(solve(n - d) + 1, ret);

  return dp[n] = ret;
}

void main() {
  int N; readlnTo(N);
  foreach(i; 0..N+1)
    dp[i] = -1;
  writeln(solve(N));
}

// helpers
void readlnTo(T...)(ref T t) {
  auto s = readln.split;
  assert(s.length == t.length);
  foreach(ref ti; t) {
    ti = s[0].to!(typeof(ti));
    s = s[1..$];
  }
}
