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

using namespace std;

#define ll long long

const int maxn=2e5+5;

vector<int>E[maxn];

int d[maxn][3]; // mod 3=j

int vis[maxn][3];

void bfs(int x)

{

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

    {

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

        {

            d[i][j]=1e9;

        }

    }

    queue<pair<int,int>>q;

    d[x][0]=0;

    vis[x][0]=1;

    q.push({x,0});

    while(!q.empty())

    {

        int now=q.front().first;

        int t=q.front().second;//这一步mod3的值

        int y=(t+1)%3;//下一步mod3的值

        q.pop();

        for(auto&v:E[now])

        {

            if(vis[v][y]) continue;

            vis[v][y]=1;

            d[v][y]=d[now][t]+1;

            q.push({v,y});

        }

    }



}

void dijkstra(int x)

{



}

int main()

{

   ios::sync_with_stdio(false);

   cin.tie(0);

   int n,m;

   cin>>n>>m;

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

   {

       int u,v;

       cin>>u>>v;

       E[u].push_back(v);

   }

   int s,t;

   cin>>s>>t;

   bfs(s);

   if(d[t][0]==1e9)

   {

       cout<<-1<<'\n';

   }

   else

    cout<<d[t][0]/3<<'\n';







}




















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

using namespace std;

#define int long long

#define ar array

const int mxn=1e5+3,INF=1e18;

int n,m;

vector<int> g[mxn];

int vis[mxn][3];

int s,t;

signed main()

{

    ios_base::sync_with_stdio(false);

    cin.tie(0);

    cout.tie(0);

    cin>>n>>m;

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

    {

        int u,v;

        cin>>u>>v;

        g[u].push_back(v);

       // g[v].push_back(u);

    }

    cin>>s>>t;

   // for (int i=1;i<=n;i++) vis[i][0]=vis[i][1]=vis[i][2]=-1;

    queue<ar<int,2>> q;

    q.push({s,0});

    vis[s][0]=0;

    while(!q.empty())

    {

        int u=q.front()[0],time=q.front()[1];

        q.pop();

        for (int v : g[u])

        if (!vis[v][(time+1)%3])

        {

            vis[v][(time+1)%3]=vis[u][time]+1;

            q.push({v,(time+1)%3});

        }

    }

    if (vis[t][0]) cout<<vis[t][0]/3;

    else cout<<-1;

}
// Slower C++ solution:
#include <iostream>

#include <bits/stdc++.h>



using namespace std;

int n,m,s,t;

vector<int> rad[100005];

int dp[100005];

int vdp[3][100005];



struct tor{

    int n,c,t;

    tor(int nn,int cc,int tt){

        n = nn;c = cc;t = tt;

    }

};

queue<tor> que;



void dfs(int now,int cnt,int toq)

{

    if(toq!=0)

    {

        if(vdp[toq][now]<=cnt) return;

        vdp[toq][now] = cnt;

        for(int i=0;i<rad[now].size();++i)

        {

            //dfs(rad[now][i],cnt,toq-1);

            que.push(tor(rad[now][i],cnt,toq-1));

        }

        return;

    }



    if(dp[now]<=cnt) return;

    dp[now] = cnt;

    for(int i=0;i<rad[now].size();++i)

    {

        //dfs(rad[now][i],cnt+1,2);

        que.push(tor(rad[now][i],cnt+1,2));

    }

}



int main()

{

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

    int u,v;

    memset(dp,0x3f,sizeof(dp));

    memset(vdp,0x3f,sizeof(vdp));



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

    {

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

        rad[u].push_back(v);

    }



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



    que.push(tor(s,0,0));

    while(!que.empty())

    {

        tor now = que.front();

        que.pop();

        dfs(now.n,now.c,now.t);

    }

    if(dp[t]==dp[n+1])printf("-1\n");

    else printf("%d\n",dp[t]);

    return 0;

}


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

using namespace std;

#define int long long

#define ar array

const int mxn=1e5+3,INF=1e18;

int n,m;

vector<int> g[mxn];

int vis[mxn][3];

int s,t;

signed main()

