Problem:
Problem J. Prime Routing

Fox Jiro is one of the staffs of the ACM-ICPC 2018 Asia Yokohama Regional Contest and is responsible for designing the network for the venue of the contest. His network consists of $N$ computers, which are connected by $M$ cables. The $i$-th cable connects the $a_i$-th computer and the $b_i$-th computer, and it carries data in both directions. Your team will use the $S$-th computer in the contest, and a judge server is the $T$-th computer.

He decided to adjust the routing algorithm of the network to maximize the performance of the contestants through the magical power of prime numbers. In this algorithm, a packet (a unit of data carried by the network) is sent from your computer to the judge server passing through the cables a prime number of times if possible. If it is impossible, the contestants cannot benefit by the magical power of prime numbers. To accomplish this target, a packet is allowed to pass through the same cable multiple times.

You decided to write a program to calculate the minimum number of times a packet from $S$ to $T$ needed to pass through the cables. If the number of times a packet passes through the cables cannot be a prime number, print $-1$.

Input

The input consists of a single test case, formatted as follows.

$N$ $M$ $S$ $T$
$a_1$ $b_1$
$\vdots$
$a_M$ $b_M$

The first line consists of four integers $N, M, S,$ and $T$ ($2 \leq N \leq 10^5, 1 \leq M \leq 10^5, 1 \leq S, T \leq N, S \ne T$). The $i$-th line of the following $M$ lines consists of two integers $a_i$ and $b_i$ ($1 \leq a_i < b_i \leq N$), which means the $i$-th cables connects the $a_i$-th computer and the $b_i$-th computer in the network. You can assume that the network satisfies the following conditions.

The network has no multi-edge, i.e.,$(a_i, b_i) \ne (a_j, b_j)$ for all $i,j$ ($1 \leq i < j \leq M$).

The packets from $N$ computers are reachable to $T$ by passing through some number of cables. The number is not necessarily a prime.

Output

If there are ways such that the number of times a packet sent from $S$ to $T$ passes through the cables is a prime number, print the minimum prime number of times in one line. Otherwise, print $-1$.

Examples

Sample Input 1

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

Output for Sample Input 1

3

Sample Input 2

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

Output for Sample Input 2

-1

Sample Input 3

2 1 1 2
1 2

Output for Sample Input 3

3

Sample Input 4

3 3 1 2
1 2
1 3
2 3

Output for Sample Input 4

2

Solution in Kotlin (judge verdict: Accepted)

import java.util.*
import kotlin.collections.ArrayList


fun primes(upperLimit: Int): List<Int> {
    val isPrime = Array(upperLimit + 1){true}
    for (i in 2 .. upperLimit) {
        if (i * i > upperLimit) break
        if (isPrime[i]) for (j in i * i .. upperLimit step i) {
            isPrime[j] = false
        }
    }
    return (2 .. upperLimit).filter(isPrime::get)
}
fun <T: Comparable<T>> List<T>.lowerBound(value: T): Int {
    var left = 0
    var right = size
    while (left < right) {
        val mid = (left + right) / 2
        if (this[mid] < value) left = mid + 1
        else right = mid
    }
    return right
}
fun main(args: Array<String>?): Unit {
    val (n, m, s, t) = readLine()!!.trim().split(' ').map(String::toInt)
    val nodes = Array(n * 2){ArrayList<Int>()}
    repeat(m){
        val (a, b) = readLine()!!.trim().split(' ').map(String::toInt)
        nodes[a * 2 - 2].add(b * 2 - 1)
        nodes[a * 2 - 1].add(b * 2 - 2)
        nodes[b * 2 - 2].add(a * 2 - 1)
        nodes[b * 2 - 1].add(a * 2 - 2)
    }
    val queue: Queue<Int> = ArrayDeque<Int>().apply { add(s * 2 - 2) }
    val minLength = Array(n * 2){-1}.apply { this[s * 2 - 2] = 0 }
    while (queue.isNotEmpty()) {
        val current = queue.poll()
        for (next in nodes[current]) if (minLength[next] == -1){
            minLength[next] = minLength[current] + 1
            queue.add(next)
        }
    }
    if (minLength[t * 2 - 2] == 2){
        println(2)
    }else if (minLength[t * 2 - 1] != -1) {
        val prime = primes(1000000).drop(1)
        println(prime[prime.lowerBound(minLength[t * 2 - 1])])
    }else {
        println(-1)
    }
}