#include <bits/stdc++.h>
using namespace std;


int main() {
  int n, m, s, t;
  cin >> n >> m >> s >> t;
  s--, t--;
  vector<vector<int>> graph(n, vector<int>());
  for (int i = 0; i < m; i++){
    int a, b;
    cin >> a >> b;
    a--, b--;
    graph[a].push_back(b);
    graph[b].push_back(a);
  }

  vector<vector<int>> dists(n, vector<int>(2, 2147483647 / 2));
  dists[s][0] = 0;
  queue<pair<int, int>> q{};
  q.push(make_pair(s, 0));
  while (q.size() >= 1){
    auto elem = q.front();q.pop();
    auto parity = elem.second;
    auto ind = elem.first;
    auto dist = dists[ind][parity];
    for (auto&& item : graph[ind]){
      if (dists[item][parity ^ 1] <= dist + 1) continue;
      dists[item][parity ^ 1] = dist + 1;
      q.push(make_pair(item, parity ^ 1));
    }
  }
  if (dists[t][0] == 2){
    cout << 2 << endl;
    return 0;
  }
  if (dists[t][1] >= 10000000){
    cout << -1 << endl;
    return 0;
  }
  auto res = dists[t][1];
  while (true){
    if (res == 1) goto end;
    for (int i = 2; i * i <= res; i++){
      if (res % i == 0) goto end;
    }
    cout << res << endl;
    return 0;
  end:;
    res += 2;
  }
}

