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

using namespace std;

typedef pair<int,int> P;

typedef long long ll;

typedef long double ld;

const int inf=1e9+7;

const ll longinf=1LL<<60;

#define REP(i,m,n) for(int i=(int)(m) ; i < (int) (n) ; ++i )

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

#define F first

#define S second

 

const int mx=200010;

const ll mod=1e9+7;



vector<int> dp(mx,-1);

vector<int> v[mx];



int rec(int x){

  if(dp[x]!=-1){ return dp[x]; }

  dp[x]=0;

  for(auto to:v[x]){

    dp[x] = max(dp[x], rec(to)+1);

  }

  return dp[x];

}





int main(){  

  int n,m;

  cin >> n >> m;



  rep(i,m){

    int x,y; cin >> x >> y; x--; y--;

    v[x].emplace_back(y);

  }

  

  int ans=0;

  rep(i,n){ ans = max(ans,rec(i)); }

  cout << ans << endl;

  return 0;

}

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

using namespace std;

typedef pair<int,int> P;

typedef long long ll;

typedef long double ld;

const int inf=1e9+7;

const ll longinf=1LL<<60;

#define REP(i,m,n) for(int i=(int)(m) ; i < (int) (n) ; ++i )

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

#define F first

#define S second

constexpr char ln =  '\n';



const int mx=100010;

const ll mod=1e9+7;



vector<int> dp(mx,-1);

vector<int> v[mx];



int rec(int x){

  if(dp[x]!=-1) return dp[x];

  int tmp = 0;

  for(auto it:v[x]){

    tmp = max(tmp, rec(it)+1);

  }

  return dp[x] = tmp; 

}



int main(){

  int n,m;

  cin >> n >> m;

  

  rep(i,m){

    int x,y; cin >> x >> y; x--; y--;

    v[x].emplace_back(y);

  }

  

  int ans = 0;

  rep(i,n){

    ans = max(ans, rec(i));

  }

  cout << ans << ln;

  return 0;

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

using namespace std;

const int MAXN=1e5+100;

int dp[MAXN];

vector<int>E[MAXN];

int dfs(int t)

{

	if(dp[t]!=-1) return dp[t];

	if(E[t].empty()) return dp[t]=0;

	for(int i=0;i<E[t].size();i++)

		dp[t]=max(dp[t],dfs(E[t][i])+1);

	return dp[t];

}

int main()

{

	int n,m; 

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

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

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

	{

		int x,y;

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

		E[x].push_back(y);

	}

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

		dfs(i);

	printf("%d\n",*max_element(dp+1,dp+n+1));

	return 0;

}

/*

#include<bits/stdc++.h>

using namespace std;

const int N=1e5+10;

queue<int>q;

vector<int>E[N];

int n,m,ans;

int dp[N],indegree[N];

int main()

{

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

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

	{

		int x,y;

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

		E[x].push_back(y); 

		indegree[y]++;

	}

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

		if(!indegree[i]) 

			q.push(i);

    while(!q.empty()) 

	{

    	int u=q.front(); 

		q.pop();

    	for(int i=0;i<E[u].size();i++) 

		{

			int uu=E[u][i];

			indegree[uu]--;

			if(!indegree[uu]) 

			{

				dp[uu]=dp[u]+1;

				q.push(uu);

			}

		}

	}

	int maxn=0;

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

		maxn=max(maxn,dp[i]);

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

	return 0;

}

*/

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

}
// Slower C++ solution:
#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;

}

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

}
// Slower C++ solution:
#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;

}


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

}
// Slower C++ solution:
/// 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;

}


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

}
// Slower C++ solution:
#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);

}

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

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

}
// Slower C++ solution:
#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);

}

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

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

}
// Slower C++ solution:
#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;

}

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

}
// Slower C++ solution:
#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";

}


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

}