atcoder#ARC144E. [ARC144E] GCD of Path Weights

[ARC144E] GCD of Path Weights

Score : 800800 points

Problem Statement

You are given a directed graph GG with NN vertices and MM edges. The vertices are numbered 1,2,,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,,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,,vk)(v_1, \ldots, v_k) (v1=1,vk=Nv_1 = 1, v_k = N) from Vertex 11 to Vertex NN in GG, i=1kWvi\sum_{i=1}^k W_{v_i} is a multiple of xx.

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

Constraints

  • 2N3×1052\leq N\leq 3\times 10^5
  • 1M3×1051\leq M\leq 3\times 10^5
  • 1ai<biN1\leq a_i < b_i \leq N
  • (ai,bi)(aj,bj)(a_i,b_i)\neq (a_j,b_j) if iji\neq j
  • In the given graph GG, there is a path from Vertex 11 to Vertex NN.
  • Ai=1A_i = -1 or 1Ai10121\leq A_i\leq 10^{12}

Input

Input is given from Standard Input in the following format:

NN MM

a1a_1 b1b_1

\vdots

aMa_M bMb_M

A1A_1 A2A_2 \ldots ANA_N

Output

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

4 4
1 2
1 3
2 4
3 4
-1 3 7 -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.

4 5
1 2
1 3
2 4
3 4
1 4
-1 3 7 -1
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.

4 4
1 2
1 3
2 4
3 4
3 -1 -1 7
-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.

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