Under construction

Still being built. Things may change

Tidying the Plush Toys

Time limit2sMemory limit512 MB

Summary
Given a row of N toys of M types, find the fewest toys to remove so that after reinserting them all toys of each type form one contiguous block.
Level

Hard8 of 10

Topics
Dynamic programming, Bit manipulation, Prefix sum
Solved
No attempts yet

Problem

Someone involved with JOI works at a toy store. Today they have to tidy up the plush toy corner of the store.

On the shelf of the plush toy corner, NN plush toys stand in a row from left to right. Dividers split the shelf into NN compartments, and each compartment holds one plush toy. The store sells MM types of plush toys in total, numbered from 11 to MM. Each of the NN plush toys on the shelf is one of these MM types. Every type has at least one plush toy on the shelf.

To make the shelf look better, you want to rearrange the plush toys so that all toys of the same type sit consecutively on the shelf. You rearrange them as follows.

  • Choose some of the NN plush toys and take them off the shelf. The toys that are not taken off stay where they are.
  • Put the toys you took off back into the empty compartments of the shelf, in any order you like.

After the rearrangement, all plush toys of the same type must sit consecutively on the shelf. Write a program that finds the minimum number of plush toys you must take off to do this.

Input

The input consists of 1+N1 + N lines.

The first line contains two integers NN and MM (1≤N≤100 0001 \le N \le 100\,000, 1≤M≤201 \le M \le 20) separated by a space: there are NN plush toys of MM types.

Each of the next NN lines contains an integer between 11 and MM inclusive. The integer on the ii-th of these lines (1≤i≤N1 \le i \le N) is the type of the plush toy in the ii-th compartment from the left. Every type is guaranteed to have at least one plush toy.

Output

Print, on one line, the minimum number of plush toys you must take off to rearrange the shelf.

Hint

In sample 1, the types of the plush toys are 1, 2, 2, 2, 1, 2, 1 from left to right. To take off as few toys as possible, take off the 1st and 6th toys from the left, then put a type 2 toy in the 1st compartment and a type 1 toy in the 6th compartment. This takes off 2 toys.

Examples2

  1. Example 1

    Input
    7 2
    1
    2
    2
    2
    1
    2
    1
    
    Expected output
    2
    
  2. Example 2

    Input
    12 4
    1
    3
    2
    4
    2
    1
    2
    3
    1
    1
    3
    4
    
    Expected output
    7