This page is still under construction.

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

Hostile States

Time limit1sMemory limit128 MB

Summary
Assign each undecided city to one of two states to minimize the number of roads crossing between states.
Level

Medium7 of 10

Topics
Graph
Solved
No attempts yet

Problem

Bitocja and Bajtocja are about to sign a truce after a long war. The two states must decide which state each city belongs to. Their rulers agreed to split the cities so as to minimize the number of roads that directly connect a pair of cities belonging to different states.

After the truce is signed, report how many such roads connect the two states.

Input

The first line contains the number of cities nn and the number of roads mm (1≤n≤5001 \le n \le 500, 0≤m≤n(n−1)/20 \le m \le n(n-1)/2).

The second line contains nn integers a1,a2,…,ana_1, a_2, \ldots, a_n (1≤ai≤31 \le a_i \le 3). If ai=1a_i = 1, city ii belongs to Bitocja; if ai=2a_i = 2, city ii belongs to Bajtocja; if ai=3a_i = 3, city ii must be assigned to either Bitocja or Bajtocja.

Each of the next mm lines contains two integers aa, bb (1≤a<b≤n1 \le a < b \le n), meaning that cities aa and bb are joined by a direct road. No pair (a,b)(a, b) appears more than once.

Output

Print a single integer: the minimum possible number of roads connecting a city of Bitocja with a city of Bajtocja after the truce is signed.

Hint

In the first example, city 3 is the only unassigned city. Assigning it to Bitocja leaves just one road between the two states, the road between cities 3 and 4, whereas assigning it to Bajtocja would leave two such roads. Hence the minimum is 1.

Examples5

  1. Example 1

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

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

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

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

    Input
    4 4
    1 3 3 2
    1 2
    1 3
    2 4
    3 4
    
    Expected output
    2