Depot Rearrangement

Time limit2sMemory limit128 MB

Problem

A company operates N shops and sells M different products, numbered from 1 to M. In its depot the company packs one container of a product for each shop, so there are exactly N containers labeled with each product and N×M containers in total. Because the depot is narrow, the containers are arranged in a single row at positions 1 through N×M. One extra position, N×M+1, is a single free slot that holds no container.

To speed up distribution, the manager wants to rearrange the row so that each consecutive block of M containers — positions 1..M, then M+1..2M, and so on — is labeled with M distinct products (exactly one container of each product). The order of containers within a block does not matter.

Rearranging is done using only the free slot. A single move takes the container at some occupied position and places it in the position that is currently free; the position it came from then becomes free. After all moves are finished, the free slot must again be at position N×M+1.

Compute the minimum number of moves required.

Input

The first line contains two integers N and M (1 ≤ N ≤ 400, 1 ≤ M ≤ 400). The second line contains N×M integers: the labels of the containers in their initial left-to-right order. Each product label x (1 ≤ x ≤ M) appears exactly N times.

Output

Print a single integer: the minimum number of moves needed so that every consecutive block of M containers holds M distinct product labels and the free slot returns to position N×M+1.