num = int(input())

array = []
for i in range(0, num):
    array.append([int(x) for x in input().split()])

array2 = []
for j in range(0, num):
    array2.append([int(x) for x in input().split()])

res_array = []
for k in range(0, len(array2)):
    res_array.append(array2[k][:array[k].count(1)+1])

res = 0
judge = 0
for l in res_array:
    if l[0] == max(l) and l.count(l[0]) == 1:
        judge += 1
    res += max(l)
if judge == len(res_array):
    tmp_array = []
    for m in res_array:
        tmp_array += m[1:]
        res = min(tmp_array)
print(res)