f (x:[]) num
   |x < 6     = num + x
   |otherwise = num + (11 -x)
f (x:xs:xss) num
   |x==5&&xs>4= f ((xs+1):xss) (num + 10 -x) 
   |x < 6     = f (xs:xss) (num + x)
   |otherwise = f ((xs+1):xss) (num + 10 -x)

countbill n = f (reverse n) 0

main = do
   n <- map (read :: String -> Integer) . (map (\a -> [a])) <$> getLine
   putStrLn $ show $ countbill n