local mmi = math.min
local sn = io.read()
local n = #sn
local t = {}
for i = 1, n do
  t[i] = tonumber(sn:sub(i, i))
end
local p1, p2 = 1, 0
for i = 1, n do
  local np1 = 1000000007
  if i ~= n and 0 < t[i + 1] then
    np1 = mmi(p1 + 9 - t[i], p2 + t[i] + 1)
  end
  local np2 = mmi(p1 + 10 - t[i], p2 + t[i])
  p1, p2 = np1, np2
end
print(mmi(p1, p2))
