import std.stdio, std.conv, std.string, std.range, std.algorithm, std.range;
import std.functional;

void main() {
  auto N = readln.split[0].to!int;

  int[] items;
  items ~= 1;
  int tmp = 6;
  while(tmp <= N) {
    items ~= tmp;
    tmp *= 6;
  }
  tmp = 9;
  while(tmp <= N) {
    items ~= tmp;
    tmp *= 9;
  }
  items.sort!"a<b";

  auto dp = new int[1+N];
  dp[0] = 0;
  foreach(i; 1..(1+N)) {
    auto m = int.max;
    foreach(item; items) {
      if(item > i) break;
      m = min(m, 1 + dp[i-item]);
    }
    dp[i] = m;
  }

  dp[N].writeln;
}
