def cc080():
    n = int(input())
    fn = list()
    pn = list()
    for i in range(0, n):
        fn.append(list(map(int, input().split())).count(1))
    for i in range(0, n):
        pn.append(list(map(int, input().split())))

    close = True
    mi = 10000000
    tp = 0
    for i in range(0, n):
        clo = pn[i][0]
        ope = max(pn[i][1:fn[i]+1])
        tp += max([clo, ope])
        if close and max([clo, ope]) == clo:
            mi = min([mi, ope])
        else:
            close = False
    if close:
        tp -= abs(mi)

    print(tp)

cc080()