line_count = int(input())
birds = []
sums = []
for _ in range(line_count):
    line = input().split()
    bird_count = int(line[0])
    sums.append(bird_count)
    position = {}
    for bird in line[1:]:
        desired_pos = int(bird)
        if desired_pos in position:
            position[desired_pos] += 1
        else:
            position[desired_pos] = 1
    birds.append(position)

all_sums = sum(sums)
collisions = 0
for birds_index, (start, cur_sum) in enumerate(zip(birds, sums)):
    for pos in start.keys():
        collisions += all_sums - cur_sum
        for other_index, other_birds in enumerate(birds):
            if other_index == birds_index:
                continue
            for other_pos, other_count in other_birds.items():
                if birds_index < other_index and other_pos >= pos:
                    collisions -= other_count
                elif birds_index > other_index and other_pos <= pos:
                    collisions -= other_count


print(collisions // 2)

