import std.functional,
       std.algorithm,
       std.container,
       std.typetuple,
       std.typecons,
       std.bigint,
       std.string,
       std.traits,
       std.array,
       std.range,
       std.stdio,
       std.conv;

bool chmin(t)(ref t a, t b) {
  if (b < a) {
    a = b;
    return true;
  } else {
    return false;
  }
}

void main() {
  long N = readln.chomp.to!long;
  long ans = N;

  foreach (long i; 1..N+1) {
    long count, t;
    t = i;
    while (t > 0) {
      count += t % 6;
      t /= 6;
    }
    t = N - i;
    while (t > 0) {
      count += t % 9;
      t /= 9;
    }

    chmin(ans, count);
  }

  writeln(ans);
}
