In the event of technical difficulties with Szkopuł, please contact us via email at [email protected].
If you would like to talk about tasks, solutions or technical problems, please visit our Discord servers. They are moderated by the community, but members of the support team are also active there.
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 through . Each ranking is a sequence containing the numbers between to 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, and . We calculate the number 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.
The first line of the standard input contains two integers and (, ), the number of attractions in Byteland and the number of online guidebooks that Byteasar has found. Each of the following lines describes one of the online rankings. Such a ranking is given as a sequence of integers between and , in which each of the numbers occurs exactly once. The attractions are listed from the most recommended to the least recommended one.
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.
For the input data:
5 2 5 2 4 3 1 2 4 1 3 5
the correct result is:
8
Explanation of the example: Byteasar can create, among other possibilities, one of the following rankings: 2 4 5 3 1 or 5 2 4 3 1.
Task author: Piotr Sankowski (story by Jakub Lacki).