After the lecture on sorting, Diana's CS teacher has given the class a challenge: what's the most efficient way to sort the class list? Students are only allowed to sort by repeatedly swapping two names in the list, and the person who makes the fewest swaps wins.
Diana really wants to win, so she decides to do something sneaky. When showing the teacher her efficient sort, she will omit one of the names in the list, which will let her use fewer swaps. Using her tactical advantage, what is the fewest swaps she has to do?
The input will contain ~10~ test cases. Each test case starts with an integer ~N~ ~(1 \le N \le 1\,000)~. The next ~N~ lines represent the class list, with one name on each line in capital letters. Names will be unique and only contain uppercase letters.
For each test case, your program should output the minimum number of swaps Diana needs to sort the list using her trick.
3 SAM DIANA REBECCA 5 DEREK MEGAN BRIAN BOB DIANA
Note: Only ~2~ cases are shown in this sample.
In the first example, removing
SAM will leave the list sorted, so no swaps are required.
In the second example, Diana could omit
MEGAN and then sort the list by swapping
Educational Computing Organization of Ontario - statements, test data and other materials can be found at ecoocs.org