stablegradients/pie-gem5-pairs huggingface dataset:
// Slower C++ solution:
#include<bits/stdc++.h>

using namespace std;

typedef long long ll;

const int N=1e5+5;



#define endl "\n"



bool vis[N][3];

vector<int> G[N];

int n,m,S,T;



struct node{

	int now,t;

};



void bfs(){

	node p,q;

	p.now=S;

	p.t=0;

	vis[p.now][p.t]=1;

	queue<node> Q;

	Q.push(p);

	while(!Q.empty()){

		q=Q.front();

		Q.pop();

		if(q.now==T&&q.t%3==0) {

			cout<<q.t/3<<endl;

			return;

		}

		int t=(q.t+1)%3,v=q.now,u;

		for(int i=0;i<G[v].size();i++){

			u=G[v][i];

			if(!vis[u][t]){

				vis[u][t]=1;

				p.now=u;

				p.t=q.t+1;

				Q.push(p);

			}

		}

	}

	cout<<-1<<endl;

}



int main(){

	ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);

	cin>>n>>m;

	memset(vis,0,sizeof(vis));

	int v,u;

	while(m--){

		cin>>v>>u;

		G[v].push_back(u);

	}

	cin>>S>>T;

	bfs();

	return 0;

}

// Faster C++ solution for the same problem (measured speedup: 3.780175082945939x):
#include <bits/stdc++.h>

using namespace std;

#define ld long double

#define ll long long

#define pb push_back

#define F first

#define S second

const int mod = 1e9+7, A = 4e4+5;

int n, m;

vector<vector<int>> v;

int bfs(int src, int dst)

{

    int ans[n + 1][3];

    for (int i = 0; i <= n; i++)

        for (int j = 0; j < 3; j++)

            ans[i][j] = 1e9;

    queue<pair<int, int>> q;

    q.push({src, 0});

    ans[src][0] = 0;

    while(!q.empty())

    {

        int node = q.front().F, md = q.front().S;

        q.pop();

        for (auto to : v[node])

        {

            int nxt_md = (md + 1) % 3;

            if (ans[to][nxt_md] == 1e9)

            {

                q.push({to, nxt_md});

                ans[to][nxt_md] = ans[node][md] + 1;

            }

        }

    }

    return (ans[dst][0] == 1e9 ? -1 : ans[dst][0] / 3);

}

int main()

{

    ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0);

    cin >> n >> m;

    v.resize(n + 1);

    for (int i = 0; i < m; i++)

    {

        int x, y;

        cin >> x >> y;

        v[x].pb(y);

    }

    int s, t;

    cin >> s >> t;

    cout << bfs(s, t);

    return 0;

}
// Slower C++ solution:
#include<bits/stdc++.h>

using namespace std;

typedef long long ll;

const ll MAXN=2e5+5;

const ll MOD=1e9+7;

const ll INF=1e18;

ll n,m,s,t,vis1[MAXN],vis2[MAXN],vis3[MAXN];

vector<ll>G[MAXN];

void solve()

{

    queue<pair<ll,ll> >q;

    q.push(make_pair(s,0));

    while(!q.empty())

    {

        pair<ll,ll> up=q.front();

        ll u=up.first,step=up.second;

        if(u==t)

        {

            printf("%lld\n",step);

            return;

        }

        q.pop();

        for(ll i=0;i<G[u].size();i++)

        {

            ll v1=G[u][i];

            if(vis1[v1])continue;

            vis1[v1]=1;

            for(ll j=0;j<G[v1].size();j++)

            {

                ll v2=G[v1][j];

                if(vis2[v2])continue;

                vis2[v2]=1;

                for(ll k=0;k<G[v2].size();k++)

                {

                    ll v3=G[v2][k];

                    if(vis3[v3])continue;

                    q.push(make_pair(v3,step+1));

                    vis3[v3]=1;

                }

            }

        }

    }

    printf("-1\n");

}

int main()

{

//    freopen("c://duipai//data.txt","r",stdin);

//    freopen("c://duipai//wa.txt","w",stdout);

    while(~scanf("%lld %lld",&n,&m))

    {

//        string ss;

//        cin>>ss;

//        cout<<ss<<endl;

       for(ll i=1;i<=m;i++)

       {

           ll u,v;

           scanf("%lld %lld",&u,&v);

           G[u].push_back(v);

       }

       scanf("%lld %lld",&s,&t);

       solve();

    }

}

