cswork / OAOpen practice workspace

SnowflakeOA #39

Minimum Index Distance Between Person and Cake

Estimated EasyJudge-ready

Topics (AI-labelled)Two PointersIn-place Arrays

In the array, 0 is an empty spot, 1 is a person, and 2 is a cake. Find the minimum absolute index difference between any person and any cake; return −1 if either kind is missing. This is not about assigning a different cake to each person.

Input

The first line contains n, and the second line contains the array. 1≤n≤200000, and elements are only 0, 1, or 2.

Output

Output the single integer required by the problem.

Example 1

Input:

5
0 1 0 2 0

Output:

2

Example 2

Input:

3
2 0 1

Output:

2

Example 3

Input:

3
0 0 2

Output:

-1

Example notes

The answers to the three public examples are, in order: 2; 2; -1. The examples were rechecked by independent enumeration, state search, or direct simulation.

Editorial

Loading solution…