N=io.read("n")
t={}
for i=1,N do
  count=0
  j=N-i 
  while (i>0) do
    count=count+i%6
    i=i//6
  end
  while (j>0) do
    count=count+j%9
    j=j//9
  end
  t[i]=count
end
table.sort(t)
print(t[1])