Arranging Cards

Time limit1sMemory limit128 MB

Problem

Dave's four-year-old son Maverick loves card games, but because he is so young he always loses when he plays with his older friends. Even arranging the cards in his hand is a struggle for him.

When Maverick receives his cards, he must arrange them so that all cards of the same color form one contiguous group. Then, within each group, he must sort the cards in ascending order of value: the card with the lowest value must be the leftmost in its group. The color groups may be placed in any order. Of course, he must keep holding all of the cards in his hand the entire time.

Maverick wants to arrange his cards with as few moves as possible. A single move consists of changing the position of one card.

Write a program that computes the minimum number of moves needed to arrange the cards.

Input

The first line contains two integers C, the number of colors (1 ≤ C ≤ 4), and N, the number of cards of each color (1 ≤ N ≤ 100), separated by a space.

Each of the next C×N lines contains two integers X and Y (1 ≤ X ≤ C, 1 ≤ Y ≤ N), separated by a space. They give the color X and the value Y of a card dealt to Maverick. The lines are given in the order the cards were dealt to him.

No two lines describe the same card.

Output

Print a single line containing the minimum number of moves needed to arrange the cards as described above.