{

    ios_base::sync_with_stdio(false);

    cin.tie(0);

    cout.tie(0);

    cin>>n>>m;

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

    {

        int u,v;

        cin>>u>>v;

        g[u].push_back(v);

       // g[v].push_back(u);

    }

    cin>>s>>t;

   // for (int i=1;i<=n;i++) vis[i][0]=vis[i][1]=vis[i][2]=-1;

    queue<ar<int,2>> q;

    q.push({s,0});

    vis[s][0]=0;

    while(!q.empty())

    {

        int u=q.front()[0],time=q.front()[1];

        q.pop();

        for (int v : g[u])

        if (!vis[v][(time+1)%3])

        {

            vis[v][(time+1)%3]=vis[u][time]+1;

            q.push({v,(time+1)%3});

        }

    }

    if (vis[t][0]) cout<<vis[t][0]/3;

    else cout<<-1;

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



using namespace std;



typedef long long ll;



const int N = 2e5+5;



int n;

int m;

int s , t;

vector < int > adj[N];



bool vis[N][3];



struct state{

    int u;

    int dist;

};



int main(){

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

    for(int i = 1; i <= m; i++){

        int u , v;

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

        adj[u].push_back(v);

    }

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

    queue < state > q;

    q.push({s , 0});

    while(!q.empty()){

        int u = q.front().u;

        int dist = q.front().dist;

        q.pop();

        if(u == t && dist % 3 == 0){

            printf("%d\n" , dist / 3);

            return 0;

        }

        vis[u][dist % 3] = 1;

        for(auto v : adj[u]){

            if(!vis[v][ (dist + 1)%3 ]){

                vis[v][ (dist + 1)%3 ] = 1;

                q.push({v , dist + 1});

            }

        }

    }

    puts("-1");

}


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

using namespace std;

#define int long long

#define ar array

const int mxn=1e5+3,INF=1e18;

int n,m;

vector<int> g[mxn];

int vis[mxn][3];

int s,t;

signed main()

{

    ios_base::sync_with_stdio(false);

    cin.tie(0);

    cout.tie(0);

    cin>>n>>m;

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

    {

        int u,v;

        cin>>u>>v;

        g[u].push_back(v);

       // g[v].push_back(u);

    }

    cin>>s>>t;

   // for (int i=1;i<=n;i++) vis[i][0]=vis[i][1]=vis[i][2]=-1;

    queue<ar<int,2>> q;

    q.push({s,0});

    vis[s][0]=0;

    while(!q.empty())

    {

        int u=q.front()[0],time=q.front()[1];

        q.pop();

        for (int v : g[u])

        if (!vis[v][(time+1)%3])

        {

            vis[v][(time+1)%3]=vis[u][time]+1;

            q.push({v,(time+1)%3});

        }

    }

    if (vis[t][0]) cout<<vis[t][0]/3;

    else cout<<-1;

}
// Slower C++ solution:
#include <iostream>

#include <algorithm>

#include <fstream>

#include <utility>

#include <iomanip>

#include <cstring>

#include <string>

#include <sstream>

#include <vector>

#include <queue>

#include <cmath>

#include <map>

#include <set>

#define IO ios::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL);

typedef long long ll;

typedef long double ld;



using namespace std;

const ll INF = 1e18;

const int mod = 1e9 + 7, N = 1e5 + 5, inf = 1e9;

int n, m, s, t, u, v;

vector<int>arr[N];



int bfs()

{

    queue<pair<int, int>>q;

    bool vis[N][5] = {};

    q.push({ s, 0 });

    vis[s][0] = true;

    while(q.size())

    {

        u = q.front().first;

        int ken = q.front().second;

        q.pop();



        if(u == t && ken % 3 == 0)

            return ken / 3;



        for(auto i : arr[u])

        {

            if(!vis[i][(ken + 1) % 3])

            {

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

                vis[i][(ken + 1) % 3] =true;

            }

        }

    }

    return -1;

}



int main()

{

    IO;

    cin >> n >> m;

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

    {

        cin >> u >> v;

        arr[u].push_back(v);

    }

    cin >> s >> t;

    cout << bfs() << '\n';

    return 0;

}

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

using namespace std;

#define int long long

#define ar array

const int mxn=1e5+3,INF=1e18;

int n,m;

vector<int> g[mxn];

int vis[mxn][3];

int s,t;

signed main()

{

    ios_base::sync_with_stdio(false);

    cin.tie(0);

    cout.tie(0);

    cin>>n>>m;

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

    {

        int u,v;

        cin>>u>>v;

        g[u].push_back(v);

       // g[v].push_back(u);

    }

    cin>>s>>t;

   // for (int i=1;i<=n;i++) vis[i][0]=vis[i][1]=vis[i][2]=-1;

    queue<ar<int,2>> q;

    q.push({s,0});

    vis[s][0]=0;

    while(!q.empty())

    {

        int u=q.front()[0],time=q.front()[1];

        q.pop();

        for (int v : g[u])

        if (!vis[v][(time+1)%3])

        {

            vis[v][(time+1)%3]=vis[u][time]+1;

            q.push({v,(time+1)%3});

        }

    }

    if (vis[t][0]) cout<<vis[t][0]/3;

    else cout<<-1;

}
// 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: 2.150456566035745x):
#include <bits/stdc++.h>



using namespace std;

const int N = 1e5 + 5;

int head[N], nxt[N * 2], to[N * 2], ne = 0;



void addedge(int a, int b) {

    nxt[ne] = head[a];

    to[ne] = b;

    head[a] = ne++;

}



int dist[N][4];

int a, b;



int bfs(int s) {

    memset(dist, '?', sizeof dist);

    dist[s][0] = 0;

    queue<pair<int, int>> q;

    q.push({s, 0});

    while (q.size()) {

        int u = q.front().first, l = q.front().second;

        q.pop();

        int lv = dist[u][l];

        if (u == b && l == 0) return lv / 3;

        for (int i = head[u]; ~i; i = nxt[i]) {

            int v = to[i], vl = (l + 1) % 3;

            if (dist[v][vl] > lv + 1) {

                dist[v][vl] = lv + 1;

                q.push({v, vl});

            }

        }

    }

    return -1;

}



int main() {

 //   freopen("in.txt", "rt", stdin);

    int n, m;

    memset(head, -1, sizeof head);

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

    while (m--) {

        scanf("%d %d", &a, &b);

        addedge(a, b);

    }

    scanf("%d %d", &a, &b);

    printf("%d\n", bfs(a));

}
// 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.8115755543507437x):
#include<bits/stdc++.h>

using namespace std;

const int N = 1e5 + 10;

vector<int> a[N];

int b[N][3];

int main()

{

	ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);

	int n, m;

	cin >> n >> m;

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

	{

		int u, v;

		cin >> u >> v;

		a[u].push_back(v);

	}

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

	{

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

		{

			b[i][j] = -3;

		}

	}

	int s, t;

	cin >> s >> t;

	queue <pair< int, int >> q;

	b[s][0] = 0;

	q.push({ s,0 });

	while (!q.empty())

	{

		const int cx = q.front().first, cy = q.front().second, ny = (cy + 1) % 3;

		q.pop();

		for (int i = 0; i<int(a[cx].size()); i++)

		{

			const int nx = a[cx][i];

			if (b[nx][ny] == -3)

			{

				q.push({ nx,ny });

				b[nx][ny] = b[cx][cy] + 1;

			}

		}

	}

	cout << b[t][0]/3 << endl;





}
// 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.6695510947753949x):
#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;

}
// 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: 6.311195448175061x):
#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;

