Coloured Bridges A (1/2/4 Points)


Problem Statement

Warning: This problem is not necessarily easy, this is the easier version of this problem.

You are given an undirected graph with nn vertices (labelled 11 through nn) and mm edges. Additionally, each vertex has a colour, which is given as a number between 00 and nn. Between any two vertices of the same colour you may build a bridge (edge) but only if they are not colour 00.

Building bridges is expensive so you do not want to build many. You want to get from vertex 11 to nn by traversing at little edges as possible, determine the way which cost the least number of bridges.

Determine the minimum number of bridges needed to get from vertex 11 to nn in the minimum possible time. If it is not possible to make such a path, output 1-1.

Input Format

Your first line will contain two integers nn and mm respectively. Your next line will contain nn integers, representing the colours of the nn nodes given in order of the nodes from 11 to nn. Your next mm lines will contain two integers each, uu and vv, meaning there is an edge between node uu and node vv.

Output Format

Output a single integer representing the minimum number of bridges needed to get from vertex 11 to nn in the minimum possible time.

Constraints

Subtask 1 - 25%:

  • 1n,m1031 \leq n, m \leq 10^3

Subtask 2 - 25%:

  • 1n1031 \leq n \leq 10^3
  • 1m1051 \leq m \leq 10^5

Subtask 3 - 50%:

  • 1n,m1051 \leq n, m \leq 10^5

Sample Cases

Input 1
4 3
1 2 3 4
1 2
2 3
3 4
Output 1
0
Input 2
4 2
1 2 1 3
1 2
3 4
Output 2
1
Input 3
3 0
0 1 2
Output 3
-1
Input 4
6 4
1 2 3 2 1 4
1 2
2 3
3 4
5 6
Output 4
1

Comments0


No comments yet

Be the first to comment.

New comment


Log in to join the discussion.