program c

integer :: n,six(100000),nine(100000),six_nine,i,j
read(*,*) n

do i = 1,n
	six(i) = 0
	do j = 0,6
		six(i) = six(i) + mod(i / (6 ** j),6)
	enddo
enddo

do i = 1,n
	nine(i) = 0
	do j = 0,5
		nine(i) = nine(i) + mod(i / (9 ** j),9)
	enddo
enddo

six_nine = min(six(n),nine(n))

do i= 1,n-1
	six_nine = min(six_nine,six(i)+nine(n-i))
enddo

write(*,*) six_nine

end program