import qualified Data.ByteString.Char8 as C
import Data.List
import Data.Maybe
import Data.Vector.Unboxed ((!))
import qualified Data.Vector.Unboxed as U

main = C.interact $ put . sol . get

get = U.unfoldr (C.readInt . C.dropWhile (<'0'))

put = C.pack . show

sol v = if e then -1 else d
  where
  n = v!0
  a = U.drop 1 v
  (e,d) = foldl' h (False, 0) [(i,j) | i <- [0..n-2], j <- [i+1..n-1]]
  h (b,d) (i,j) = (b || e, if e' then d else d+a!(i*n+j))
    where
    ts = [(a!(i*n+j),a!(i*n+k)+a!(k*n+j)) | k <- [0..n-1], k/=i, k/=j]
    e = isJust $ find (uncurry (>)) ts
    e' = isJust $ find (uncurry (==)) ts