import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,queue,copy

sys.setrecursionlimit(10**7)
inf=10**20
mod=10**9+7
dd=[(-1,0),(0,1),(1,0),(0,-1)]
ddn=[(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS(): return sys.stdin.readline().split()
def S(): return input()

def main():
  n=I()
  l1=[]

  for _ in range(n):
    l1.append(sum(LI()))

  l2=[LI() for _ in range(n)]

  ans=0
  f=True
  mn=-inf
  for i in range(n):
    x=l1[i]
    y=l2[i]
    _ans=max(y[:x+1])
    if _ans==y[0]:
      if y[:x+1].count(_ans)>1:
        f=False
      else:
        mn=max(mn,max(y[1:x+1]))
    else:
      f=False
    ans+=_ans

  if f:
    return mn
  return ans

# main()
print(main())
