#arc144e. [arc144_e]GCD of Path Weights

[arc144_e]GCD of Path Weights

Problem Statement

You are given a directed graph GG with NN vertices and MM edges. The vertices are numbered 1,2,ldots,N1, 2, \\ldots, N. The ii-th edge is directed from Vertex aia_i to Vertex bib_i, where ai<bia_i < b_i.

The beautifulness of a sequence of positive integers W=(W1,W2,ldots,WN)W = (W_1, W_2, \\ldots, W_N) is defined as the maximum positive integer xx that satisfies the following:

  • For every path (v1,ldots,vk)(v_1, \\ldots, v_k) (v1=1,vk=Nv_1 = 1, v_k = N) from Vertex 11 to Vertex NN in GG, sumi=1kWvi\\sum_{i=1}^k W_{v_i} is a multiple of xx.

You are given an integer sequence A=(A1,A2,ldots,AN)A = (A_1, A_2, \\ldots, A_N). Find the maximum beautifulness of a sequence of positive integers W=(W1,ldots,WN)W = (W_1, \\ldots, W_N) such that Aineq1impliesWi=AiA_i \\neq -1 \\implies W_i = A_i. If the maximum beautifulness does not exist, print -1.

Constraints

  • 2leqNleq3times1052\\leq N\\leq 3\\times 10^5
  • 1leqMleq3times1051\\leq M\\leq 3\\times 10^5
  • 1leqai<bileqN1\\leq a_i < b_i \\leq N
  • (ai,bi)neq(aj,bj)(a_i,b_i)\\neq (a_j,b_j) if ineqji\\neq j
  • In the given graph GG, there is a path from Vertex 11 to Vertex NN.
  • Ai=1A_i = -1 or 1leqAileq10121\\leq A_i\\leq 10^{12}

Input

Input is given from Standard Input in the following format:

NN MM a1a_1 b1b_1 vdots\\vdots aMa_M bMb_M A1A_1 A2A_2 ldots\\ldots ANA_N

Output

Print the maximum beautifulness of a sequence of positive integers WW. If the maximum beautifulness does not exist, print -1.


Sample Input 1

4 4
1 2
1 3
2 4
3 4
-1 3 7 -1

Sample Output 1

4

There are two paths from Vertex 11 to Vertex NN: (1,2,4)(1,2,4) and (1,3,4)(1,3,4). For instance, W=(5,3,7,8)W = (5, 3, 7, 8) has a beautifulness of 44. Indeed, both W1+W2+W4=16W_1 + W_2 + W_4 = 16 and W1+W3+W4=20W_1 + W_3 + W_4 = 20 are multiples of 44.


Sample Input 2

4 5
1 2
1 3
2 4
3 4
1 4
-1 3 7 -1

Sample Output 2

1

There are three paths from Vertex 11 to Vertex NN: (1,2,4)(1,2,4), (1,3,4)(1,3,4), and (1,4)(1,4). For instance, W=(5,3,7,8)W = (5, 3, 7, 8) has a beautifulness of 11.


Sample Input 3

4 4
1 2
1 3
2 4
3 4
3 -1 -1 7

Sample Output 3

-1

For instance, W=(3,10100,10100,7)W = (3, 10^{100}, 10^{100}, 7) has a beautifulness of 10100+1010^{100}+10. Since you can increase the beautifulness of WW as much as you want, there is no maximum beautifulness.


Sample Input 4

5 5
1 3
3 5
2 3
3 4
1 4
2 -1 3 -1 4

Sample Output 4

9