QOJ.ac

QOJ

Time Limit: 1 s Memory Limit: 128 MB Total points: 10

#10388. Vacation [A]

统计

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, $ p_{1} $ and $ p_{2} $. We calculate the number $\min(|p_{1} - p_{2}|, 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.

Input Format

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.

Output Format

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.

Example

Input

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

Output

8

Notes

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

About Issues

We understand that our problem archive is not perfect. If you find any issues with the problem, including the statement, scoring configuration, time/memory limits, test cases, etc.

You may use this form to submit an issue regarding the problem. A problem moderator will review your issue and proceed it properly.

STOP! Before you submit an issue, please READ the following guidelines:

  1. This is not a place to publish a discussion, editorial, or requests to debug your code. Your issue will only be visible by you and problem moderators. Other users will not be able to view or reply your issues.
  2. Do not submit duplicated issues. If you have already submitted one, please wait for an moderator to review it. Submitting multiple issues will not speed up the review process and might cause your account to be banned.
  3. Issues must be filed in English or Chinese only.
  4. Be sure your issue is related to this problem. If you need to submit an issue regarding another problem, contest, category, etc., you should submit it to the corresponding page.

Active Issues 0

No issues in this category.

Closed/Resolved Issues 0

No issues in this category.