n = int(input())
arr = []
cnt = 0

for i in range(n):
    inp = list(map(int, input().split(" ")))
    for x in inp[1:]:
        if len(arr) > 0:
            for y in arr:
                if i > y[0] and x < y[1] or i < y[0] and x > y[1]:
                    cnt += 1
        arr.append([i, x])  
print(cnt)


