import Control.Monad
import Data.Maybe
import qualified Data.ByteString.Char8 as BS

readInt = fst . fromJust . BS.readInteger

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 n 0

makeList n
   |d == 0     = [m]
   |otherwise  = makeList d ++ [m]
   where (d,m) = divMod n 10

main = do
   n <- readInt <$> BS.getLine
   putStrLn $ show $ countBill $ makeList n