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)
    }
}
