local memo={}
local function rec(x)
    if x==0 then 
        return 0 
    end
    if memo[x]~=-1 then
        return memo[x]
    end
    local result=x
    local six=1
    while six<=x do
        result=math.min(result,rec(x-six)+1)
        six=six*6
    end
    local nine=1
    while nine<=x do
        result=math.min(result,rec(x-nine)+1)
        nine=nine*9
    end
    memo[x]=result
    return memo[x]
end

local n=io.read("n")
for i=1,n do
    memo[i]=-1
end
print(rec(n))