Problem:
Score : 500 points

Problem Statement

Ken loves ken-ken-pa (Japanese version of hopscotch). Today, he will play it on a directed graph G.
G consists of N vertices numbered 1 to N, and M edges. The i-th edge points from Vertex u_i to Vertex v_i.

First, Ken stands on Vertex S. He wants to reach Vertex T by repeating ken-ken-pa. In one ken-ken-pa, he does the following exactly three times: follow an edge pointing from the vertex on which he is standing.

Determine if he can reach Vertex T by repeating ken-ken-pa. If the answer is yes, find the minimum number of ken-ken-pa needed to reach Vertex T. Note that visiting Vertex T in the middle of a ken-ken-pa does not count as reaching Vertex T by repeating ken-ken-pa.

Constraints

2 \leq N \leq 10^5

0 \leq M \leq \min(10^5, N (N-1))

1 \leq u_i, v_i \leq N(1 \leq i \leq M)

u_i \neq v_i (1 \leq i \leq M)

If i \neq j, (u_i, v_i) \neq (u_j, v_j).

1 \leq S, T \leq N

S \neq T

Input

Input is given from Standard Input in the following format:

N M
u_1 v_1
:
u_M v_M
S T

Output

If Ken cannot reach Vertex T from Vertex S by repeating ken-ken-pa, print -1.
If he can, print the minimum number of ken-ken-pa needed to reach vertex T.

Sample Input 1

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

Sample Output 1

2

Ken can reach Vertex 3 from Vertex 1 in two ken-ken-pa, as follows: 1 \rightarrow 2 \rightarrow 3 \rightarrow 4 in the first ken-ken-pa, then 4 \rightarrow 1 \rightarrow 2 \rightarrow 3 in the second ken-ken-pa. This is the minimum number of ken-ken-pa needed.

Sample Input 2

3 3
1 2
2 3
3 1
1 2

Sample Output 2

-1

Any number of ken-ken-pa will bring Ken back to Vertex 1, so he cannot reach Vertex 2, though he can pass through it in the middle of a ken-ken-pa.

Sample Input 3

2 0
1 2

Sample Output 3

-1

Vertex S and Vertex T may be disconnected.

Sample Input 4

6 8
1 2
2 3
3 4
4 5
5 1
1 4
1 5
4 6
1 6

Sample Output 4

2

Solution in C++14 (GCC 5.4.1) (judge verdict: Wrong Answer)

#include "bits/stdc++.h"
#define _overload3(_1,_2,_3,name,...) name
#define _rep(i,n) repi(i,0,n)
#define repi(i,a,b) for(int i=int(a),i##_len=(b);i<i##_len;++i)
#define MSVC_UNKO(x) x
#define rep(...) MSVC_UNKO(_overload3(__VA_ARGS__,repi,_rep,_rep)(__VA_ARGS__))
#define all(c) c.begin(),c.end()
#define mp make_pair
#define write(x) cout<<(x)<<"\n"
using namespace std; typedef long long ll; typedef vector<int> vi; typedef vector<ll> vll;
template<class T>using vv = vector<vector<T>>; template<class T>auto vvec(int n, int m, T v) { return vv<T>(n, vector<T>(m, v)); }
template<class T>bool chmax(T& a, const T& b) { return a < b ? a = b, 1 : 0; }
template<class T>bool chmin(T& a, const T& b) { return b < a ? a = b, 1 : 0; }
constexpr int INF = 1 << 29, MOD = int(1e9) + 7; constexpr ll LINF = 1LL << 60;
struct aaa { aaa() { cin.tie(0); ios::sync_with_stdio(0); cout << fixed << setprecision(10); }; }aaaa;

using TP = pair<pair<int, int>, int>;
//using Q = pair<pair<int, int>, TP>;

int N, M;
vv<int> graph, kkpGraph;
vector<ll> dist;

//vector<int> in;
vv<bool> visited;

void dfs(int v, TP hist, int d) {
    //visited[v]++;

    if (/*d % 3 == 0 && */hist.first.first != -1) {
        kkpGraph[hist.first.first].push_back(v);
    }

    for (int u : graph[v]) {
        if (!visited[u][d]) {
            visited[u][d] = true;
            dfs(u, TP(mp(hist.first.second, hist.second), v), d + 1);
        }
        //if (used[v][u] < 3) {
        //    used[v][u]++;
        //    dfs(u, TP(mp(hist.first.second, hist.second), v), d + 1);
        //}
    }
}

//void bfs(int s) {
//    queue<Q> que;
//    que.emplace(mp(s, 0), TP({ -1, -1 }, -1));
//
//    while (!que.empty()) {
//        auto q = que.front();
//        que.pop();
//        int v = q.first.first;
//        int d = q.first.second;
//        auto& hist = q.second;
//
//        if (/*d % 3 == 0 && */hist.first.first != -1) {
//            kkpGraph[hist.first.first].push_back(v);
//        }
//
//        for (int u : graph[v]) {
//            if (find(all(used[v]), mp(u, d)) == used[v].end()) {
//                used[v].insert(mp(u, d));
//                que.emplace(mp(u, (d + 1) % 3), TP(mp(hist.first.second, hist.second), v));
//            }
//        }
//    }
//}

void bfs_kkp(int s) {
    dist.assign(kkpGraph.size(), LINF);
    dist[s] = 0;
    queue<int> que;
    que.push(s);

    while (!que.empty()) {
        int v = que.front(); que.pop();

        for (int e : kkpGraph[v]) {
            if (dist[e] == LINF/*&& other condition*/) {
                dist[e] = dist[v] + 1;
                que.push(e);
            }
        }
    }
}

int main() {
    cin >> N >> M;
    graph.resize(N);
    //in.resize(N);
    rep(i, M) {
        int u, v;
        cin >> u >> v;
        graph[u - 1].push_back(v - 1);
        //in[v - 1]++;
    }
    int S, T;
    cin >> S >> T;

    kkpGraph.resize(N);
    visited = vvec(N, 3, false);
    //used.resize(N);
    dfs(S - 1, TP(mp(-1, -1), -1), 0);
    //bfs(S - 1);

    rep(i, N) {
        sort(all(kkpGraph[i]));
        kkpGraph[i].erase(unique(all(kkpGraph[i])), kkpGraph[i].end());
    }

    bfs_kkp(S - 1);
    if (dist[T - 1] == LINF) {
        write(-1);
    }
    else {
        write(dist[T - 1]);
    }
}