#define  _CRT_SECURE_NO_WARNINGS
#pragma comment (linker, "/STACK:526000000")

#include "bits/stdc++.h"

using namespace std;
typedef string::const_iterator State;
#define eps 1e-11L
#define MAX_MOD 1000000007LL
#define GYAKU 500000004LL

#define MOD 998244353LL
#define seg_size 262144 * 4LL
#define pb push_back
#define mp make_pair
typedef long long ll;
#define REP(a,b) for(long long (a) = 0;(a) < (b);++(a))
#define ALL(x) (x).begin(),(x).end()

void init() {
	iostream::sync_with_stdio(false);
	cout << fixed << setprecision(20);
}

#define int ll
vector<int> vertexs[100001];
int dp[2][100001];
void solve(){
	int n, m, s, t;
	cin >> n >> m >> s >> t;
	REP(i, m) {
		int a, b;
		cin >> a >> b;
		vertexs[a].push_back(b);
		vertexs[b].push_back(a);
	}
	REP(i, n) {
		dp[0][i+1] = 1e9;
		dp[1][i + 1] = 1e9;
	}
	dp[0][s] = 0;
	queue<pair<int, int>> next;
	next.push(mp(0, s));
	while (next.empty() == false) {
		pair<int, int> now = next.front();
		next.pop();
		for (auto x : vertexs[now.second]) {
			if (dp[(now.first + 1) % 2][x] > now.first + 1) {
				dp[(now.first + 1) % 2][x] = now.first + 1;
				next.push(mp((now.first + 1), x));
			}
		}
	}
	int ans = 1e9;
	if (dp[0][t] == 2) {
		cout << 2 << endl;
		return;
	}
	if (dp[1][t] == 1e9) {
		cout << -1 << endl;
		return;
	}
	for (int i = max(3LL, dp[1][t]);;i += 2) {
		int ok = 1;
		for (int q = 2; q * q <= i; ++q) {
			if (i % q == 0) ok = 0;
		}
		if (ok == 1) {
			cout << i << endl;
			return;
		}
	}
}

#undef int
int main() {
	init();
	solve();
}
