count = int (input())
pairs = []
indexes = [0]

for i in range(count):
    line = list(map(int, input().split(" ")))
    indexes.append(0)
    for x in range(1, len(line)):
        pairs.append((i, line[x]))
        indexes[-1] += 1
    indexes[-1] += indexes[-2]

count = 0
destination = sorted(pairs, key=lambda x: (x[1], x[0]))

for i, pair in enumerate(destination):
    pos, next = pair

    for j in range(i + 1, indexes[pos] + 1):
        Npos, Nnext = destination[j]

        if Nnext > next:
            count += 1


print(count)
