#arc144e. [arc144_e]GCD of Path Weights
[arc144_e]GCD of Path Weights
Problem Statement
You are given a directed graph with vertices and edges. The vertices are numbered . The -th edge is directed from Vertex to Vertex , where .
The beautifulness of a sequence of positive integers is defined as the maximum positive integer that satisfies the following:
- For every path () from Vertex to Vertex in , is a multiple of .
You are given an integer sequence . Find the maximum beautifulness of a sequence of positive integers such that . If the maximum beautifulness does not exist, print -1
.
Constraints
- if
- In the given graph , there is a path from Vertex to Vertex .
- or
Input
Input is given from Standard Input in the following format:
Output
Print the maximum beautifulness of a sequence of positive integers . 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 to Vertex : and . For instance, has a beautifulness of . Indeed, both and are multiples of .
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 to Vertex : , , and . For instance, has a beautifulness of .
Sample Input 3
4 4
1 2
1 3
2 4
3 4
3 -1 -1 7
Sample Output 3
-1
For instance, has a beautifulness of . Since you can increase the beautifulness of 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