/*

5

76578820211343624  293297008968192 189152283861189120 324294649604739072 20266198324215808

*/


// Faster C++ solution for the same problem (measured speedup: 1.8304291233012637x):
#include "bits/stdc++.h"



typedef long long ll;



const int size = 100005;



std::vector<int> g[size];



bool visited[size][3];



int dfs(int s, int e)

{

	// Vertex, mod

	std::queue<std::pair<int, int>> q;

	q.push({ s, 0 });

	while (!q.empty())

	{

		int vert = q.front().first;

		int ken = q.front().second;

		q.pop();

		if (vert == e && ken % 3 == 0)

		{

			return ken / 3;

		}

		if (visited[vert][ken % 3])

		{

			continue;

		}

		visited[vert][ken % 3] = true;

		for (int nV : g[vert])

		{

			q.push({ nV, ken + 1 });

		}

	}

	return -1;

}



int main()

{

	int n, m;

	scanf("%d%d", &n, &m);

	for (int i = 0; i < m; ++i)

	{

		int u, v;

		scanf("%d%d", &u, &v);

		g[u].push_back(v);

	}

	int s, e;

	scanf("%d%d", &s, &e);

	printf("%d\n", dfs(s, e));

	return 0;

}
// Slower C++ solution:
#include<bits/stdc++.h>

using namespace std;

typedef long long ll;

#define rep(i, n) for(int i = 0; i < (n); i++)

#define rep1(i, n) for(int i = 1; i <= (n); i++)

#define co(x) cout << (x) << "\n"

#define cosp(x) cout << (x) << " "

#define ce(x) cerr << (x) << "\n"

#define cesp(x) cerr << (x) << " "

#define pb push_back

#define mp make_pair

#define Would

#define you

#define please



#define PT pair<int, int>



int main() {

	cin.tie(0);

	ios::sync_with_stdio(false);





	int N, M;

	cin >> N >> M;

	int to[100001], ne[100001], he[100001] = {};

	rep1(i, M) {

		int u, v;

		cin >> u >> v;

		to[i] = v;

		ne[i] = he[u];

		he[u] = i;

	}

	int S, T;

	cin >> S >> T;



	int D[300004];

	rep(i, 300004) D[i] = 1e9;

	D[3 * S] = 0;



	priority_queue<PT, vector<PT>, greater<PT>> que;

	que.push(mp(0, 3 * S));

	while (que.size()) {

		PT p = que.top();

		que.pop();

		int i = p.second;

		if (D[i] != p.first) continue;

		int i2 = i / 3;

		for (int j = he[i2]; j; j = ne[j]) {

			int d = D[i] + 1;

			int j2 = to[j] * 3 + (i + 1) % 3;

			if (D[j2] > d) {

				D[j2] = d;

				que.push(mp(d, j2));

			}

		}

	}



	if (D[3 * T] == 1e9) co(-1);

	else co(D[3 * T] / 3);



	Would you please return 0;

}

// Faster C++ solution for the same problem (measured speedup: 1.4333848218104923x):
//#include<bits/stdc++.h>

#include<cstdio>

using namespace std;

typedef long long ll;

#define rep(i, n) for(int i = 0; i < (n); i++)

#define rep1(i, n) for(int i = 1; i <= (n); i++)

#define co(x) cout << (x) << "\n"

#define cosp(x) cout << (x) << " "

#define ce(x) cerr << (x) << "\n"

#define cesp(x) cerr << (x) << " "

#define pb push_back

#define mp make_pair

#define Would

#define you

#define please



const int cm = 1 << 17;

char cn[cm], * ci = cn + cm;

inline char getcha() {

	if (ci - cn == cm) { fread(cn, 1, cm, stdin); ci = cn; }

	return *ci++;

}

inline int getint() {

	char C = getcha();

	int A = C - '0';

	while ((C = getcha()) >= '0') A = A * 10 + C - '0';

	return A;

}



