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

Discussions

About Discussions

The discussion section is only for posting: Editorials, General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues. Submitting multiple issues may cause your account to be banned.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.