Vacation

No attempts yetTime limit1sMemory limit128 MB

Problem

Byteasar is going on a vacation to Byteland. As he plans his stay, he wants to decide which tourist attractions he would most like to see. Byteland has several online guidebooks, and each guidebook contains a ranking of all the attractions. Byteasar would like to use these rankings to build his own.

The attractions are numbered from 11 to nn. A ranking is a sequence of the numbers 11 through nn, listed from the most recommended attraction to the least recommended one.

The distance between two rankings is computed as follows. For each attraction, find the positions p1p_1 and p2p_2 where it appears in the two rankings and compute the value min(p1p2,8)\min(|p_1 - p_2|, 8), which measures how much the two rankings differ on that attraction. The distance between the two rankings is the sum of these values over all attractions.

Byteasar wants to build his own ranking so that the total distance to all of the online rankings he found is as small as possible. Find that minimum total distance.

Input

The first line contains two integers nn and kk (2n50002 \le n \le 5000, 2k32 \le k \le 3): the number of attractions in Byteland and the number of online guidebooks Byteasar found.

Each of the next kk lines describes one online ranking. A ranking is a sequence of nn integers, each between 11 and nn and occurring exactly once, listed from the most recommended attraction to the least recommended one.

Output

Print a single integer: the minimum possible total distance between Byteasar's ranking and the online rankings he found.

Note

Several different rankings may achieve the minimum total distance, but the only value you must output is that minimum total distance (a single integer). For example, if there are 55 attractions and the rankings are 5 2 4 3 1 and 2 4 1 3 5, then rankings such as 2 4 5 3 1 or 5 2 4 3 1 all achieve the minimum total distance of 88.