시간 제한 메모리 제한 제출 정답 맞은 사람 정답 비율
1 초 128 MB 0 0 0 0.000%

문제

Byteasar is going for a vacation to Byteland. Right now he is planning his stay and trying to figure out which of the possible touristic attractions he would like to see the most. He found several online guidebooks around Byteland. Each of the guidebooks contains a ranking of all the attractions. Now Byteasar would like to create his own ranking.

The attractions are numbered 1 through n. Each ranking is a sequence containing the numbers between 1 to n ordered from the most to the least recommended attraction. The distance between two rankings is computed as follows. For each attraction we find the positions where it occurs in the respective sequences, p1 and p2. We calculate the number min(|p1 - p2|, 8) that represents how much do the two rankings differ on this attraction. The distance of the rankings is now the sum of such numbers over all the attractions.

Byteasar would like to create a ranking for which the sum of distances from all the online rankings that he found is as small as possible.

입력

The first line of the standard input contains two integers n and k (2 ≤ n ≤ 5 000, 2 ≤ k ≤ 3), the number of attractions in Byteland and the number of online guidebooks that Byteasar has found. Each of the following k lines describes one of the online rankings. Such a ranking is given as a sequence of n integers between 1 and n, in which each of the numbers occurs exactly once. The attractions are listed from the most recommended to the least recommended one.

출력

The first and only line of the standard output should contain one integer: the minimum total distance of Byteasar's ranking to the online rankings that he found.

예제 입력 1

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

예제 출력 1

8

힌트

Byteasar can create, among other possibilities, one of the following rankings: 2 4 5 3 1 or 5 2 4 3 1.