stablegradients/pie-gem5-bysrc huggingface dataset:
// Original C++ solution (slower):
#include<bits/stdc++.h>

using namespace std;

int n,m,a,b,dp[100010];

vector <int> v[100010];

inline int dfs(int x)

{

	if(dp[x]>=0)return dp[x];

	else{

		if(v[x].empty())return dp[x]=0;

		else{

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

				dp[x]=max(dfs(v[x][i])+1,dp[x]);

			}

		}

	}

	return dp[x];

}

int main()

{

	memset(dp,-1,sizeof(dp));

	cin>>n>>m;

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

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

		v[a].push_back(b);

	}

	int ans=0;

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

	for(int i=1;i<=n;++i)ans=max(ans,dp[i]);

	cout<<ans<<endl;

	return 0;

}

// Optimized solution to the same problem (faster):
#include<bits/stdc++.h>

using namespace std;

int n,m,a,b,dp[100010],to[100010];

vector <int> v[100010];

queue <int> q;



int main()

{

	cin>>n>>m;

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

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

		v[a].push_back(b);

		to[b]++;

	}	

	for(int i=1;i<=n;++i)if(to[i]==0)q.push(i);

	while(!q.empty()){

		int x=q.front();

		q.pop();

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

			if(dp[x]+1>dp[v[x][i]]){

				dp[v[x][i]]=dp[x]+1;

			}

			to[v[x][i]]--;

			if(to[v[x][i]]==0)q.push(v[x][i]);

		}

	}

	int ans=0;

	for(int i=1;i<=n;++i)ans=max(ans,dp[i]);

	cout<<ans<<endl;

	return 0;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <bits/stdc++.h>

using namespace std;



#define REP(i, a, b) for(int i = (a); i <= (b); i++)

#define PER(i, a, b) for(int i = (a); i >= (b); i--)

#define rep(i, a, b) for(int i = (a); i < (b); i++)

#define debug(x) cerr << #x << " = " << x << endl;

#define SET(c, v) memset(c, v, sizeof c)

#define pi 3.14159265358979323846264338

#define all(S) (S).begin(), (S).end()

#define INF 0x3f3f3f3f

#define mod 1000000007

#define pf push_front

#define pb push_back

#define mk make_pair

#define MAX 100010

#define S second

#define F first

typedef long long ll;

typedef long double lf;

typedef pair<int, int> ii;

typedef unsigned long long int ull;

vector<int> adj[MAX];

int pd[MAX];



int solve(int u) {

	int &ans = pd[u];

	if(ans != -1) return ans;

	int t = adj[u].size();

	ans = 0;

	rep(i, 0, t) {

		int v = adj[u][i];

		ans = max(ans, solve(v)+1);

	}

	return ans;

}



int main(int argc, char** argv) {

	int N, M, a, b, ans = 0;

	scanf("%d%d", &N, &M);

	rep(i, 0, M) {

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

		adj[a].pb(b);

	}

	memset(pd, -1, sizeof pd);

	REP(i, 1, N) ans = max(ans, solve(i));

	printf("%d\n", ans);

	return 0;

}


// Optimized solution to the same problem (faster):
#include <bits/stdc++.h>

using namespace std;

 

#define REP(i, a, b) for(int i = (a); i <= (b); i++)

#define PER(i, a, b) for(int i = (a); i >= (b); i--)

#define rep(i, a, b) for(int i = (a); i < (b); i++)

#define all(S) (S).begin(), (S).end()

#define pf push_front

#define pb push_back

#define mk make_pair

#define S second

#define F first

typedef long long ll;

typedef long double lf;

typedef pair<int, ll> il;

const int MAX = 1e5+5;

vector<int> adj[MAX];

int N, M, ans, h[MAX];



void solve(int u) {

	if(h[u]) return;

	h[u] = 1;

	rep(i, 0, adj[u].size()) {

		int v = adj[u][i];

		solve(v);

		h[u] = max(h[u], 1 + h[v]);

	}

	ans = max(ans, h[u]);

}



