This page is still under construction.

Parts of this page are still being built. What you see may change.

Double Rainbow

Interview

Time limit1sMemory limit1024 MB

Summary
Given a color sequence and k colors, find the shortest contiguous block that contains every color and whose complement also contains every color, or print 0.
Level

Medium6 of 10

Topics
Two pointers, Sliding window, Prefix sum, Array
Solved
No attempts yet

Problem

Let PP be a set of nn points on the xx-axis, and each point is colored with one of the colors 1,2,…,k1, 2, \dots, k. For each of the kk colors, there is at least one point in PP colored with that color. For a set P′P' of consecutive points from PP, if both P′P' and P\P′P \backslash P' contain at least one point of each color, then P′P' makes a double rainbow. See the figure below as an example. The set PP consists of ten points, and each point is colored with one of the colors 11, 22, 33, and 44. The set P′P' of the five consecutive points inside the rectangle makes a double rainbow.

Given a set PP of points and the number kk of colors as input, write a program that computes and prints the minimum size of P′P' that makes a double rainbow.

Input

Your program is to read from standard input. The input starts with a line containing two integers nn and kk (1≤k≤n≤10,0001 ≤ k ≤ n ≤ 10,000), where nn is the number of points in PP and kk is the number of colors. Each of the following nn lines consists of an integer from 11 to kk, inclusive, and the ii-th line corresponds to the color of the ii-th point of PP from the left.

Output

Your program is to write to standard output. Print exactly one line. The line should contain the minimum size of P′P' that makes a double rainbow. If there is no such P′P', print 0.

Examples2

  1. Example 1

    Input
    10 4
    1
    2
    3
    1
    1
    4
    2
    4
    3
    3
    
    Expected output
    5
    
  2. Example 2

    Input
    6 3
    1
    1
    2
    2
    3
    3
    
    Expected output
    0