function Main(s) {
  var n = s.split("\n")[0]*1;
  var ans = n; 
  for (var i = 0; i <= n; i++) {
    var cnt = 0, t = i;
    while(t > 0) cnt += t % 6, t = ~~(t / 6);
    t = n - i;
    while(t > 0) cnt += t % 9, t = ~~(t / 9);
    if(ans > cnt) ans = cnt;
  }
  console.log(ans);
}
Main("127\n");