int main() {

	//cin.tie(0);

	//ios::sync_with_stdio(false);





	int N = getint(), M = getint();

	int to[100001], ne[100001], he[100001] = {};

	rep1(i, M) {

		int u = getint();

		to[i] = getint();

		ne[i] = he[u];

		he[u] = i;

	}

	int S = getint(), T = getint();



	int D[300004];

	rep(i, 300004) D[i] = 1e9;

	D[3 * S] = 0;



	int q[300000];

	int k1 = 0;

	int k2 = 1;

	q[0] = 3 * S;

	for (int k1 = 0; k1 < k2; k1++) {

		int i = q[k1];

		int i2 = i / 3;

		int d = D[i] + 1;

		for (int j = he[i2]; j; j = ne[j]) {

			int j2 = to[j] * 3 + (i + 1) % 3;

			if (D[j2] > d) {

				D[j2] = d;

				q[k2++] = j2;

			}

		}

	}



	if (D[3 * T] == 1e9) printf("-1");

	else printf("%d", D[3 * T] / 3);



	Would you please return 0;

}
// Slower C++ solution:
#include<bits/stdc++.h>

using namespace std;

typedef long long ll;

#define rep(i, n) for(int i = 0; i < (n); i++)

#define rep1(i, n) for(int i = 1; i <= (n); i++)

#define co(x) cout << (x) << "\n"

#define cosp(x) cout << (x) << " "

#define ce(x) cerr << (x) << "\n"

#define cesp(x) cerr << (x) << " "

#define pb push_back

#define mp make_pair

#define Would

#define you

#define please



#define PT pair<int, int>



int main() {

	cin.tie(0);

	ios::sync_with_stdio(false);





	int N, M;

	cin >> N >> M;

	int to[100001], ne[100001], he[100001] = {};

	rep1(i, M) {

		int u, v;

		cin >> u >> v;

		to[i] = v;

		ne[i] = he[u];

		he[u] = i;

	}

	int S, T;

	cin >> S >> T;



	int D[300004];

	rep(i, 300004) D[i] = 1e9;

	D[3 * S] = 0;



	queue<PT> que;

	que.push(mp(0, 3 * S));

	while (que.size()) {

		PT p = que.front();

		que.pop();

		int i = p.second;

		if (D[i] < p.first) continue;

		int i2 = i / 3;

		for (int j = he[i2]; j; j = ne[j]) {

			int d = D[i] + 1;

			int j2 = to[j] * 3 + (i + 1) % 3;

			if (D[j2] > d) {

				D[j2] = d;

				que.push(mp(d, j2));

			}

		}

	}



	if (D[3 * T] == 1e9) co(-1);

	else co(D[3 * T] / 3);



	Would you please return 0;

}

// Faster C++ solution for the same problem (measured speedup: 1.4330720739260783x):
//#include<bits/stdc++.h>

#include<cstdio>

using namespace std;

typedef long long ll;

#define rep(i, n) for(int i = 0; i < (n); i++)

#define rep1(i, n) for(int i = 1; i <= (n); i++)

#define co(x) cout << (x) << "\n"

#define cosp(x) cout << (x) << " "

#define ce(x) cerr << (x) << "\n"

#define cesp(x) cerr << (x) << " "

#define pb push_back

#define mp make_pair

#define Would

#define you

#define please



const int cm = 1 << 17;

char cn[cm], * ci = cn + cm;

inline char getcha() {

	if (ci - cn == cm) { fread(cn, 1, cm, stdin); ci = cn; }

	return *ci++;

}

inline int getint() {

	char C = getcha();

	int A = C - '0';

	while ((C = getcha()) >= '0') A = A * 10 + C - '0';

	return A;

}



int main() {

	//cin.tie(0);

	//ios::sync_with_stdio(false);





	int N = getint(), M = getint();

	int to[100001], ne[100001], he[100001] = {};

	rep1(i, M) {

		int u = getint();

		to[i] = getint();

		ne[i] = he[u];

		he[u] = i;

	}

	int S = getint(), T = getint();



	int D[300004];

	rep(i, 300004) D[i] = 1e9;

	D[3 * S] = 0;



	int q[300000];

	int k1 = 0;

	int k2 = 1;

	q[0] = 3 * S;

	for (int k1 = 0; k1 < k2; k1++) {

		int i = q[k1];

		int i2 = i / 3;

		int d = D[i] + 1;

		for (int j = he[i2]; j; j = ne[j]) {

			int j2 = to[j] * 3 + (i + 1) % 3;

			if (D[j2] > d) {

				D[j2] = d;

				q[k2++] = j2;

			}

		}

	}



	if (D[3 * T] == 1e9) printf("-1");

	else printf("%d", D[3 * T] / 3);



	Would you please return 0;

}
// Slower C++ solution:
#include<bits/stdc++.h>

