B - Discord Editorial /

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 200200

問題文

1,2,,N1,2,\ldots,N と番号づけられた NN 人が MM 回、一列に並んで集合写真を撮りました。ii 番目の撮影で左から jj 番目に並んだ人の番号は ai,ja_{i,j} です。

ある二人組は MM 回の撮影で一度も連続して並ばなかった場合、不仲である可能性があります。  

不仲である可能性がある二人組の個数を求めてください。なお、人 xx と人 yy からなる二人組と人 yy と人 xx からなる二人組は区別しません。

制約

  • 2N502 \leq N \leq 50
  • 1M501 \leq M \leq 50
  • 1ai,jN1 \leq a_{i,j} \leq N
  • ai,1,,ai,Na_{i,1},\ldots,a_{i,N} には 1,,N1,\ldots,N11 回ずつ現れる
  • 入力はすべて整数

入力

入力は以下の形式で標準入力から与えられる。

NN MM
a1,1a_{1,1} \ldots a1,Na_{1,N}
\vdots
aM,1a_{M,1} \ldots aM,Na_{M,N}

出力

答えを出力せよ。


入力例 1Copy

Copy
4 2
1 2 3 4
4 3 1 2

出力例 1Copy

Copy
2

11 と人 44 からなる二人組と、人 22 と人 44 からなる二人組がそれぞれ不仲である可能性があります。


入力例 2Copy

Copy
3 3
1 2 3
3 1 2
1 2 3

出力例 2Copy

Copy
0

入力例 3Copy

Copy
10 10
4 10 7 2 8 3 9 1 6 5
3 6 2 9 1 8 10 7 4 5
9 3 4 5 7 10 1 8 2 6
7 3 1 8 4 9 5 6 2 10
5 2 1 4 10 7 9 8 3 6
5 8 1 6 9 3 2 4 7 10
8 10 3 4 5 7 2 9 6 1
3 10 2 7 8 5 1 4 9 6
10 6 1 5 4 2 3 8 9 7
4 5 9 1 8 2 7 6 3 10

出力例 3Copy

Copy
6

Score : 200200 points

Problem Statement

NN people numbered 1,2,,N1,2,\ldots,N were in MM photos. In each of the photos, they stood in a single line. In the ii-th photo, the jj-th person from the left is person ai,ja_{i,j}.

Two people who did not stand next to each other in any of the photos may be in a bad mood.

How many pairs of people may be in a bad mood? Here, we do not distinguish a pair of person xx and person yy, and a pair of person yy and person xx.

Constraints

  • 2N502 \leq N \leq 50
  • 1M501 \leq M \leq 50
  • 1ai,jN1 \leq a_{i,j} \leq N
  • ai,1,,ai,Na_{i,1},\ldots,a_{i,N} contain each of 1,,N1,\ldots,N exactly once.
  • All values in the input are integers.

Input

The input is given from Standard Input in the following format:

NN MM
a1,1a_{1,1} \ldots a1,Na_{1,N}
\vdots
aM,1a_{M,1} \ldots aM,Na_{M,N}

Output

Print the answer.


Sample Input 1Copy

Copy
4 2
1 2 3 4
4 3 1 2

Sample Output 1Copy

Copy
2

The pair of person 11 and person 44, and the pair of person 22 and person 44, may be in a bad mood.


Sample Input 2Copy

Copy
3 3
1 2 3
3 1 2
1 2 3

Sample Output 2Copy

Copy
0

Sample Input 3Copy

Copy
10 10
4 10 7 2 8 3 9 1 6 5
3 6 2 9 1 8 10 7 4 5
9 3 4 5 7 10 1 8 2 6
7 3 1 8 4 9 5 6 2 10
5 2 1 4 10 7 9 8 3 6
5 8 1 6 9 3 2 4 7 10
8 10 3 4 5 7 2 9 6 1
3 10 2 7 8 5 1 4 9 6
10 6 1 5 4 2 3 8 9 7
4 5 9 1 8 2 7 6 3 10

Sample Output 3Copy

Copy
6


2025-04-17 (Thu)
23:15:36 +00:00