Under construction

Still being built. Things may change

Houses in Berland

Interview

Time limit2sMemory limit1024 MB

Summary
Find the shortest distance between two residential houses with different resident counts in a connected graph of crossroads and roads.
Level

Medium6 of 10

Topics
Graph, BFS
Solved
No attempts yet

Statement

The capital of Berland has nn crossroads. Some of them have residential houses. Let aia_i be the number of people in the family living in the house at crossroad ii, for all 1≤i≤n1 \le i \le n. If aia_i is 0, crossroad ii has no house.

Some crossroads are connected by roads. Berland has mm roads. Each road can be traveled in either direction. Each road connects two different crossroads, and any two crossroads are connected by at most one road. You can reach any crossroad from any other crossroad by roads. The distance between crossroads ii and jj is the minimum number of roads on a path from ii to jj.

When one family visits another, each person pairs up with exactly one person from the other family, and the two talk together. Two families find the visit boring if some person is left without a partner.

For reasons unknown, the government of Berland asks you to find two residential houses vv and uu such that if the family in house vv visits the family in house uu, the visit is boring. That means av>0a_v > 0, au>0a_u > 0, av≠aua_v \ne a_u, and the distance from vv to uu is minimal. It is guaranteed that two residential houses with different numbers of residents exist.

Input

The first line contains nn and mm (2≤n≤10000002 \le n \le 1000000, 1≤m≤10000001 \le m \le 1000000), the number of crossroads and the number of two-way roads.

The second line contains nn integers aia_i (0≤ai≤1090 \le a_i \le 10^9).

Each of the next mm lines contains two integers viv_i and uiu_i (1≤vi,ui≤n1 \le v_i, u_i \le n, vi≠uiv_i \ne u_i), the crossroads connected by the ii-th road.

Output

Print one integer: the minimum distance between two residential houses with different numbers of residents.

Hint

In the first example, only crossroads 1 and 3 have houses, and their residents differ. The distance between them is 2, and the shortest path uses roads 1 and 2.

Examples2

  1. Example 1

    Input
    4 4
    1 0 2 0
    1 2
    2 3
    3 4
    4 1
    
    Expected output
    2
    
  2. Example 2

    Input
    5 4
    1 0 2 1 0
    1 5
    4 5
    5 2
    2 3
    
    Expected output
    3