Under construction

Still being built. Things may change

Anomaly Mahjong 9

Interview

Time limit2sMemory limit1024 MB

Summary
Given up to 100000 tiles from 34 kinds, find the shortest contiguous span that contains 5 tiles of the same kind, or -1 if no span does.
Level

Easy3 of 10

Topics
Hash map, Array
Solved
No attempts yet

Problem

A mahjong tile (牌) comes in 34 kinds, with 4 copies of each kind, for 136 tiles in total. The kinds are as follows.

NameDescription
Man tiles (萬數牌)Each tile shows a Chinese numeral. There are 9 kinds: 1m, 2m, 3m, 4m, 5m, 6m, 7m, 8m, 9m.
Pin tiles (筒數牌)Each tile shows circles. There are 9 kinds: 1p, 2p, 3p, 4p, 5p, 6p, 7p, 8p, 9p, by the number of circles.
Sou tiles (索數牌)Each tile shows bamboo sticks. There are 9 kinds: 1s, 2s, 3s, 4s, 5s, 6s, 7s, 8s, 9s, by the number of sticks.
Honor tiles (字牌)Each tile shows a character. There are 7 kinds: 1z, 2z, 3z, 4z, 5z, 6z, 7z.

You are practicing by shuffling the tiles and laying them down one at a time. An anomaly then changed the tile kinds. Having learned that each kind has 4 copies, you resolved it.

A bigger anomaly is happening now. Besides the change in kinds, the number of tiles has grown, and up to 100 000100\,000 tiles will be laid. You did not notice the odd count. So you must check whether any kind appears 5 or more times, as before. Your memory XX is finite, so you can only remember the XX most recently laid tiles. In other words, to notice the anomaly, right after laying some tile, at least 5 of the XX tiles in your memory must be the same kind.

Find the minimum memory needed to notice the anomaly. If no memory size lets you notice it, print -1.

Input

The first line contains NN, the number of tiles to lay. (1≤N≤100 000)(1 \le N \le 100\,000)

The second line contains the NN tiles in the order you lay them, separated by spaces. Every tile is one of the 34 kinds above.

Output

Print the minimum memory needed to notice the anomaly. If you cannot notice it no matter how large your memory is, print -1.

Examples2

  1. Example 1

    Input
    9
    5z 2z 5z 5z 5z 6z 5z 5z 3z
    
    Expected output
    6
    
  2. Example 2

    Input
    13
    1m 1m 1m 2m 3m 4m 5m 6m 7m 8m 9m 9m 9m
    
    Expected output
    -1