using namespace std;

typedef long long ll;

#define rep(i, n) for(int i = 0; i < (n); i++)

#define rep1(i, n) for(int i = 1; i <= (n); i++)

#define co(x) cout << (x) << "\n"

#define cosp(x) cout << (x) << " "

#define ce(x) cerr << (x) << "\n"

#define cesp(x) cerr << (x) << " "

#define pb push_back

#define mp make_pair

#define Would

#define you

#define please



int main() {

	cin.tie(0);

	ios::sync_with_stdio(false);





	int N, M;

	cin >> N >> M;

	int to[100001], ne[100001], he[100001] = {};

	rep1(i, M) {

		int u, v;

		cin >> u >> v;

		to[i] = v;

		ne[i] = he[u];

		he[u] = i;

	}

	int S, T;

	cin >> S >> T;



	int D[300004];

	rep(i, 300004) D[i] = 1e9;

	D[3 * S] = 0;



	queue<ll> que;

	que.push(3 * S);

	while (que.size()) {

		ll p = que.front();

		que.pop();

		int i = p & ((1 << 20) - 1);

		int d = p >> 20;

		if (D[i] < d) continue;

		int i2 = i / 3;

		d++;

		for (int j = he[i2]; j; j = ne[j]) {

			int j2 = to[j] * 3 + (i + 1) % 3;

			if (D[j2] > d) {

				D[j2] = d;

				que.push(((ll)d << 20) + j2);

			}

		}

	}



	if (D[3 * T] == 1e9) co(-1);

	else co(D[3 * T] / 3);



	Would you please return 0;

}

// Faster C++ solution for the same problem (measured speedup: 1.437393445725909x):
//#include<bits/stdc++.h>

#include<cstdio>

using namespace std;

typedef long long ll;

#define rep(i, n) for(int i = 0; i < (n); i++)

#define rep1(i, n) for(int i = 1; i <= (n); i++)

#define co(x) cout << (x) << "\n"

#define cosp(x) cout << (x) << " "

#define ce(x) cerr << (x) << "\n"

#define cesp(x) cerr << (x) << " "

#define pb push_back

#define mp make_pair

#define Would

#define you

#define please



const int cm = 1 << 17;

char cn[cm], * ci = cn + cm;

inline char getcha() {

	if (ci - cn == cm) { fread(cn, 1, cm, stdin); ci = cn; }

	return *ci++;

}

inline int getint() {

	char C = getcha();

	int A = C - '0';

	while ((C = getcha()) >= '0') A = A * 10 + C - '0';

	return A;

}



int main() {

	//cin.tie(0);

	//ios::sync_with_stdio(false);





	int N = getint(), M = getint();

	int to[100001], ne[100001], he[100001] = {};

	rep1(i, M) {

		int u = getint();

		to[i] = getint();

		ne[i] = he[u];

		he[u] = i;

	}

	int S = getint(), T = getint();



	int D[300004];

	rep(i, 300004) D[i] = 1e9;

	D[3 * S] = 0;



	int q[300000];

	int k1 = 0;

	int k2 = 1;

	q[0] = 3 * S;

	for (int k1 = 0; k1 < k2; k1++) {

		int i = q[k1];

		int i2 = i / 3;

		int d = D[i] + 1;

		for (int j = he[i2]; j; j = ne[j]) {

			int j2 = to[j] * 3 + (i + 1) % 3;

			if (D[j2] > d) {

				D[j2] = d;

				q[k2++] = j2;

			}

		}

	}



	if (D[3 * T] == 1e9) printf("-1");

	else printf("%d", D[3 * T] / 3);



	Would you please return 0;

}
// Slower C++ solution:
#include<bits/stdc++.h>

using namespace std;

typedef long long ll;

#define rep(i, n) for(int i = 0; i < (n); i++)

#define rep1(i, n) for(int i = 1; i <= (n); i++)

#define co(x) cout << (x) << "\n"

#define cosp(x) cout << (x) << " "

#define ce(x) cerr << (x) << "\n"

#define cesp(x) cerr << (x) << " "

#define pb push_back

#define mp make_pair

#define Would

#define you

