local n=io.read("n")
local result=n
for i=0,n do
    local counter=0
    local draw=i
    while draw>0 do
        counter=counter+draw%6
        draw=math.floor(draw/6)
    end
    local draw=n-i
    while draw>0 do
        counter=counter+draw%9
        draw=math.floor(draw/9)
    end
    if result>counter then
        result=counter
    end
end
print(result)