QOJ.ac

QOJ

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

#11839. Sport Clubs [A]

Statistics

The most popular sport in Byteland is BitBall. Many cities of Byteland organize BitBall Leagues every year. Each BitBall team has the right to participate in any number of leagues (no matter who is the organizer of the league). At the end of the season all the results are summarized. Each league publishes its own ranking list, which orders teams participating in that league. On the base of these ranking lists, The best BitBaller magazine determines and publishes the super ranking list of all BitBall teams.

Determining the super ranking list is not an easy task. The process of calculating it requires the following assumptions. If a given team took $ m $-th place on the ranking list which consists of $ l $ positions, then the number of points that this team scored is equal to $ l $ + 1 - $ m $. If the team did not participate in the league at all, it receives 0 points. The distance between two given ranking lists is computed as follows: for each team we calculate the absolute value of the difference between the number of points the team scored on both lists, then we calculate the sum of such values obtained for all teams. Super ranking list that has to be determined is such that its total distance from all ranking lists is minimal.

Write a program which:

  • reads from the standard input the description of the ranking lists for all BitBall leagues,
  • determines the super ranking list for The best BitBaller magazine,
  • writes total distance of the super ranking list to the standard output.

Input Format

In the first line of the standard input there are two integers $ n $ and $ k $ (2 ≤ $ n $ ≤ 500, 1 ≤ $ k $ ≤ 500), separated by a single space. They represent the number of teams and the number of leagues. Following $ k $ lines describe the leagues. Description of each league consists of an integer $ m $ (2 ≤ $ m $ ≤ $ n $), representing number of teams in the league, followed by $ m $ numbers $ l_{i} $ (1 ≤ $ l_{i} $ ≤ $ n $). No two numbers $ l_{i} $ and $ l_{j} $ for $ i $ ≠ $ j $ are equal. Number $ l_{i} $ means that $ i $-th place in the ranking list has been taken by the team $ l_{i} $. All numbers in the lines are separated by single spaces.

Output Format

In the first line of the standard output your program should write a single integer - the total distance of the super ranking list (calculated as the sum of distances from all ranking lists).

Example

Input

4 2
3 1 2 3
2 4 3

Output

11
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.