#define please



int main() {

	cin.tie(0);

	ios::sync_with_stdio(false);





	int N, M;

	cin >> N >> M;

	int to[100001], ne[100001], he[100001] = {};

	rep1(i, M) {

		int u, v;

		cin >> u >> v;

		to[i] = v;

		ne[i] = he[u];

		he[u] = i;

	}

	int S, T;

	cin >> S >> T;



	int D[300004];

	rep(i, 300004) D[i] = 1e9;

	D[3 * S] = 0;



	queue<int> que;

	que.push(3 * S);

	while (que.size()) {

		int i = que.front();

		que.pop();

		int i2 = i / 3;

		int d = D[i] + 1;

		for (int j = he[i2]; j; j = ne[j]) {

			int j2 = to[j] * 3 + (i + 1) % 3;

			if (D[j2] > d) {

				D[j2] = d;

				que.push(j2);

			}

		}

	}



	if (D[3 * T] == 1e9) co(-1);

	else co(D[3 * T] / 3);



	Would you please return 0;

}

// Faster C++ solution for the same problem (measured speedup: 1.4372883685641462x):
//#include<bits/stdc++.h>

#include<cstdio>

using namespace std;

typedef long long ll;

#define rep(i, n) for(int i = 0; i < (n); i++)

#define rep1(i, n) for(int i = 1; i <= (n); i++)

#define co(x) cout << (x) << "\n"

#define cosp(x) cout << (x) << " "

#define ce(x) cerr << (x) << "\n"

#define cesp(x) cerr << (x) << " "

#define pb push_back

#define mp make_pair

#define Would

#define you

#define please



const int cm = 1 << 17;

char cn[cm], * ci = cn + cm;

inline char getcha() {

	if (ci - cn == cm) { fread(cn, 1, cm, stdin); ci = cn; }

	return *ci++;

}

inline int getint() {

	char C = getcha();

	int A = C - '0';

	while ((C = getcha()) >= '0') A = A * 10 + C - '0';

	return A;

}



int main() {

	//cin.tie(0);

	//ios::sync_with_stdio(false);





	int N = getint(), M = getint();

	int to[100001], ne[100001], he[100001] = {};

	rep1(i, M) {

		int u = getint();

		to[i] = getint();

		ne[i] = he[u];

		he[u] = i;

	}

	int S = getint(), T = getint();



	int D[300004];

	rep(i, 300004) D[i] = 1e9;

	D[3 * S] = 0;



	int q[300000];

	int k1 = 0;

	int k2 = 1;

	q[0] = 3 * S;

	for (int k1 = 0; k1 < k2; k1++) {

		int i = q[k1];

		int i2 = i / 3;

		int d = D[i] + 1;

		for (int j = he[i2]; j; j = ne[j]) {

			int j2 = to[j] * 3 + (i + 1) % 3;

			if (D[j2] > d) {

				D[j2] = d;

				q[k2++] = j2;

			}

		}

	}



	if (D[3 * T] == 1e9) printf("-1");

	else printf("%d", D[3 * T] / 3);



	Would you please return 0;

}
// Slower C++ solution:
//#include<bits/stdc++.h>

#include<cstdio>

#include<queue>

using namespace std;

typedef long long ll;

#define rep(i, n) for(int i = 0; i < (n); i++)

#define rep1(i, n) for(int i = 1; i <= (n); i++)

#define co(x) cout << (x) << "\n"

#define cosp(x) cout << (x) << " "

#define ce(x) cerr << (x) << "\n"

#define cesp(x) cerr << (x) << " "

#define pb push_back

#define mp make_pair

#define Would

#define you

#define please



const int cm = 1 << 17;

char cn[cm], * ci = cn + cm;

inline char getcha() {

	if (ci - cn == cm) { fread(cn, 1, cm, stdin); ci = cn; }

	return *ci++;

}

inline int getint() {

	char C = getcha();

	int A = C - '0';

	while ((C = getcha()) >= '0') A = A * 10 + C - '0';

	return A;

}



