program test
implicit none
integer(8) :: n,i,j,k
integer(8) :: dp(0:12,0:100000)
integer(8),parameter :: ten = 10
integer(8) :: inf = ten ** 15
integer(8) :: money(12) = (/1,6**1,6**2,6**3,6**4,6**5,6**6,9**1,9**2,9**3,9**4,9**5/)

read(*,*) n

dp(0:12,0) = 0
dp(0:12,1:100000) = inf

do i = 1,12
	do j = 1,n
		k = 1
		dp(i,j) = dp(i-1,j)
		do while (j >= money(i)*k .and. k<= 8)
			dp(i,j) = min(dp( i-1,j-(k*money(i)))+k,dp(i-1,j),dp(i,j))
			k = k + 1
		enddo
	enddo
enddo

write(*,*) dp(12,n)

end program