int main(int argc, char *argv[]) {

	int u, v;

	scanf("%d%d", &N, &M);

	rep(i, 0, M) {

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

		adj[u].pb(v);

	}

	REP(i, 1, N) solve(i);

	printf("%d\n", ans-1);

	return 0;

}


// Fastest known solution for this problem:
// Original C++ solution (slower):
/// However long the night,

///     The dawn will break

/// ICPC next year



#include<bits/stdc++.h>

using namespace std ;

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



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

vector<int> adj[N] ;

bool vis[N] ;

long long dis[N][3] ;

int main(){

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

    while(m--){

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

        adj[v].push_back(u);

    }

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

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

    queue<pair<int,int>> q;

    q.push({t,0});

    dis[t][0] = 0 ;

    while(q.size()){

        int p = q.front().first ;

        int md = q.front().second ;

        q.pop() ;

        for(int ch:adj[p]){

            int d = dis[p][md] + 1 ;

            if(dis[ch][d%3] <= d) continue ;

            dis[ch][d%3] = d ;

            q.push({ch,d%3});

        }

    }

    if(dis[s][0]>1e9) return cout << -1,0 ;

    cout << dis[s][0]/3 ;

    return 0;

}


// Optimized solution to the same problem (faster):
#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;

}


// Fastest known solution for this problem:
// Original C++ solution (slower):
#include<cstdio>

#include<vector>

#include<queue>

#define f(x,k) 3*(x-1)+k+1

std::vector<int> a[300005];

int n,m,s,t,dis[300005];

void add(int x,int y){a[x].push_back(y);}

void bfs(int s)

{

	std::queue<int> q;

	for (int i=1;i<=3*n;i++) dis[i]=1000000000;

	dis[s]=0;q.push(s);

	while (!q.empty())

	{

		int v=q.front();q.pop();

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

		{

			int u=a[v][i];

			if (dis[v]+1<dis[u]) dis[u]=dis[v]+1,q.push(u);

	    }

	}

}

int main()

{

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

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

	{

		int x,y;

		scanf("%d%d",&x,&y);

		add(f(x,0),f(y,1));

		add(f(x,1),f(y,2));

		add(f(x,2),f(y,0));

	}

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

	bfs(f(s,0));

	printf("%d\n",dis[f(t,0)]!=1000000000?dis[f(t,0)]/3:-1);

}

// Optimized solution to the same problem (faster):
#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;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#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';







}




















// Optimized solution to the same problem (faster):
#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;

}


// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <bits/stdc++.h>



using namespace std;

const int N = 1e5 + 3;

vector<int> adj[N];



struct State {

	int node, cost, mod;

	bool const operator<(const State &e) const {

		return e.cost < cost;

	}

};



int dist[N][4];

int dijkstra(int source, int destination) {

	memset(dist, 0x3f, sizeof dist);

	priority_queue<State> q;

	q.push({source, 0, 0});

	while (!q.empty()) {

		auto cur = q.top();

		q.pop();

		if (dist[cur.node][cur.mod] < cur.cost)

			continue;

		for (auto child: adj[cur.node]) {

			int nC = cur.cost + (cur.mod == 0), nM = (cur.mod + 1) % 3;

			if (dist[child][nM] > nC) {

				dist[child][nM] = nC;

				q.push({child, nC, nM});

			}

		}

	}

	if (dist[destination][0] < 1e9)

		return dist[destination][0];

	return -1;

}

int main() {

#ifndef ONLINE_JUDGE

//	freopen("input.in", "r", stdin);

#endif

	int n, m;

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

	while (m--) {

		int u, v;

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

		adj[u].push_back(v);

	}

	int a, b;

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

	printf("%d\n", dijkstra(a, b));

	return 0;

}

// Optimized solution to the same problem (faster):
#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;

}


// Fastest known solution for this problem:
// Original C++ solution (slower):
#ifdef ConanYu

#include "local.hpp"

#else

#pragma GCC optimize("-O3")

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

