local read = setmetatable({}, {__index = function(t, k) local a = {} for i=1,#k do table.insert(a, '*'..string.sub(k, i, i)) end local r = io.read local u = table.unpack or unpack return function() return r(u(a)) end end})
read.N = function(N) local t={} for i=1,N do t[i]=read.n() end return t end
string.totable = function(s) local t={} local u=string.sub for i=1,#s do t[i] = u(s, i, i) end return t end
string.split = function(s) local t={} for w in string.gmatch(s, "[^%s]+") do table.insert(t, w) end return (table.unpack or unpack)(t) end
local function array(dimension, default_val) local n=dimension local m={default_val and {__index=function()return default_val end}}for i=2,n do m[i]={__index=function(p, k)local c=setmetatable({},m[i-1])rawset(p,k,c)return c end}end return setmetatable({},m[n])end
----
local N = read.n()
local tbl = array(1, {10^9, {0}})
local function chmin(i, s)
    if tbl[i][1] > tbl[s][1] then
        local route = {table.unpack(tbl[s][2])}
        table.insert(route, i-s)
        tbl[i] = {tbl[s][1]+1, route}
    end
end
tbl[0] = {0, {}}
for i=0,N-1 do
    chmin(i+1, i)
    local j = 6
    while j <= N do
        chmin(i+j, i)
        j = j * 6
    end
    j = 9
    while j <= N do
        chmin(i+j, i)
        j = j * 9
    end
end
for i=0,N do
    --print(i, tbl[i][1], table.concat(tbl[i][2], ","))
end
print(tbl[N][1])