program abc099_c
    use,intrinsic :: iso_fortran_env
    implicit none
    integer(int32):: n,ans,i,cc,t
    read*, n

    ans = n


    do i=0,n
        cc = 0
        t = i
        do while(t > 0)
            cc=cc+mod(t,6)
            t=t/6
        end do
        t = n-i
        do while(t>0)
            cc=cc+mod(t,9)
            t=t/9
        end do
        ans=min(ans,cc)
    end do
    print'(i0)', ans
end program abc099_c