void Main();

#define debug(...) do { } while(0)

int main() {

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

  Main();

}

#endif





const int N = 2e5 + 10;



// bool vis[N << 2];



int dis[N << 2], h[N], tot;



struct edge {

  int v, nxt;

} G[N];



void addedge(int u, int v) {

  G[tot] = {v, h[u]}; h[u] = tot++;

}



void Main() {

  for(int i = 0; i < (N << 2); i++) dis[i] = -3;

  memset(h, 0xff, sizeof h);

  tot = 0;

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

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

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

    addedge(u, v);

  }

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

  queue<int> q;

  // vis[s << 2] = 1;

  dis[s << 2] = 0;

  q.push(s << 2);

  while(!q.empty()) {

    const int cur = q.front();

    q.pop();

    const int u = cur >> 2, p = cur & 3;

    // debug(u, p, cur);

    for(int i = h[u]; ~i; i = G[i].nxt) {

      const int v = G[i].v, np = (p + 1) % 3;

      const int nxt = (v << 2) | np;

      if(dis[nxt] == -3) {

        // vis[nxt] = true;

        dis[nxt] = dis[cur] + 1;

        // debug(dis[nxt], dis[u]);

        q.push(nxt);

      }

    }

  }

  // debug(dis[t << 2]);

  cout << dis[t << 2] / 3 << "\n";

}


// Optimized solution to the same problem (faster):
#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;

}


// Fastest known solution for this problem:
// Original C++ solution (slower):
#include<bits/stdc++.h>

using namespace std;

#define all(v) ((v).begin()),((v).end())

#define ll long long

#define fastIO cout << fixed << setprecision(12), ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr)

double const EPS = 1e-12, PI = acos(-1);

const int N = 1e5 + 9, M = 5e3 + 9, OO = 1e9 + 7, MOD = 1e9 + 7;

const ll inf = 1e18;



vector<int> adj[N];

int start, endd, best = OO;

int vis[N][5];



void dfs(int node, int type, int cnt) {

  if(type == 0 && node == endd) {

    best = min(best, cnt);

    return;

  }

  if(vis[node][type] <= cnt)

    return;

  vis[node][type] = cnt;

  for(auto it: adj[node]) {

    dfs(it, (type + 1) == 3 ? 0 : type + 1, cnt + (type + 1 == 3));

  }

}



bool can = 0;

void bfs(int node, int type, int cnt) {

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

  q.push({node, {type , cnt}});

  while(!q.empty()) {

    auto it = q.front();

    q.pop();

    if(it.first == endd && it.second.first == 0) {

      cout << it.second.second;

      can = 1;

      return;

    }

    vis[it.first][it.second.first] = cnt;

    for(auto ii: adj[it.first]) {

      int nxtType = (it.second.first == 2) ? 0 : it.second.first + 1;

      int nxtCnt = it.second.second + (nxtType == 0);

      if(vis[ii][nxtType] > nxtCnt) {

        vis[ii][nxtType] = nxtCnt;

        q.push({ii, {nxtType, nxtCnt}});

      }

    }

  }

}



int main() {

  fastIO;

#ifdef LOCAL

  freopen("input.in", "rt", stdin);

#endif

  int n, m, u, v;

  cin >> n >> m;

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

    cin >> u >> v;

    adj[u].push_back(v);

  }

  cin >> start >> endd;

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

  bfs(start, 0, 0);

  if(!can)

    cout << -1;

  return 0;

}


// Optimized solution to the same problem (faster):
#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;

}


// Fastest known solution for this problem:
// Original C++ solution (slower):
#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;

}


// Optimized solution to the same problem (faster):
#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;

}


// Fastest known solution for this problem:
// Original C++ solution (slower):
#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");

}


// Optimized solution to the same problem (faster):
#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;

}


// Fastest known solution for this problem:
// Original C++ solution (slower):
#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;

}

// Optimized solution to the same problem (faster):
#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;

}


// Fastest known solution for this problem: