#P5831. [USACO19DEC] Cow Gymnastics B

[USACO19DEC] Cow Gymnastics B

Problem Description

To improve their health, the cows have started doing gymnastics training. Farmer John chose his favorite cow, Bessie, to coach the other NN cows, while also evaluating their progress in learning different gymnastics skills.

In each of the KK training sessions, Bessie ranks the NN cows based on their performance. Later, she became curious about how consistent these rankings are. A pair of different cows is called consistent if one cow performs better than the other in every training session.

Please help Bessie count the number of consistent cow pairs.

Input Format

The first line contains two positive integers KK and NN. The next KK lines each contain a permutation of the integers 1N1 \ldots N, representing the rankings of the cows (the cows are identified by numbers 1N1 \ldots N). If in a line AA appears before BB, it means cow AA performed better than cow BB.

Output Format

Output one line containing the number of consistent cow pairs.

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

Hint

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

1K101 \leq K \leq 10, 1N201 \leq N \leq 20.

Problem by: Nick Wu.

Translated by ChatGPT 5