import Control.Monad wafuu :: [[Int]] -> Int wafuu (x:xs) | wafuu' a xs && a >= 0 = a | otherwise = (-1) where a = foldr (-) 0 $ reverse x wafuu' :: Int -> [[Int]] -> Bool wafuu' n [] = True wafuu' n (y:ys) = n == (foldr (-) 0 $ reverse y) && wafuu' n ys main = do n <- readLn str <- replicateM n getLine let xs = map (map read . words) str :: [[Int]] print $ wafuu xs