const int N = 1e5 + 5;

int head[N], nxt[N * 2], to[N * 2], ne = 0;



void addedge(int a, int b) {

    nxt[ne] = head[a];

    to[ne] = b;

    head[a] = ne++;

}



int dist[N][4];

int a, b;



int bfs(int s) {

    memset(dist, '?', sizeof dist);

    dist[s][0] = 0;

    queue<pair<int, int>> q;

    q.push({s, 0});

    while (q.size()) {

        int u = q.front().first, l = q.front().second;

        q.pop();

        int lv = dist[u][l];

        if (u == b && l == 0) return lv / 3;

        for (int i = head[u]; ~i; i = nxt[i]) {

            int v = to[i], vl = (l + 1) % 3;

            if (dist[v][vl] > lv + 1) {

                dist[v][vl] = lv + 1;

                q.push({v, vl});

            }

        }

    }

    return -1;

}



int main() {

 //   freopen("in.txt", "rt", stdin);

    int n, m;

    memset(head, -1, sizeof head);

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

    while (m--) {

        scanf("%d %d", &a, &b);

        addedge(a, b);

    }

    scanf("%d %d", &a, &b);

    printf("%d\n", bfs(a));

}

// Faster C++ solution for the same problem (measured speedup: 2.9348165165732327x):
#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;

const int N = 1e5 + 10;

vector<int> a[N];

int b[N][3];

int main()

{

	ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);

	int n, m;

	cin >> n >> m;

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

	{

		int u, v;

		cin >> u >> v;

		a[u].push_back(v);

	}

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

	{

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

		{

			b[i][j] = -3;

		}

	}

	int s, t;

	cin >> s >> t;

	queue <pair< int, int >> q;

	b[s][0] = 0;

	q.push({ s,0 });

	while (!q.empty())

	{

		const int cx = q.front().first, cy = q.front().second, ny = (cy + 1) % 3;

		q.pop();

		for (int i = 0; i<int(a[cx].size()); i++)

		{

			const int nx = a[cx][i];

			if (b[nx][ny] == -3)

			{

				q.push({ nx,ny });

				b[nx][ny] = b[cx][cy] + 1;

			}

		}

	}

	cout << b[t][0]/3 << endl;





}


// Faster C++ solution for the same problem (measured speedup: 3.483815749786351x):
#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;

}