integer N,N_6,N_9
integer ans,preans
read*,N

ans=huge(ans)
do i =0,N
  preans=0
  N_6=i
  do while(N_6/=0)
    preans=preans+mod(N_6,6)
    N_6=N_6/6
  end do
  N_9=N-i
  do while(N_9/=0)
    preans=preans+mod(N_9,9)
    N_9=N_9/9
  end do
  ans=min(ans,preans)
end do

print"(I0)",ans
end