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