#include<stdio.h>
int main(void){
  char *s="python3 -c\""
"s=lambda a,b:a if a<b else s(a//b,b)+a%b;n=int(input());print(min(s(i,6)+s(n-i,9)for i in range(n+1)))\n"
"\"";
  system(s);
  return 0;
}            