import Control.Monad (replicateM) solve :: [[Int]] -> Int solve ab = minimum $ map abs $ f 0 ab f :: Int -> [[Int]] -> [Int] f k [] = [k] f k ab = left ++ right where left = f (k + head (head ab)) (tail ab) right = f (k - last (head ab)) (tail ab) main = do n <- readLn :: IO Int ab <- map (map read . words) <$> replicateM n getLine :: IO [[Int]] print $ solve ab