int main() {

	//cin.tie(0);

	//ios::sync_with_stdio(false);





	int N = getint(), M = getint();

	int to[100001], ne[100001], he[100001] = {};

	rep1(i, M) {

		int u = getint();

		to[i] = getint();

		ne[i] = he[u];

		he[u] = i;

	}

	int S = getint(), T = getint();



	int D[300004];

	rep(i, 300004) D[i] = 1e9;

	D[3 * S] = 0;



	queue<int> que;

	que.push(3 * S);

	while (que.size()) {

		int i = que.front();

		que.pop();

		int i2 = i / 3;

		int d = D[i] + 1;

		for (int j = he[i2]; j; j = ne[j]) {

			int j2 = to[j] * 3 + (i + 1) % 3;

			if (D[j2] > d) {

				D[j2] = d;

				que.push(j2);

			}

		}

	}



	if (D[3 * T] == 1e9) printf("-1");

	else printf("%d", D[3 * T] / 3);



	Would you please return 0;

}

// Faster C++ solution for the same problem (measured speedup: 1.3771706926138463x):
//#include<bits/stdc++.h>

#include<cstdio>

using namespace std;

typedef long long ll;

#define rep(i, n) for(int i = 0; i < (n); i++)

#define rep1(i, n) for(int i = 1; i <= (n); i++)

#define co(x) cout << (x) << "\n"

#define cosp(x) cout << (x) << " "

#define ce(x) cerr << (x) << "\n"

#define cesp(x) cerr << (x) << " "

#define pb push_back

#define mp make_pair

#define Would

#define you

#define please



const int cm = 1 << 17;

char cn[cm], * ci = cn + cm;

inline char getcha() {

	if (ci - cn == cm) { fread(cn, 1, cm, stdin); ci = cn; }

	return *ci++;

}

inline int getint() {

	char C = getcha();

	int A = C - '0';

	while ((C = getcha()) >= '0') A = A * 10 + C - '0';

	return A;

}



int main() {

	//cin.tie(0);

	//ios::sync_with_stdio(false);





	int N = getint(), M = getint();

	int to[100001], ne[100001], he[100001] = {};

	rep1(i, M) {

		int u = getint();

		to[i] = getint();

		ne[i] = he[u];

		he[u] = i;

	}

	int S = getint(), T = getint();



	int D[300004];

	rep(i, 300004) D[i] = 1e9;

	D[3 * S] = 0;



	int q[300000];

	int k1 = 0;

	int k2 = 1;

	q[0] = 3 * S;

	for (int k1 = 0; k1 < k2; k1++) {

		int i = q[k1];

		int i2 = i / 3;

		int d = D[i] + 1;

		for (int j = he[i2]; j; j = ne[j]) {

			int j2 = to[j] * 3 + (i + 1) % 3;

			if (D[j2] > d) {

				D[j2] = d;

				q[k2++] = j2;

			}

		}

	}



	if (D[3 * T] == 1e9) printf("-1");

	else printf("%d", D[3 * T] / 3);



	Would you please return 0;

}
// Slower C++ solution:
#include <bits/stdc++.h>

using namespace std;



#define IOS ios::sync_with_stdio(false); cin.tie(0);

#define FOR(i,s,n) for(int i = s; i < (n); i++)

#define REP(i,n) FOR(i,0,n)

#define ALL(n) (n).begin(), (n).end()

#define RALL(n) (n).rbegin(), (n).rend()

using ll = long long;

using ull = unsigned long long;





int main(void)

{

    IOS

    int n,m; cin >> n >> m;

    vector<set<int>> vs(n+1);

    REP(i,m) {

        int u,v; cin >> u >>v;

        vs[u].emplace(v);

    }

    int s,t; cin >> s >> t;



    int dp[3][100010];

    REP(i,3) REP(j,100010) dp[i][j] = 0;

    dp[0][s] = 1;



    set<int> next(vs[s]);



    int ans = 1;

    while(true) {

        FOR(i,0,3) {

            set<int> tmp;

            //cerr << next.size() << endl;

            int j = i == 2 ? 0:i+1;

            for(auto x:next) {

                if (dp[j][x]!=1) {

                    dp[j][x] = 1;

                    tmp.insert(ALL(vs[x]));

                }

            }

            next = tmp;

        }

        if (dp[0][t]== 1) break;

        //cerr << next.size()<<endl;

        if (next.size() == 0) {

            ans = -1;

            break;

        }

        ans++;

    }



    cout << ans << endl;



    return 0;

}


// Faster C++ solution for the same problem (measured speedup: 1.6913568028110402x):
#include <bits/stdc++.h>

