USACO Bronze division

Cow Gymnastics

In order to improve their physical fitness, the cows have taken up gymnastics! Farmer John designates his favourite cow Bessie to coach the n other cows and to assess their progress as they learn various gymnastic skills.

In each of k practice sessions, Bessie ranks the n cows according to their performance. Afterward, she is curious about the consistency in these rankings. A pair of two distinct cows is consistent if one cow did better than the other one in every practice session.

Help Bessie compute the total number of consistent pairs.


The first line contains two positive integers k (1k10) and n (1n20). The next k lines will each contain the integers 1 ... n in some order, indicating the rankings of the cows (cows are identified by the numbers 1...n). If A appears before B in one of these lines, that means cow A did better than cow B.


Print the number of consistent pairs.


The consistent pairs of cows are (1, 4), (2, 4), (3, 4) and (1, 3).

Time limit 1 second
Memory limit 128 MiB
Input example #1
3 4
4 1 2 3
4 1 3 2
4 2 1 3
Output example #1
Source 2019 USACO December Bronze