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 1 to n. A ranking is a sequence of the numbers 1 through n, 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 p1 and p2 where it appears in the two rankings and compute the value min(∣p1−p2∣,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.
The first line contains two integers n and k (2≤n≤5000, 2≤k≤3): the number of attractions in Byteland and the number of online guidebooks Byteasar found.
Each of the next k lines describes one online ranking. A ranking is a sequence of n integers, each between 1 and n and occurring exactly once, listed from the most recommended attraction to the least recommended one.
Print a single integer: the minimum possible total distance between Byteasar's ranking and the online rankings he found.
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 5 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 8.