#P5831. Consistent Cow Gymnastics

    ID: 19059 Type: Default 1000ms 256MiB

Consistent Cow Gymnastics

Consistent Cow Gymnastics

Bessie has been chosen by Farmer John to train N cows in gymnastics. In K training sessions, Bessie ranks the cows according to their performance. A pair of distinct cows is considered consistent if one cow always outperforms the other in every session.

Your task is to count the number of consistent pairs.

Formally, given an integer \(K\) and \(N\), followed by \(K\) lines each containing a permutation of \(\{1, 2, \dots, N\}\) representing the ranking of cows for that session, a pair of cows \((i,j)\) (with \(i \neq j\)) is consistent if either cow \(i\) is ranked before cow \(j\) in every session, or cow \(j\) is ranked before cow \(i\) in every session.

inputFormat

The first line contains two integers \(K\) and \(N\) where \(1 \le K \le 10\) and \(1 \le N \le 20\). Each of the next \(K\) lines contains a permutation of \(N\) integers representing the ranking order of the cows in that session.

outputFormat

Output a single integer representing the number of consistent cow pairs.

sample

4 3
1 2 3
1 2 3
2 1 3
1 2 3
2