#include<algorithm>
#include<cassert>
#include<climits>
#include<cmath>
#include<cstring>
#include<deque>
#include<functional>
#include<iomanip>
#include<iostream>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<unordered_map>
#include<unordered_set>
#include<utility>
#include<vector>

using namespace std;

using lint = long long;
using P = pair<int, int>;
using LLP = pair<long long, long long>;

#define REP(i, x, n) for(int i = (x), i##_len = int(n) ; i < i##_len ; ++i)
#define rep(i, n) for(int i = 0, i##_len = int(n) ; i < i##_len ; ++i)
#define reps(i, n) for(int i = 1, i##_len = int(n) ; i <= i##_len ; ++i)
#define rrep(i, n) for(int i = int(n) - 1 ; i >= 0 ; --i)
#define rreps(i, n) for(int i = int(n) ; i > 0 ; --i)
#define SORT(x) sort((x).begin(), (x).end())
#define SORT_INV(x) sort((x).rbegin(), (x).rend())
#define TWINS(x) cout << ((x) ? "Yay!" : ":(") << endl

constexpr int IINF = (1 << 30) - 1;
constexpr long long LLINF = 1LL << 61;
constexpr double EPS = 1e-8;

const int dx4[] = {1, 0, -1, 0}, dy4[] = {0, 1, 0, -1};
const int dx8[] = {1, 1, 0, -1, -1, -1, 0, 1}, dy8[] = {0, -1, -1, -1, 0, 1, 1, 1};

template<typename T>
bool chmax(T& a, T b, bool equal = false){
    if(a < b || equal && a == b){
        a = b;
        return true;
    }
    return false;
}

template<typename T>
bool chmin(T& a, T b, bool equal = false){
    if(b < a || equal && a == b){
        a = b;
        return true;
    }
    return false;
}

struct node{
    int id, cost, isOdd;
};

void eratosthenes(int n, vector<bool> &isPrime){
    isPrime.clear();
    isPrime.resize(max(n + 1, 2), true);
    isPrime[0] = false;
    isPrime[1] = false;

    for(int i = 4 ; i <= n ; i += 2){
        isPrime[i] = false;
    }
    for(int i = 9 ; i <= n ; i += 6){
        isPrime[i] = false;
    }
    int s = 4;
    for(int i = 5 ; i * i <= n ; i += s){
        if(isPrime[i]){
            for(int j = i * i ; j <= n ; j += i){
                isPrime[j] = false;
            }
        }
        s ^= 6;
    }
    return;
}

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);

    int n, m, s, t;
    cin >> n >> m >> s >> t;
    --s;
    --t;

    vector< vector<int> > g(n);
    rep(i, m){
        int a, b;
        cin >> a >> b;
        g[--a].emplace_back(--b);
        g[b].emplace_back(a);
    }

    vector< vector<int> > cost(2, vector<int>(n, IINF));
    cost[0][s] = 0;
    queue<node> que;
    que.push({s, 0, 0});

    while(!que.empty()){
        node now = que.front();
        que.pop();
        if(cost[now.isOdd][now.id] < now.cost){
            continue;
        }
        for(auto to : g[now.id]){
            if(chmin(cost[now.isOdd ^ 1][to], now.cost + 1)){
                que.push({to, now.cost + 1, now.isOdd ^ 1});
            }
        }
    }

    if(cost[0][t] <= 2){
        cout << 2 << endl;
        return 0;
    }

    vector<bool> isPrime;
    eratosthenes(max(n, m) * 100, isPrime);

    int ans = cost[1][t];
    while(ans < max(n, m) * 100 && !isPrime[ans]){
        ans += 2;
    }

    if(ans < max(n, m) * 100){
        cout << ans << endl;
    }else{
        cout << -1 << endl;
    }

    return 0;
}


Measured memory_bytes: 10220