import Control.Applicative main = do n <- readLn wid <- map ((\(a:b:_) -> a + 4 * b) . map read . words) . lines <$> getContents let sumOfModTwo = sum (map (`mod` 2) wid) let max = maximum wid print $ if sumOfModTwo == n || sumOfModTwo == 0 then sum [(max - x) `div` 2|x <- wid] else -1