using namespace std;



#define IOS ios::sync_with_stdio(false); cin.tie(0);

#define FOR(i,s,n) for(int i = s; i < (n); i++)

#define REP(i,n) FOR(i,0,n)

#define ALL(n) (n).begin(), (n).end()

#define RALL(n) (n).rbegin(), (n).rend()

using ll = long long;

using ull = unsigned long long;





int main(void)

{

    IOS

    int n,m; cin >> n >> m;

    vector<set<int>> vs(n+1);

    REP(i,m) {

        int u,v; cin >> u >>v;

        vs[u].emplace(v);

    }

    int s,t; cin >> s >> t;



    bool dp[3][100010];

    memset(dp,0,sizeof dp);

    dp[0][s] = 1;



    set<int> next(vs[s]);



    int ans = 1;

    while(true) {

        FOR(i,0,3) {

            set<int> tmp;

            //cerr << next.size() << endl;

            int j = i == 2 ? 0:i+1;

            for(auto x:next) {

                if (dp[j][x]!=1) {

                    dp[j][x] = 1;

                    tmp.insert(ALL(vs[x]));

                }

            }

            next = tmp;

        }

        if (dp[0][t]== 1) break;

        //cerr << next.size()<<endl;

        if (next.size() == 0) {

            ans = -1;

            break;

        }

        ans++;

    }



    cout << ans << endl;



    return 0;

}
// Slower C++ solution:
#include <bits/stdc++.h>

using namespace std;



#define IOS ios::sync_with_stdio(false); cin.tie(0);

#define FOR(i,s,n) for(int i = s; i < (n); i++)

#define REP(i,n) FOR(i,0,n)

#define ALL(n) (n).begin(), (n).end()

#define RALL(n) (n).rbegin(), (n).rend()

using ll = long long;

using ull = unsigned long long;





int main(void)

{

    IOS

    int n,m; cin >> n >> m;

    vector<set<int>> vs(n+1);

    REP(i,m) {

        int u,v; cin >> u >>v;

        vs[u].emplace(v);

    }

    int s,t; cin >> s >> t;



    int dp[3][100010];

    memset(dp,0,sizeof dp);

    dp[0][s] = 1;



    set<int> next(vs[s]);



    int ans = 1;

    while(true) {

        FOR(i,0,3) {

            set<int> tmp;

            //cerr << next.size() << endl;

            int j = i == 2 ? 0:i+1;

            for(auto x:next) {

                if (dp[j][x]!=1) {

                    dp[j][x] = 1;

                    tmp.insert(ALL(vs[x]));

                }

            }

            next = tmp;

        }

        if (dp[0][t]== 1) break;

        //cerr << next.size()<<endl;

        if (next.size() == 0) {

            ans = -1;

            break;

        }

        ans++;

    }



    cout << ans << endl;



    return 0;

}


// Faster C++ solution for the same problem (measured speedup: 1.6913555559658306x):
#include <bits/stdc++.h>

using namespace std;



#define IOS ios::sync_with_stdio(false); cin.tie(0);

#define FOR(i,s,n) for(int i = s; i < (n); i++)

#define REP(i,n) FOR(i,0,n)

#define ALL(n) (n).begin(), (n).end()

#define RALL(n) (n).rbegin(), (n).rend()

using ll = long long;

using ull = unsigned long long;





int main(void)

{

    IOS

    int n,m; cin >> n >> m;

    vector<set<int>> vs(n+1);

    REP(i,m) {

        int u,v; cin >> u >>v;

        vs[u].emplace(v);

    }

    int s,t; cin >> s >> t;



    bool dp[3][100010];

    memset(dp,0,sizeof dp);

    dp[0][s] = 1;



    set<int> next(vs[s]);



    int ans = 1;

    while(true) {

        FOR(i,0,3) {

            set<int> tmp;

            //cerr << next.size() << endl;

            int j = i == 2 ? 0:i+1;

            for(auto x:next) {

                if (dp[j][x]!=1) {

                    dp[j][x] = 1;

                    tmp.insert(ALL(vs[x]));

                }

            }

            next = tmp;

        }

        if (dp[0][t]== 1) break;

        //cerr << next.size()<<endl;

        if (next.size() == 0) {

            ans = -1;

            break;

        }

        ans++;

    }



    cout << ans << endl;



    return 0;

}