#include <bits/stdc++.h>
using namespace std;

#define LLI				long long int
#define PN 				cout<<'\n'

#define SZ(a) 			int((a).size())
#define PB 				push_back
#define ALL(c) 			(c).begin(),(c).end()
#define FORITER(i,c)	for(typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)
#define CFIND(x,c) 		((c).find(x) != (c).end())
#define FIND(x,c) 		(find(ALL(c),x) != (c).end())

#define PI 			    3.1415926535
#define MOD				1000000007

#define error(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args); }

void err(istream_iterator<string> it) {}
template<typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
	cerr << *it << " = " << a << endl;
	err(++it, args...);
}

vector<vector<int>> gra(1e6+1);
deque<int> Q;
vector<int> out(1e6+1);
vector<int> dp(1e6+1, INT_MAX);

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);

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

	int l, r;

	while(m--){
		cin>>l>>r;
		l--; r--;
		gra[r].push_back(l);
		out[l]++;
	}

	cin>>l>>r;
	l--; r--;

	dp[r] = 0;

	Q.push_front(r);
	int c = 0;
	while(!Q.empty()){
		int top = Q.front();
		// cout<<top;
		Q.pop_front();
		c++;
		if(c > 11)
			break;
		for(auto node: gra[top]){
			// error(node, top, out[node]);PN;
			out[node]--;
			if(out[node] == 0){
				if(dp[node] >= dp[top]){
					dp[node] = min(dp[node], dp[top]);
					Q.push_front(node);
				}
			}
			else{
				if(dp[node] >= dp[top] + 1){
					dp[node] = min(dp[node], 1 + dp[top]);
					Q.push_back(node);
				}

			}
		}
		// PN;
		// PN;
	}

	if(dp[l] == INT_MAX)
		cout<<-1;
	else
		cout<<dp[l];

	return 0;
}


#include <bits/stdc++.h>
using namespace std;

#define LLI				long long int
#define PN 				cout<<'\n'

#define SZ(a) 			int((a).size())
#define PB 				push_back
#define ALL(c) 			(c).begin(),(c).end()
#define FORITER(i,c)	for(typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)
#define CFIND(x,c) 		((c).find(x) != (c).end())
#define FIND(x,c) 		(find(ALL(c),x) != (c).end())

#define PI 			    3.1415926535
#define MOD				1000000007

#define error(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args); }

void err(istream_iterator<string> it) {}
template<typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
	cerr << *it << " = " << a << endl;
	err(++it, args...);
}

vector<vector<int>> gra(1e6+1);
deque<int> Q;
vector<int> out(1e6+1), vis(1e6+1);
vector<int> dp(1e6+1, INT_MAX);

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);

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

	int l, r;

	while(m--){
		cin>>l>>r;
		l--; r--;
		gra[r].push_back(l);
		out[l]++;
	}

	cin>>l>>r;
	l--; r--;

	dp[r] = 0;

	Q.push_front(r);

	while(!Q.empty()){
		int top = Q.front();
		// cout<<top;
		Q.pop_front();

		if(vis[top])
			continue;
		vis[top] = 1;

		for(auto node: gra[top]){
			// error(node, top, out[node]);PN;
			out[node]--;
			if(out[node] == 0){
				if(dp[node] > dp[top]){
					dp[node] = min(dp[node], dp[top]);
					Q.push_front(node);
				}
			}
			else{
				if(dp[node] > dp[top] + 1){
					dp[node] = min(dp[node], 1 + dp[top]);
					Q.push_back(node);
				}

			}
		}
	}

	if(dp[l] == INT_MAX)
		cout<<-1;
	else
		cout<<dp[l];

	return 0;
}


#include <bits/stdc++.h>
using namespace std;

#define LLI				long long int
#define PN 				cout<<'\n'

#define SZ(a) 			int((a).size())
#define PB 				push_back
#define ALL(c) 			(c).begin(),(c).end()
#define FORITER(i,c)	for(typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)
#define CFIND(x,c) 		((c).find(x) != (c).end())
#define FIND(x,c) 		(find(ALL(c),x) != (c).end())

#define PI 			    3.1415926535
#define MOD				1000000007

#define error(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args); }

void err(istream_iterator<string> it) {}
template<typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
	cerr << *it << " = " << a << endl;
	err(++it, args...);
}

vector<vector<int>> gra(1e6+1);
deque<int> Q;
vector<int> out(1e6+1);
vector<int> dp(1e6+1, INT_MAX);

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);

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

	int l, r;

	while(m--){
		cin>>l>>r;
		l--; r--;
		gra[r].push_back(l);
		out[l]++;
	}

	cin>>l>>r;
	l--; r--;

	dp[r] = 0;

	Q.push_front(r);
	int c = 0;
	while(!Q.empty()){
		int top = Q.front();
		// cout<<top;
		Q.pop_front();
		c++;
		if(c > 11)
			break;
		for(auto node: gra[top]){
			// error(node, top, out[node]);PN;
			out[node]--;
			if(out[node] == 0){
				if(dp[node] > dp[top]){
					dp[node] = min(dp[node], dp[top]);
					Q.push_front(node);
				}
			}
			else{
				if(dp[node] > dp[top] + 1){
					dp[node] = min(dp[node], 1 + dp[top]);
					Q.push_back(node);
				}

			}
		}
		// PN;
		// PN;
	}

	if(dp[l] == INT_MAX)
		cout<<-1;
	else
		cout<<dp[l];

	return 0;
}


#include <bits/stdc++.h>
using namespace std;

#define LLI				long long int
#define PN 				cout<<'\n'

#define SZ(a) 			int((a).size())
#define PB 				push_back
#define ALL(c) 			(c).begin(),(c).end()
#define FORITER(i,c)	for(typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)
#define CFIND(x,c) 		((c).find(x) != (c).end())
#define FIND(x,c) 		(find(ALL(c),x) != (c).end())

#define PI 			    3.1415926535
#define MOD				1000000007

#define error(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args); }

void err(istream_iterator<string> it) {}
template<typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
	cerr << *it << " = " << a << endl;
	err(++it, args...);
}

vector<vector<int>> gra(1e6+1);
deque<int> Q;
vector<int> out(1e6+1);
vector<int> dp(1e6+1, INT_MAX);

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);

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

	int l, r;

	while(m--){
		cin>>l>>r;
		l--; r--;
		gra[r].push_back(l);
		out[l]++;
	}

	cin>>l>>r;
	l--; r--;

	dp[r] = 0;

	Q.push_front(r);

	while(!Q.empty()){
		int top = Q.front();
		// cout<<top;
		Q.pop_front();

		for(auto node: gra[top]){
			// error(node, top, out[node]);PN;
			out[node]--;
			if(out[node] == 0){
				if(dp[node] >= dp[top]){
					dp[node] = min(dp[node], dp[top]);
					Q.push_front(node);
				}
			}
			else{
				if(dp[node] >= dp[top] + 1){
					dp[node] = min(dp[node], 1 + dp[top]);
					Q.push_back(node);
				}

			}
		}
		// PN;
		// PN;
	}

	if(dp[l] == INT_MAX)
		cout<<-1;
	else
		cout<<dp[l];

	return 0;
}


#include <bits/stdc++.h>
using namespace std;

#define LLI				long long int
#define PN 				cout<<'\n'

#define SZ(a) 			int((a).size())
#define PB 				push_back
#define ALL(c) 			(c).begin(),(c).end()
#define FORITER(i,c)	for(typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)
#define CFIND(x,c) 		((c).find(x) != (c).end())
#define FIND(x,c) 		(find(ALL(c),x) != (c).end())

#define PI 			    3.1415926535
#define MOD				1000000007

#define error(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args); }

void err(istream_iterator<string> it) {}
template<typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
	cerr << *it << " = " << a << endl;
	err(++it, args...);
}

vector<vector<int>> gra(1e6+1);
deque<int> Q;
vector<int> out(1e6+1);
vector<int> dp(1e6+1, INT_MAX);

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);

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

	int l, r;

	while(m--){
		cin>>l>>r;
		l--; r--;
		gra[r].push_back(l);
		out[l]++;
	}

	cin>>l>>r;
	l--; r--;

	dp[r] = 0;

	Q.push_front(r);

	while(!Q.empty()){
		int top = Q.front();
		// cout<<top;
		Q.pop_front();

		for(auto node: gra[top]){
			// error(node, top, out[node]);PN;
			out[node]--;
			if(out[node] == 0){
				if(dp[node] > dp[top]){
					dp[node] = min(dp[node], dp[top]);
					Q.push_front(node);
				}
			}
			else{
				if(dp[node] > dp[top] + 1){
					dp[node] = min(dp[node], 1 + dp[top]);
					Q.push_back(node);
				}

			}
		}
		// PN;
		// PN;
	}

	if(dp[l] == INT_MAX)
		cout<<-1;
	else
		cout<<dp[l];

	return 0;
}


#include <bits/stdc++.h>
using namespace std;

#define LLI				long long int
#define PN 				cout<<'\n'

#define SZ(a) 			int((a).size())
#define PB 				push_back
#define ALL(c) 			(c).begin(),(c).end()
#define FORITER(i,c)	for(typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)
#define CFIND(x,c) 		((c).find(x) != (c).end())
#define FIND(x,c) 		(find(ALL(c),x) != (c).end())

#define PI 			    3.1415926535
#define MOD				1000000007

#define error(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args); }

void err(istream_iterator<string> it) {}
template<typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
	cerr << *it << " = " << a << endl;
	err(++it, args...);
}

vector<vector<int>> gra(1e6+1);
deque<int> Q;
vector<int> out(1e6+1), vis(1e6+1);
vector<int> dp(1e6+1, INT_MAX);

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);

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

	int l, r;

	while(m--){
		cin>>l>>r;
		l--; r--;
		gra[r].push_back(l);
		out[l]++;
	}

	cin>>l>>r;
	l--; r--;

	dp[r] = 0;

	Q.push_front(r);

	while(!Q.empty()){
		int top = Q.front();
		// cout<<top;
		Q.pop_front();

		if(vis[top])
			continue;
		vis[top] = 1;

		for(auto node: gra[top]){
			// error(node, top, out[node]);PN;
			out[node]--;
			if(out[node] == 0){
				if(dp[node] > dp[top]){
					dp[node] = min(dp[node], dp[top]);
					Q.push_front(node);
				}
			}
			else{
				if(dp[node] > dp[top] + 1){
					dp[node] = min(dp[node], 1 + dp[top]);
					Q.push_back(node);
				}

			}
		}
		// PN;
		// PN;
	}

	if(dp[l] == INT_MAX)
		cout<<-1;
	else
		cout<<dp[l];

	return 0;
}


// In the name of GOD
#include<bits/stdc++.h>
#define pii pair<int, int>
#define pb push_back
#define F first
#define S second
#define ll long long
#define ld long double
using namespace std;
const int MAXN = 1e6 + 10;
ll INF = 1e15, dp[MAXN], dp1[MAXN], dp2[MAXN];
vector<int> a[MAXN], b[MAXN];
set<pair<ll, int> > s;
int cnt[MAXN];
int main(){
	ios::sync_with_stdio(false); cin.tie(0);
	int n, m, st, t;
	cin >> n >> m;
	for(int i = 0; i < m; i ++){
		int x, y;
		cin >> x >> y;
		a[x].pb(y);
		b[y].pb(x);
	}
	cin >> st >> t;
	fill(dp, dp + MAXN, INF);
	fill(dp1, dp1 + MAXN, INF);
	fill(dp2, dp2 + MAXN, -1);
	dp[t] = 0;
	for(int i = 1; i <= n; i ++)
		s.insert({dp[i], i});
	while(s.size()){
		int v = s.begin() -> S; s.erase(s.begin());
		for(auto u : b[v])
			dp1[u] = min(dp1[u], dp[v] + 1);
		for(auto u : b[v]){
			dp2[u] = max(dp2[u], dp[v]);
			cnt[u] ++;
		}
		for(auto u : b[v]){
			ll val = dp2[u];
			if(cnt[u] != a[u].size()) val = INF;
			if(min(val, dp1[u]) < dp[u]){
				s.erase({dp[u], u});
				dp[u] = min(val, dp1[u]);
				s.insert({dp[u], u});
			}
		}
	}
	if(dp[st] >= INF)
		dp[st] = -1;
	cout << dp[st] << endl;
	return 0;
}


// In the name of GOD
#include<bits/stdc++.h>
#define pii pair<int, int>
#define pb push_back
#define F first
#define S second
#define ll long long
#define ld long double
using namespace std;
const int MAXN = 1e5 + 10;
ll INF = 1e15, dp[MAXN], dp1[MAXN], dp2[MAXN];
vector<int> a[MAXN], b[MAXN];
set<pair<ll, int> > s;
int cnt[MAXN];
int main(){
	ios::sync_with_stdio(false); cin.tie(0);
	int n, m, st, t;
	cin >> n >> m;
	for(int i = 0; i < m; i ++){
		int x, y;
		cin >> x >> y;
		a[x].pb(y);
		b[y].pb(x);
	}
	cin >> st >> t;
	fill(dp, dp + MAXN, INF);
	fill(dp1, dp1 + MAXN, INF);
	fill(dp2, dp2 + MAXN, -1);
	dp[t] = 0;
	for(int i = 1; i <= n; i ++)
		s.insert({dp[i], i});
	while(s.size()){
		int v = s.begin() -> S; s.erase(s.begin());
		for(auto u : b[v])
			dp1[u] = min(dp1[u], dp[v] + 1);
		for(auto u : b[v]){
			dp2[u] = max(dp2[u], dp[v]);
			cnt[u] ++;
		}
		for(auto u : b[v]){
			ll val = dp2[u];
			if(cnt[u] != a[u].size()) val = INF;
			if(min(val, dp1[u]) < dp[u]){
				s.erase({dp[u], u});
				dp[u] = min(val, dp1[u]);
				s.insert({dp[u], u});
			}
		}
	}
	if(dp[st] >= INF)
		dp[st] = -1;
	cout << dp[st] << endl;
	return 0;
}


#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std;

const int MAXN = 1000005;
const int INF = 0x3f3f3f3f;

int N, M;
int S, T;
vector <int> E[MAXN];
int dp[MAXN];
bool on[MAXN];

void load() {
    scanf("%d%d", &N, &M);
    while (M--) {
        int u, v;
        scanf("%d%d", &u, &v);
        E[u].push_back(v);
    }
    scanf("%d%d", &S, &T);
}

int dfs(int x) {
    if (dp[x] < INF) return dp[x];
    on[x] = true;
    int mini = INF, maks = 0;
    for (int i = 0; i < E[x].size(); i++) {
        int tmp = on[E[x][i]] ? INF : dfs(E[x][i]);
        mini = min(mini, tmp);
        maks = max(maks, tmp);
    }
    dp[x] = mini + (mini < maks);
    on[x] = false;
    return dp[x];
}

int solve() {
    memset(dp, INF, sizeof dp);
    dp[T] = 0;
    int sol = dfs(S);
    return sol < INF ? sol : -1;
}

int main() {
    load();
    printf("%d\n", solve());
    return 0;
}


#include <cstdio>
#include <algorithm>
#include <vector>
using namespace std;

const int MAXN = 1000005;
const int INF = 0x3f3f3f3f;

int N, M;
int S, T;
vector <int> E[MAXN];
int dp[MAXN];
int bio[MAXN];

void load() {
    scanf("%d%d", &N, &M);
    while (M--) {
        int u, v;
        scanf("%d%d", &u, &v);
        E[u].push_back(v);
    }
    scanf("%d%d", &S, &T);
}

int dfs(int x) {
    if (bio[x] == 1) return INF;
    if (bio[x] == 2) return dp[x];
    bio[x] = 1;
    int mini = INF, maks = 0;
    for (int i = 0; i < E[x].size(); i++) {
        int tmp = dfs(E[x][i]);
        mini = min(mini, tmp);
        maks = max(maks, tmp);
    }
    dp[x] = mini + (mini < maks);
    bio[x] = 2;
    return dp[x];
}

int solve() {
    bio[T] = 2;
    int sol = dfs(S);
    return sol < INF ? sol : -1;
}

int main() {
    load();
    printf("%d\n", solve());
    return 0;
}


#include <cstdio>
#include <cstring>
#include <vector>
#include <set>
using namespace std;

const int MAXN = 1000005;
const int INF = 0x3f3f3f3f;

int N, M;
int s, t;
vector <int> inv[MAXN];
int deg[MAXN];
int dist[MAXN];

struct cmp {
    bool operator()(const int &lhs, const int &rhs) const {
        if (dist[lhs] != dist[rhs])
            return dist[lhs] < dist[rhs];
        return lhs < rhs;
    }
};

set <int, cmp> S;

void load() {
    scanf("%d%d", &N, &M);
    while (M--) {
        int u, v;
        scanf("%d%d", &u, &v);
        inv[v].push_back(u);
        deg[u]++;
    }
    scanf("%d%d", &s, &t);
}

int solve() {
    memset(dist, INF, sizeof dist);
    dist[t] = 0;
    for (int i = 1; i <= N; i++)
        S.insert(i);

    while (!S.empty()) {
        int x = *S.begin();
        S.erase(x);
        for (int i = 0; i < inv[x].size(); i++) {
            int nxt = inv[x][i];
            if (dist[x] < dist[nxt]) {
                deg[nxt]--;
                S.erase(nxt);
                dist[nxt] = dist[x] + !!deg[nxt];
                S.insert(nxt);
            }
        }
    }

    return dist[s] < INF ? dist[s] : -1;
}

int main() {
    load();
    printf("%d\n", solve());
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define int long long
#define pii pair<int,int>
vector<vector<int> > vv(1000001);
int mod=1e9 + 7;
int huge=(1ll<<60);
int out[1000001];
bool vis[1000001];
void add_edge(int u,int v)
{
    vv[v].push_back(u);
    out[u]++;
}
int shortest_worst_cost(int s,int t,int n)
{
    deque<int> q;
    q.push_front(t);
    int dis[n+1];
    for(int i=1;i<=n;i++)
        dis[i]=-1;
    dis[t]=0;
    while(!q.empty())
    {
        int u=q.front();
        q.pop_front();
        if(u==s)
            return dis[s];
        if(!vis[u])
        {
            vis[u]=true;
            for(int i=0;i<vv[u].size();i++)
            {
                int v=vv[u][i];
                out[v]--;
                if(out[v]==0 && (dis[u]<dis[v]|| dis[v]==-1))
                {
                    dis[v]=dis[u];
                    q.push_front(v);
                }
                else if(dis[v]==-1)
                {
                    dis[v]=1+dis[u];
                    q.push_back(v);
                }
            }
        }
    }
    return -1;
}
int32_t main()
{
    #ifndef ONLINE_JUDGE
    // for getting input from input.txt
    freopen("input.txt", "r", stdin);
    // for writing output to output.txt
    freopen("output.txt", "w", stdout);
    #endif
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int t;
    t=1;
    while(t--)
    {
        int n,m;
        cin>>n>>m;
        for(int i=1;i<=n;i++)
        {
            vv[i].clear();
            out[i]=0;
            vis[i]=false;
        }
        int u,v;
        for(int i=1;i<=m;i++)
        {
            cin>>u>>v;
            add_edge(u,v);
        }
        int s,t;
        cin>>s>>t;
        int ans=shortest_worst_cost(s,t,n);
        cout<<ans<<"\n";
    }
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <ctype.h>
#include <deque>
#include <queue>
#include <cstring>
#include <set>
#include <list>
#include <map>
#include <random>
#include <unordered_map>
#include <stdio.h>

using namespace std;

typedef long long ll;
typedef std::vector<int> vi;
typedef std::vector<bool> vb;
typedef std::vector<string> vs;
typedef std::vector<double> vd;
typedef std::vector<long long> vll;
typedef std::vector<std::vector<int> > vvi;
typedef vector<vvi> vvvi;
typedef vector<vll> vvll;
typedef std::vector<std::pair<int, int> > vpi;
typedef vector<vpi> vvpi;
typedef std::pair<int, int> pi;
typedef std::pair<ll, ll> pll;
typedef std::vector<pll> vpll;

const long long mod = 1000000007;

#define all(c) (c).begin(),(c).end()
#define sz(c) (int)(c).size()
#define forn(i, a, b) for(int i = a; i < b; i++)

#define pb push_back
#define mp make_pair

int main()
{
#ifndef ONLINE_JUDGE
    freopen("input.txt", "rt", stdin);
    freopen("output.txt", "wt", stdout);
#endif
    int n,m;
    scanf("%d %d", &n, &m);
    vvi g(n);
    vvi grev(n);
    vi out(n);
    forn(i,0,m) {
        int x,y;
        scanf("%d %d", &x,&y);
        x--;y--;
        out[x]++;
        g[x].pb(y);
        grev[y].pb(x);
    }

    int s,t;
    scanf("%d %d", &s,&t);
    s--;
    t--;
    deque<int> q;
    deque<int> nq;
    vi used(n,0);
    q.pb(t);
    used[t] = 1;
    int ans = 0;
    while(1) {
        if(q.empty()) break;
        int pt = 0;
        while(pt < q.size()) {
            int u =q[pt];
            for(auto v:grev[u]) {
                out[v]--;
                if(out[v] == 0 && !used[v]) {
                    q.pb(v);
                    used[v] = 1;
                }
            }
            pt++;
        }
        if(used[s]) break;
        ans++;
        for(auto u : q) {
            for(auto v : grev[u]) if(!used[v]) {
                used[v] = 1;
                nq.pb(v);
            }
        }
        q.clear();
        q=move(nq);
        nq.clear();
    }
    if(used[s]) cout<<ans;
    else cout<<-1;
}


#include <bits/stdc++.h>
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
using namespace std;
typedef long long ll;
typedef double lf;
typedef pair<ll,ll> ii;
#define REP(i,n) for(int i=0;i<n;i++)
#define REP1(i,n) for(ll i=1;i<=n;i++)
#define RST(i,n) memset(i,n,sizeof i)
#define SZ(a) (int)a.size()
#define ALL(a) a.begin(),a.end()
#define X first
#define Y second
#define mkp make_pair
#define pb push_back
#define eb emplace_back
#define pob pop_back
#ifdef cold66
#define debug(...) do{\
    fprintf(stderr,"%s - %d (%s) = ",__PRETTY_FUNCTION__,__LINE__,#__VA_ARGS__);\
    _do(__VA_ARGS__);\
}while(0)
template<typename T>void _do(T &&_x){cerr<<_x<<endl;}
template<typename T,typename ...S> void _do(T &&_x,S &&..._t){cerr<<_x<<", ";_do(_t...);}
template<typename _a,typename _b> ostream& operator << (ostream &_s,const pair<_a,_b> &_p){return _s<<"("<<_p.X<<","<<_p.Y<<")";}
template<typename It> ostream& _OUTC(ostream &_s,It _ita,It _itb)
{
    _s<<"{";
    for(It _it=_ita;_it!=_itb;_it++)
    {
        _s<<(_it==_ita?"":",")<<*_it;
    }
    _s<<"}";
    return _s;
}
template<typename _a> ostream &operator << (ostream &_s,vector<_a> &_c){return _OUTC(_s,ALL(_c));}
template<typename _a> ostream &operator << (ostream &_s,set<_a> &_c){return _OUTC(_s,ALL(_c));}
template<typename _a,typename _b> ostream &operator << (ostream &_s,map<_a,_b> &_c){return _OUTC(_s,ALL(_c));}
template<typename _t> void pary(_t _a,_t _b){_OUTC(cerr,_a,_b);cerr<<endl;}
#define IOS()
#else
#define debug(...)
#define pary(...)
#define endl '\n'
#define IOS() ios_base::sync_with_stdio(0);cin.tie(0);
#endif // cold66
//}
template<class T> inline bool chkmax(T &a, const T &b) { return b > a ? a = b, true : false; }
template<class T> inline bool chkmin(T &a, const T &b) { return b < a ? a = b, true : false; }
template<class T> using MaxHeap = priority_queue<T>;
template<class T> using MinHeap = priority_queue<T, vector<T>, greater<T>>;

const ll MAXn=1e6+5,MAXlg=__lg(MAXn)+2;
const ll MOD=1000000007;
const ll INF=0x3f3f3f3f3f3f3f3f;

vector<ll> re[MAXn];
bool vis[MAXn];
ll dp[MAXn],out[MAXn];
int main(){
  IOS();
  ll n,m;
  cin>>n>>m;
  REP(i,n) dp[i] = INF;
  REP(i,m){
    ll u,v;
    cin>>u>>v;
    u--,v--;
    re[v].eb(u);
    out[u]++;
  }
  ll st,dst;
  cin>>st>>dst;
  st--,dst--;
  deque<ll> dq;
  dq.pb(dst);
  dp[dst] = 0;
  while(SZ(dq)){
    ll cur = dq.front();
    dq.pop_front();
    if(cur == st) break;
    if(vis[cur]) continue;
    vis[cur] = true;
    for(auto i:re[cur]){
      out[i]--;
      if(!out[i]){
        if(chkmin(dp[i],dp[cur])) dq.push_front(i);
      }
      else if(dp[i] == INF){
        dp[i] = dp[cur]+1;
        dq.pb(i);
      }
    }
  }
  if(dp[st] == INF) cout<<-1<<endl;
  else cout<<dp[st]<<endl;
}


#include <bits/stdc++.h>
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
using namespace std;
typedef long long ll;
typedef double lf;
typedef pair<ll,ll> ii;
#define REP(i,n) for(int i=0;i<n;i++)
#define REP1(i,n) for(ll i=1;i<=n;i++)
#define RST(i,n) memset(i,n,sizeof i)
#define SZ(a) (int)a.size()
#define ALL(a) a.begin(),a.end()
#define X first
#define Y second
#define mkp make_pair
#define pb push_back
#define eb emplace_back
#define pob pop_back
#ifdef cold66
#define debug(...) do{\
    fprintf(stderr,"%s - %d (%s) = ",__PRETTY_FUNCTION__,__LINE__,#__VA_ARGS__);\
    _do(__VA_ARGS__);\
}while(0)
template<typename T>void _do(T &&_x){cerr<<_x<<endl;}
template<typename T,typename ...S> void _do(T &&_x,S &&..._t){cerr<<_x<<", ";_do(_t...);}
template<typename _a,typename _b> ostream& operator << (ostream &_s,const pair<_a,_b> &_p){return _s<<"("<<_p.X<<","<<_p.Y<<")";}
template<typename It> ostream& _OUTC(ostream &_s,It _ita,It _itb)
{
    _s<<"{";
    for(It _it=_ita;_it!=_itb;_it++)
    {
        _s<<(_it==_ita?"":",")<<*_it;
    }
    _s<<"}";
    return _s;
}
template<typename _a> ostream &operator << (ostream &_s,vector<_a> &_c){return _OUTC(_s,ALL(_c));}
template<typename _a> ostream &operator << (ostream &_s,set<_a> &_c){return _OUTC(_s,ALL(_c));}
template<typename _a,typename _b> ostream &operator << (ostream &_s,map<_a,_b> &_c){return _OUTC(_s,ALL(_c));}
template<typename _t> void pary(_t _a,_t _b){_OUTC(cerr,_a,_b);cerr<<endl;}
#define IOS()
#else
#define debug(...)
#define pary(...)
#define endl '\n'
#define IOS() ios_base::sync_with_stdio(0);cin.tie(0);
#endif // cold66
//}
template<class T> inline bool chkmax(T &a, const T &b) { return b > a ? a = b, true : false; }
template<class T> inline bool chkmin(T &a, const T &b) { return b < a ? a = b, true : false; }
template<class T> using MaxHeap = priority_queue<T>;
template<class T> using MinHeap = priority_queue<T, vector<T>, greater<T>>;

const ll MAXn=1e6+5,MAXlg=__lg(MAXn)+2;
const ll MOD=1000000007;
const ll INF=0x3f3f3f3f3f3f3f3f;

vector<ll> re[MAXn];
bool vis[MAXn];
ll dp[MAXn],out[MAXn];
int main(){
  IOS();
  ll n,m;
  cin>>n>>m;
  REP(i,n) dp[i] = INF;
  REP(i,m){
    ll u,v;
    cin>>u>>v;
    u--,v--;
    re[v].eb(u);
    out[u]++;
  }
  ll st,dst;
  cin>>st>>dst;
  st--,dst--;
  deque<ll> dq;
  dq.pb(dst);
  dp[dst] = 0;
  while(SZ(dq)){
    ll cur = dq.front();
    dq.pop_front();
    if(cur == st) break;
    if(vis[cur]) continue;
    vis[cur] = true;
    for(auto i:re[cur]){
      out[i]--;
      if(!out[i]){
        if(chkmin(dp[i],dp[cur])) dq.push_front(i);
      }
      else if(dp[i] == INF){
        dp[i] = dp[cur]+1;
        dq.pb(i);
      }
    }
  }
  cout<<dp[st]<<endl;
}


#include <bits/stdc++.h>
using namespace std;
#define mx 1000100

//we create a queue and start from the last vertex that we need to find and go
//to the first vertex . we do this because in this way we can get the correct
//path and we can also say where we want to add 1 to the cost or not . If the vertex
//we are considering has more than 1 outgoing edge other than the edge we want
// we have to select our edge by adding 1 to order.

int main() {

	int n , r , q[2*mx] , freq[mx] ={0} , s , t , order[mx] , vis[mx] = {0};
	//take input
	cin >> n >> r;
	// vector which will store edges in reverse order v[y] = x gives that
	// their is and edge from  x -> y
	vector<int> vec[n+1];
	for( int i = 1 ; i <= r ; i++)
	{
		int x , y;
		cin >> x >> y;
		vec[y].push_back( x );
		// freq is used for number of outgoing edges from vertex x to any other
		//vertex.
		freq[x]++;
	}
	memset( order , 255 , sizeof( order ));
	cin >> s >> t ;
	//assigning the order of t as 0
	order[t] = 0 ;
	int qfirst = mx , qlast = mx + 1;
	q[qfirst] = t;
	while( qfirst < qlast )
	{
		//getting the 1st element of queue.
		int v = q[qfirst++];
		if( v == s )
		{
			// if we find the start vertex.
			break;
		}
		if( vis[v] == 1 )
		{
			if we have already visited the vertex then we don't need to
			visit it again.
			continue;
		}
		vis[v] = 1;
		//for nbr u of v.
		for( auto k : vec[v] )
		{
			int u = k;
			// if outgoing edge from u is only 1 then we know
			// we don't need to give an order
			if( --freq[u] == 0 ) //can be executed more than once
			{
				// we check for o(u) > o(v) 'cause freq can be decremented
				//many times and might eventually reach zero but we want to
				//maintain the lowest order of u so we only replace when we have
				// found a smaller order or if  u is not visited even once.
				if( order[u] > order[v] || order[u] == -1 )
				{
					order[u] = order[v];
					// we push u into front of queue 'cause we want to get the minimum
					//distance first.
					q[--qfirst] = u;
				}
			}
			else if( order[u] == -1 )
			{
				//if outgoing edges of u is not zero and it has been not been visited
				// even once then we increment by one.
				order[u] = order[v] + 1;
				//we push u into back because in queue we want to maintain those
				//vertex which have smaller order.
				q[qlast++] = u;
			}
			//if outgoing edges are not zero and order of u is also not -1 then
			//we know that u has already been visited and been assigned with a
			// low value and we don't need to temper with it.

		}

	}

	cout<<order[s];

	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define mx 1000100

//we create a queue and start from the last vertex that we need to find and go
//to the first vertex . we do this because in this way we can get the correct
//path and we can also say where we want to add 1 to the cost or not . If the vertex
//we are considering has more than 1 outgoing edge other than the edge we want
// we have to select our edge by adding 1 to order.

int main() {
	std::ios_base::sync_with_stdio(false);
	int n , r , q[4*mx] , freq[mx] ={0} , s , t , order[mx] , vis[mx] = {0};
	//take input
	cin >> n >> r;
	// vector which will store edges in reverse order v[y] = x gives that
	// their is and edge from  x -> y
	vector<int> vec[n+1];
	for( int i = 1 ; i <= r ; i++)
	{
		int x , y;
		cin >> x >> y;
		vec[y].push_back( x );
		// freq is used for number of outgoing edges from vertex x to any other
		//vertex.
		freq[x]++;
	}
	memset( order , -1 , sizeof( order ));
	cin >> s >> t ;
	//assigning the order of t as 0
	order[t] = 0 ;
	int qfirst = mx , qlast = mx + 1;
	q[qfirst] = t;
	while( qfirst < qlast )
	{
		//getting the 1st element of queue.
		int v = q[qfirst++];
		if( v == s )
		{
			// if we find the start vertex.
			break;
		}
		if( vis[v] == 1 )
		{
			//if we have already visited the vertex then we don't need to
			//visit it again.
			continue;
		}
		vis[v] = 1;
		//for nbr u of v.
		for( auto k : vec[v] )
		{
			int u = k;
			// if outgoing edge from u is only 1 then we know
			// we don't need to give an order

			//can be executed more than once
			if( --freq[u] == 0 )
			{
				// we check for o(u) > o(v) 'cause freq can be decremented
				//many times and might eventually reach zero but we want to
				//maintain the lowest order of u so we only replace when we have
				// found a smaller order or if  u is not visited even once.
				if( order[u] > order[v] || order[u] == -1 )
				{
					order[u] = order[v];
					// we push u into front of queue 'cause we want to get the minimum
					//distance first.
					q[--qfirst] = u;
				}
			}
			else if( order[u] == -1 )
			{
				//if outgoing edges of u is not zero and it has been not been visited
				// even once then we increment by one.
				order[u] = order[v] + 1;
				//we push u into back because in queue we want to maintain those
				//vertex which have smaller order.
				q[qlast++] = u;
			}
			//if outgoing edges are not zero and order of u is also not -1 then
			//we know that u has already been visited and been assigned with a
			// low value and we don't need to temper with it.

		}

	}

	cout<<order[s];

	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define mx 1000100

//we create a queue and start from the last vertex that we need to find and go
//to the first vertex . we do this because in this way we can get the correct
//path and we can also say where we want to add 1 to the cost or not . If the vertex
//we are considering has more than 1 outgoing edge other than the edge we want
// we have to select our edge by adding 1 to order.

int main() {

	int n , r , q[2*mx] , freq[mx] ={0} , s , t , order[mx] , vis[mx] = {0};
	//take input
	cin >> n >> r;
	// vector which will store edges in reverse order v[y] = x gives that
	// their is and edge from  x -> y
	vector<int> vec[n+1];
	for( int i = 1 ; i <= r ; i++)
	{
		int x , y;
		cin >> x >> y;
		vec[y].push_back( x );
		// freq is used for number of outgoing edges from vertex x to any other
		//vertex.
		freq[x]++;
	}
	memset( order , 255 , sizeof( order ));
	cin >> s >> t ;
	//assigning the order of t as 0
	order[t] = 0 ;
	int qfirst = mx , qlast = mx + 1;
	q[qfirst] = t;
	while( qfirst < qlast )
	{
		//getting the 1st element of queue.
		int v = q[qfirst++];
		if( v == s )
		{
			// if we find the start vertex.
			break;
		}
		if( vis[v] == 1 )
		{
			//if we have already visited the vertex then we don't need to
			//visit it again.
			continue;
		}
		vis[v] = 1;
		//for nbr u of v.
		for( auto k : vec[v] )
		{
			int u = k;
			// if outgoing edge from u is only 1 then we know
			// we don't need to give an order

			//can be executed more than once
			if( --freq[u] == 0 )
			{
				// we check for o(u) > o(v) 'cause freq can be decremented
				//many times and might eventually reach zero but we want to
				//maintain the lowest order of u so we only replace when we have
				// found a smaller order or if  u is not visited even once.
				if( order[u] > order[v] || order[u] == -1 )
				{
					order[u] = order[v];
					// we push u into front of queue 'cause we want to get the minimum
					//distance first.
					q[--qfirst] = u;
				}
			}
			else if( order[u] == -1 )
			{
				//if outgoing edges of u is not zero and it has been not been visited
				// even once then we increment by one.
				order[u] = order[v] + 1;
				//we push u into back because in queue we want to maintain those
				//vertex which have smaller order.
				q[qlast++] = u;
			}
			//if outgoing edges are not zero and order of u is also not -1 then
			//we know that u has already been visited and been assigned with a
			// low value and we don't need to temper with it.

		}

	}

	cout<<order[s];

	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define mx 1000100

//we create a queue and start from the last vertex that we need to find and go
//to the first vertex . we do this because in this way we can get the correct
//path and we can also say where we want to add 1 to the cost or not . If the vertex
//we are considering has more than 1 outgoing edge other than the edge we want
// we have to select our edge by adding 1 to order.

int main() {
	std::ios_base::sync_with_stdio(false);
	int n , r , q[4*mx] , freq[mx] ={0} , s , t , order[mx] , vis[mx] = {0};
	//take input
	cin >> n >> r;
	// vector which will store edges in reverse order v[y] = x gives that
	// their is and edge from  x -> y
	vector<int> vec[n+1];
	for( int i = 1 ; i <= r ; i++)
	{
		int x , y;
		cin >> x >> y;
		vec[y].push_back( x );
		// freq is used for number of outgoing edges from vertex x to any other
		//vertex.
		freq[x]++;
	}
	for( int i = 1 ; i <= n ; i++)
	{
		order[i] = -1;
	}
	cin >> s >> t ;
	//assigning the order of t as 0
	order[t] = 0 ;
	int qfirst = mx , qlast = mx + 1;
	q[qfirst] = t;
	while( qfirst < qlast )
	{
		//getting the 1st element of queue.
		int v = q[qfirst++];
		if( v == s )
		{
			// if we find the start vertex.
			break;
		}
		if( vis[v] == 1 )
		{
			//if we have already visited the vertex then we don't need to
			//visit it again.
			continue;
		}
		vis[v] = 1;
		//for nbr u of v.
		for( auto k : vec[v] )
		{
			int u = k;
			// if outgoing edge from u is only 1 then we know
			// we don't need to give an order

			//can be executed more than once
			if( --freq[u] == 0 )
			{
				// we check for o(u) > o(v) 'cause freq can be decremented
				//many times and might eventually reach zero but we want to
				//maintain the lowest order of u so we only replace when we have
				// found a smaller order or if  u is not visited even once.
				if( order[u] > order[v] || order[u] == -1 )
				{
					order[u] = order[v];
					// we push u into front of queue 'cause we want to get the minimum
					//distance first.
					q[--qfirst] = u;
				}
			}
			else if( order[u] == -1 )
			{
				//if outgoing edges of u is not zero and it has been not been visited
				// even once then we increment by one.
				order[u] = order[v] + 1;
				//we push u into back because in queue we want to maintain those
				//vertex which have smaller order.
				q[qlast++] = u;
			}
			//if outgoing edges are not zero and order of u is also not -1 then
			//we know that u has already been visited and been assigned with a
			// low value and we don't need to temper with it.

		}

	}

	cout<<order[s];

	return 0;
}


#include <cstdio>
#include <vector>
#include <memory.h>
using namespace std;
const int MX=1000100,MD=2*MX;
int n,m,i,j,k,x,y,z,fi,fr,v[MX],p[MX],q[2*MD];
vector<int> g[MX],o[MX];
bool u[MX];
int main() {
  scanf("%d%d",&n,&m);
  for (i=0; i<m; i++) {
    scanf("%d%d",&x,&y);
	g[x].push_back(y);
	o[y].push_back(x);
	v[x]++;
  }
  scanf("%d%d",&x,&y);
  memset(p,255,sizeof(p));
  p[y]=0; q[MD]=y; fi=MD; fr=fi+1;
  while (fi<fr) {
    i=q[fi++];
	if (i==x) break;
	if (u[i]) continue;
	u[i]=true;
	for (j=0; j<o[i].size(); j++) {
	  y=o[i][j];
	  if (--v[y]==0) {
		if (p[i]<p[y] || p[y]==-1) {
		  p[y]=p[i];
		  q[--fi]=y;
		}
	  } else if (p[y]==-1) {
	    p[y]=p[i]+1;
		q[fr++]=y;
	  }
	}
  }
  printf("%d\n",p[x]);
  return 0;
}


#include <cstdio>
#include <deque>
#include <vector>
using namespace std;

const int Maxn = 1000005;

int n, m;
vector <int> bef[Maxn];
int out[Maxn];
int s, t;
int dist[Maxn];
bool was[Maxn];

int main()
{
	scanf("%d %d", &n, &m);
	while (m--) {
		int a, b; scanf("%d %d", &a, &b);
		out[a]++; bef[b].push_back(a);
	}
	scanf("%d %d", &s, &t);
	fill(dist, dist + n + 1, Maxn); dist[t] = 0;
	deque <int> Q; Q.push_back(t);
	while (!Q.empty()) {
		int v = Q.front(), d = dist[v]; Q.pop_front();
		if (was[v]) continue;
		was[v] = true;
		for (int i = 0; i < bef[v].size(); i++) {
			int u = bef[v][i];
			if (--out[u] == 0) {
				if (d < dist[u]) { dist[u] = d; Q.push_front(u); }
			} else if (d + 1 < dist[u]) { dist[u] = d + 1; Q.push_back(u); }
		}
	}
	printf("%d\n", dist[s] < Maxn? dist[s]: -1);
	return 0;
}


#include <bits/stdc++.h>
#define INF 0x3f3f3f3f
using namespace std;
const int maxn = 1000010;
vector<int> G[maxn], G1[maxn];
int dp[maxn];
bool v[maxn];
void add(int x, int y) {
	G[x].push_back(y);
	G1[y].push_back(x);
}
queue<int> q;
void spfa(int s, int t) {
	memset(dp, 0x3f, sizeof(dp));
	dp[s] = 0;
	v[s] = 1;
	q.push(s);
	while(q.size()) {
		int x = q.front();
		q.pop();
		v[x] = 0;
		for (auto y : G[x]) {
			if(dp[y] > dp[x] + 1) {
				dp[y] = dp[x] + 1;
				if(!v[y]) {
					q.push(y);
					v[y] = 1;
				}
			}
		}
		int tmp = 0;
		for (auto y : G1[x]) {
			tmp = max(tmp, dp[y]);
		}
		if(dp[x] > tmp) {
			dp[x] = tmp;
			if(!v[x]) {
				q.push(x);
				v[x] = 1;
			}
		}
	}
}
int main() {
	int n, m, x, y, s, t;
	scanf("%d%d", &n, &m);
	for (int i = 1; i <= m; i++) {
		scanf("%d%d", &x, &y);
		add(y, x);
	}
	scanf("%d%d", &s, &t);
	spfa(t, s);
	if(dp[s] == INF) dp[s] = -1;
	printf("%d\n", dp[s]);
}


#include <bits/stdc++.h>
using namespace std;
const int maxn = 1000010;
vector<int> G[maxn];
deque<int> q;
int dp[maxn], deg[maxn];
bool v[maxn];
void add(int x, int y) {
	G[x].push_back(y);
	deg[y]++;
}
void bfs(int s, int t) {
	memset(dp, -1, sizeof(dp));
	dp[s] = 0;
	q.push_back(s);
	while(q.size()) {
		int x = q.front();
		q.pop_front();
		if(v[x]) continue;
		v[x] = 1;
		//if(x == t) return;
		for (auto y : G[x]) {
			if(!--deg[y]) {
				if(dp[y] > dp[x] || dp[y] == -1) {
					dp[y] = dp[x];
					q.push_front(y);
				}
			} else if(dp[y] == -1) {
				dp[y] = dp[x] + 1;
				q.push_back(y);
			}
		}
	}
}
int main() {
	int n, m, x, y, s, t;
	scanf("%d%d", &n, &m);
	for (int i = 1; i <= m; i++) {
		scanf("%d%d", &x, &y);
		add(y, x);
	}
	scanf("%d%d", &s, &t);
	bfs(t, s);
	printf("%d\n", dp[s]);
}


#include <stdio.h>
#include <vector>
#include <deque>
#include <queue>
using namespace std;
const int MAXN = 1000005;
int n, m, a, b;
int d[MAXN];
vector<int> g[MAXN];
vector<pair<int, int> > v[MAXN];
int h[MAXN], worst[MAXN];
int main() {
	scanf("%d%d", &n, &m);
	for (int i = 0; i < m; i++) {
		int x, y; scanf("%d%d", &x, &y);
		x--; y--;
		g[x].push_back(y);
	}
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < g[i].size(); j++) {
			v[g[i][j]].push_back({ i, !(g[i].size() == 1) });
		}
	}
	for (int i = 0; i < n; i++) worst[i] = -1;
	deque <int> dq;
	int a, b; scanf("%d%d", &a, &b); a--; b--;
	for (int i = 0; i < n; i++) d[i] = 1e9;
	d[b] = 0;
	dq.push_front(b);
	while (!dq.empty()) {
		int x = dq.front(); dq.pop_front();
		for (int i = 0; i < v[x].size(); i++) {
			int y = v[x][i].first;
			h[y]++;
			worst[y] = max(worst[y], d[x]);
			int val = d[x] + v[x][i].second;
			if (h[y] == g[y].size()) val = min(val, worst[y]);
			if (val < d[y]) {
				d[y] = val;
				if (val > d[x]) dq.push_back(y);
				else dq.push_front(y);
			}
		}
	}
	if (d[a] == 1e9) d[a] = -1;
	printf("%d", d[a]);
}


#include <bits/stdc++.h>
using namespace std;
inline int read(){
    int n= 0;
    char c;
    while((c=getchar())<48);
    n += (c-'0');
    while((c=getchar())>=48){
        n=n*10+(c-'0');
    }
    return n;
}
int outdegree[1000005];
list<int> adj[1000005];
int dis[1000005];
int visited[1000005];
int q[1000005];
int main(){
      int n,m,x,y,src,dest;
      n = read();
      m = read();

      for(int i=0;i<m;i++){
         x = read()-1;
         y = read()-1;
         adj[y].push_back(x);
         outdegree[x]++;
      }
      src = read()-1;
      dest = read()-1;
      for(int i=0;i<n;i++){
          dis[i] = -1;
          visited[i] = false;
      }
      dis[dest] = 0;
      int fr = 2,fi = 1;
      q[fi] = dest;
      while(fi<fr){
      	int u = q[fi++];
      	if(u == src){
      		break;
      	}
      	if(visited[u]){
      		continue;
      	}
      	visited[u] = true;
      	for(auto it=adj[u].begin();it!=adj[u].end();it++){
      		int v = *it;
      		if(--outdegree[v] == 0){
      			if(dis[v]>dis[u] || dis[v] == -1){
      				dis[v] = dis[u];
      				q[--fi] = v;
      			}
      		}else if(dis[v] == -1){
      				dis[v] = dis[u]+1;
      				q[fr++] = v;
      		}
      	}
      }
     cout<<dis[src]<<"\n";

}


#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
#define FOR(i, a, b) for (int (i) = (a); (i) <= (b); (i)++)
#define ROF(i, a, b) for (int (i) = (a); (i) >= (b); (i)--)
#define REP(i, n) FOR(i, 0, (n)-1)
#define sqr(x) ((x) * (x))
#define all(x) (x).begin(), (x).end()
#define reset(x, y) memset(x, y, sizeof(x))
#define uni(x) (x).erase(unique(all(x)), (x).end())
#define BUG(x) cerr << #x << " = " << (x) << endl
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define _1 first
#define _2 second
#define chkmin(a, b) a = min(a, b)
#define chkmax(a, b) a = max(a, b)

const int maxn = 1123456;

int n, m, s, t;
int f[maxn], deg[maxn];
bool vis[maxn];
vector<int> G[maxn];

int main() {
  scanf("%d%d", &n, &m);
  while (m--) {
    int u, v;
    scanf("%d%d", &u, &v);
    G[v].eb(u);
    deg[u]++;
  }
  scanf("%d%d", &s, &t);
  reset(f, -1);
  f[t] = 0;
  deque<int> q;
  q.eb(t);
  while (!q.empty()) {
    auto u = q.front(); q.pop_front();
    if (u == s) break;
    if (vis[u]) continue;
    vis[u] = true;
    for (auto v : G[u]) if (!--deg[v]) {
      if (f[v] == -1 || f[u] < f[v]) {
        f[v] = f[u];
        q.emplace_front(v);
      }
    } else if (f[v] == -1) {
      f[v] = f[u] + 1;
      q.eb(v);
    }
  }
  printf("%d", f[s]);
}


#include<bits/stdc++.h>
using namespace std;
const int inf = 0x3f3f3f3f;
const int maxn = 1e6  + 100;
vector<int> E[maxn];
vector<int> EE[maxn];
int dis[maxn];
int n,m;
int d[maxn];
bool in[maxn];
void spfa(int root){
    queue<int> Q;
    for (int i=1;i<=n;i++){
        dis[i] = inf;
    }
    in[root] = 1;
    dis[root] = 0;
    Q.push(root);
    while (!Q.empty()){
        int head = Q.front();Q.pop();
        in[head] = false;
        for (int v : E[head]){
            if (dis[head] + 1 < dis[v]){
                dis[v] = dis[head] + 1;
                if (!in[v]){
                    in[v] = true;
                    Q.push(v);
                }
            }
        }
        int tmp_dis = 0;
        for (int v : EE[head]){
            tmp_dis = max(tmp_dis,dis[v]);
        }
        if (tmp_dis < dis[head]){
            dis[head] = tmp_dis;
            in[head] = true;
            Q.push(head);
        }
    }
}
int main(){
    scanf("%d%d",&n,&m);
    for (int i=0;i<m;i++){
        int u,v;
        scanf("%d%d",&u,&v);
        EE[u].push_back(v);
        E[v].push_back(u);
//        d[u] ++;
    }
    int S,T;
    scanf("%d%d",&S,&T);
    spfa(T);
    printf("%d\n",dis[S]==inf?-1:dis[S]);
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int inf = 0x3f3f3f3f;
const int maxn = 1e6  + 100;
vector<int> E[maxn];
vector<int> EE[maxn];
int dis[maxn];
int n,m;
int d[maxn];
bool in[maxn];
void spfa(int root){
    queue<int> Q;
    for (int i=1;i<=n;i++){
        dis[i] = inf;
    }
    in[root] = 1;
    dis[root] = 0;
    Q.push(root);
    while (!Q.empty()){
        int head = Q.front();Q.pop();
        in[head] = false;
        for (int v : E[head]){
            if (dis[head] + 1 < dis[v]){
                dis[v] = dis[head] + 1;
                if (!in[v]){
                    in[v] = true;
                    Q.push(v);
                }
            }
        }
        int tmp_dis = 0;
        for (int v : EE[head]){
            tmp_dis = max(tmp_dis,dis[v]);
        }
        if (tmp_dis < dis[head]){
            dis[head] = tmp_dis;
            in[head] = true;
            Q.push(head);
        }
    }
}
int main(){
    scanf("%d%d",&n,&m);
    for (int i=0;i<m;i++){
        int u,v;
        scanf("%d%d",&u,&v);
        EE[u].push_back(v);
        E[v].push_back(u);
//        d[u] ++;
    }
    int S,T;
    scanf("%d%d",&S,&T);
    spfa(T);
    printf("%d\n",dis[S]);
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define F(i, j, k) for(register ll i = j; i <= k; ++i)
#define D(i, j, k) for(register ll i = j; i >= k; --i)
#define random(x) (rand() % (x)) + 1
#define pb push_back
#define fi first
#define se second
#define pp pair<ll, ll>
#define mp make_pair
#define error(x) cerr << #x << "=" << x << endl
//***********var defination*********
const ll maxn = 2e5+5;
const ll inf = 1e15;
ll n, m, x, y, z;
vector<ll> to[maxn];
ll f[maxn], g[maxn], inq[maxn], out[maxn];
int main() {
#ifdef orz
    freopen("input", "r", stdin);
#endif
    cin >> n >> m;
    while(m--) {
	cin >> x >> y;
	to[y].pb(x);
	out[x]++;
    }
    cin >> x >> y;
    F(i, 1, n) f[i] = inf, g[i] = 0;
    queue<ll> q;
    q.push(y);
    inq[y] = 1;
    f[y] = 0;
    F(i, 1, n) if(out[i] == 0) {
	q.push(i);
	inq[i] = 1;
    }
    while(!q.empty()) {
        ll u = q.front();
	q.pop();
	inq[u] = 0;
	for(auto v:to[u]) {
	    if(g[v] < f[u]) {
		g[v] = f[u];
		if(!inq[v]) {
		    q.push(v);
		    inq[v] = 1;
		}
	    }
	    if(f[v] > f[u] + 1) {
		f[v] = f[u] + 1;
		if(!inq[v]) {
		    q.push(v);
		    inq[v] = 1;
		}
	    }
	}
    }
    cout << min(f[x], g[x]);
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define F(i, j, k) for(register ll i = j; i <= k; ++i)
#define D(i, j, k) for(register ll i = j; i >= k; --i)
#define random(x) (rand() % (x)) + 1
#define pb push_back
#define fi first
#define se second
#define pp pair<ll, ll>
#define mp make_pair
#define error(x) cerr << #x << "=" << x << endl
//***********var defination*********
const ll maxn = 2e6+5;
const ll inf = 1e18;
ll n, m, x, y, z;
vector<ll> to[maxn], fto[maxn];
ll f[maxn], inq[maxn], out[maxn];
int main() {
#ifdef orz
    freopen("input", "r", stdin);
#endif
    cin >> n >> m;
    while(m--) {
	cin >> x >> y;
	to[y].pb(x);
	out[x]++;
    }
    cin >> x >> y;
    F(i, 1, n) f[i] = inf;
    queue<ll> q;
    q.push(y);
    inq[y] = 1;
    f[y] = 0;
    while(!q.empty()) {
        ll u = q.front();
	q.pop();
	inq[u] = 0;
	for(auto v:to[u]) {
	    if(f[u]+1 < f[v]) {
		f[v] = f[u]+1;
		if(!inq[v]) {
		    inq[v] = 1;
		    q.push(v);
		}
	    }
	}
	ll tmp = 0;
	for(auto v:fto[u]) tmp = max(tmp, f[v]);
	if(tmp < f[u]) {
	    f[u] = tmp;
	    q.push(u);
	    inq[u] = 1;
	}
    }
    if(f[x] < inf) cout << f[x];
    else cout << -1;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define F(i, j, k) for(register ll i = j; i <= k; ++i)
#define D(i, j, k) for(register ll i = j; i >= k; --i)
#define random(x) (rand() % (x)) + 1
#define pb push_back
#define fi first
#define se second
#define pp pair<ll, ll>
#define mp make_pair
#define error(x) cerr << #x << "=" << x << endl
//***********var defination*********
const ll maxn = 2e6+5;
const ll inf = 1e18;
ll n, m, x, y, z;
vector<ll> to[maxn], fto[maxn];
ll f[maxn], inq[maxn], out[maxn];
int main() {
#ifdef orz
    freopen("input", "r", stdin);
#endif
    cin >> n >> m;
    while(m--) {
	cin >> x >> y;
	to[y].pb(x);
	fto[x].pb(y);
	out[x]++;
    }
    cin >> x >> y;
    F(i, 1, n) f[i] = inf;
    queue<ll> q;
    q.push(y);
    inq[y] = 1;
    f[y] = 0;
    while(!q.empty()) {
        ll u = q.front();
	q.pop();
	inq[u] = 0;
	for(auto v:to[u]) {
	    ll tmp = 0;
	    for(auto k:fto[v]) tmp = max(tmp, f[k]);
	    if(tmp < f[v] || f[u] + 1 < f[v]) {
		f[v] = min(tmp, f[u]+1);
		if(!inq[v]) {
		    inq[v] = 1;
		    q.push(v);
		}
	    }
	}
    }
    if(f[x] < inf) cout << f[x];
    else cout << -1;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define F(i, j, k) for(register ll i = j; i <= k; ++i)
#define D(i, j, k) for(register ll i = j; i >= k; --i)
#define random(x) (rand() % (x)) + 1
#define pb push_back
#define fi first
#define se second
#define pp pair<ll, ll>
#define mp make_pair
#define error(x) cerr << #x << "=" << x << endl
//***********var defination*********
const ll maxn = 2e6+5;
const ll inf = 1e18;
ll n, m, x, y, z;
vector<ll> to[maxn], fto[maxn];
ll f[maxn], inq[maxn], out[maxn];
int main() {
#ifdef orz
    freopen("input", "r", stdin);
#endif
    cin >> n >> m;
    while(m--) {
	cin >> x >> y;
	to[y].pb(x);
	fto[x].pb(y);
	out[x]++;
    }
    cin >> x >> y;
    F(i, 1, n) f[i] = inf;
    queue<ll> q;
    q.push(y);
    inq[y] = 1;
    f[y] = 0;
    while(!q.empty()) {
        ll u = q.front();
	q.pop();
	inq[u] = 0;
	for(auto v:to[u]) {
	    if(f[u]+1 < f[v]) {
		f[v] = f[u]+1;
		if(!inq[v]) {
		    inq[v] = 1;
		    q.push(v);
		}
	    }
	}
	ll tmp = 0;
	for(auto v:fto[u]) tmp = max(tmp, f[v]);
	if(tmp < f[u]) {
	    f[u] = tmp;
	    q.push(u);
	    inq[u] = 1;
	}
    }
    if(f[x] < inf) cout << f[x];
    else cout << -1;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define F(i, j, k) for(register ll i = j; i <= k; ++i)
#define D(i, j, k) for(register ll i = j; i >= k; --i)
#define random(x) (rand() % (x)) + 1
#define pb push_back
#define fi first
#define se second
#define pp pair<ll, ll>
#define mp make_pair
#define error(x) cerr << #x << "=" << x << endl
//***********var defination*********
const ll maxn = 2e5+5;
const ll inf = 1e18;
ll n, m, x, y, z;
vector<ll> to[maxn];
ll f[maxn], inq[maxn], out[maxn];
int main() {
#ifdef orz
    freopen("input", "r", stdin);
#endif
    cin >> n >> m;
    while(m--) {
	cin >> x >> y;
	to[y].pb(x);
	out[x]++;
    }
    cin >> x >> y;
    F(i, 1, n) f[i] = inf;
    queue<ll> q;
    q.push(y);
    inq[y] = 1;
    f[y] = 0;
    F(i, 1, n) if(out[i] == 0 && i != y) {
	q.push(i);
	inq[i] = 1;
    }
    while(!q.empty()) {
        ll u = q.front();
	q.pop();
	inq[u] = 0;
	for(auto v:to[u]) {
	    ll tmp = 0;
	    for(auto k:to[v]) tmp = max(tmp, f[k]);
	    if(tmp < f[v] || f[u]+1 < f[v]) {
		f[v] = min(tmp, f[u]+1);
		if(!inq[v]) {
		    inq[v] = 1;
		    q.push(v);
		}
	    }
	}
    }
    if(f[x] < inf) cout << f[x];
    else cout << -1;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define F(i, j, k) for(register ll i = j; i <= k; ++i)
#define D(i, j, k) for(register ll i = j; i >= k; --i)
#define random(x) (rand() % (x)) + 1
#define pb push_back
#define fi first
#define se second
#define pp pair<ll, ll>
#define mp make_pair
#define error(x) cerr << #x << "=" << x << endl
//***********var defination*********
const ll maxn = 2e6+5;
const ll inf = 1e18;
ll n, m, x, y, z;
vector<ll> to[maxn];
ll f[maxn], inq[maxn], out[maxn];
int main() {
#ifdef orz
    freopen("input", "r", stdin);
#endif
    cin >> n >> m;
    while(m--) {
	cin >> x >> y;
	to[y].pb(x);
	out[x]++;
    }
    cin >> x >> y;
    F(i, 1, n) f[i] = inf;
    queue<ll> q;
    q.push(y);
    inq[y] = 1;
    f[y] = 0;
    while(!q.empty()) {
        ll u = q.front();
	q.pop();
	inq[u] = 0;
	for(auto v:to[u]) {
	    ll tmp = 0;
	    for(auto k:to[v]) tmp = max(tmp, f[k]);
	    if(tmp < f[v] || f[u]+1 < f[v]) {
		f[v] = min(tmp, f[u]+1);
		if(!inq[v]) {
		    inq[v] = 1;
		    q.push(v);
		}
	    }
	}
    }
    if(f[x] < inf) cout << f[x];
    else cout << -1;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define F(i, j, k) for(register ll i = j; i <= k; ++i)
#define D(i, j, k) for(register ll i = j; i >= k; --i)
#define random(x) (rand() % (x)) + 1
#define pb push_back
#define fi first
#define se second
#define pp pair<ll, ll>
#define mp make_pair
#define error(x) cerr << #x << "=" << x << endl
//***********var defination*********
const ll maxn = 2e6+5;
const ll inf = 1e18;
ll n, m, x, y, z;
vector<ll> to[maxn];
ll f[maxn], inq[maxn], out[maxn];
int main() {
#ifdef orz
    freopen("input", "r", stdin);
#endif
    cin >> n >> m;
    while(m--) {
	cin >> x >> y;
	to[y].pb(x);
	out[x]++;
    }
    cin >> x >> y;
    F(i, 1, n) f[i] = inf;
    queue<ll> q;
    q.push(y);
    inq[y] = 1;
    f[y] = 0;
    F(i, 1, n) if(out[i] == 0 && i != y) {
	q.push(i);
	inq[i] = 1;
    }
    while(!q.empty()) {
        ll u = q.front();
	q.pop();
	inq[u] = 0;
	for(auto v:to[u]) {
	    ll tmp = 0;
	    for(auto k:to[v]) tmp = max(tmp, f[k]);
	    if(tmp < f[v] || f[u]+1 < f[v]) {
		f[v] = min(tmp, f[u]+1);
		if(!inq[v]) {
		    inq[v] = 1;
		    q.push(v);
		}
	    }
	}
    }
    if(f[x] < inf) cout << f[x];
    else cout << -1;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define F(i, j, k) for(register ll i = j; i <= k; ++i)
#define D(i, j, k) for(register ll i = j; i >= k; --i)
#define random(x) (rand() % (x)) + 1
#define pb push_back
#define fi first
#define se second
#define pp pair<ll, ll>
#define mp make_pair
#define error(x) cerr << #x << "=" << x << endl
//***********var defination*********
const ll maxn = 2e5+5;
const ll inf = 1e15;
ll n, m, x, y, z;
vector<ll> to[maxn];
ll f[maxn], g[maxn], inq[maxn];
int main() {
#ifdef orz
    freopen("input", "r", stdin);
#endif
    cin >> n >> m;
    while(m--) {
	cin >> x >> y;
	to[y].pb(x);
    }
    cin >> x >> y;
    F(i, 1, n) f[i] = inf, g[i] = 0;
    queue<ll> q;
    q.push(y);
    inq[y] = 1;
    f[y] = 0;
    while(!q.empty()) {
        ll u = q.front();
	q.pop();
	inq[u] = 0;
	for(auto v:to[u]) {
	    if(g[v] < f[u]) {
		g[v] = f[u];
		if(!inq[v]) {
		    q.push(v);
		    inq[v] = 1;
		}
	    }
	    if(f[v] > f[u] + 1) {
		f[v] = f[u] + 1;
		if(!inq[v]) {
		    q.push(v);
		    inq[v] = 1;
		}
	    }
	}
    }
    cout << min(f[x], g[x]);
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define F(i, j, k) for(register ll i = j; i <= k; ++i)
#define D(i, j, k) for(register ll i = j; i >= k; --i)
#define random(x) (rand() % (x)) + 1
#define pb push_back
#define fi first
#define se second
#define pp pair<ll, ll>
#define mp make_pair
#define error(x) cerr << #x << "=" << x << endl
//***********var defination*********
const ll maxn = 2e5+5;
const ll inf = 1e15;
ll n, m, x, y, z;
vector<ll> to[maxn];
ll f[maxn], g[maxn], inq[maxn];
int main() {
#ifdef orz
    freopen("input", "r", stdin);
#endif
    cin >> n >> m;
    while(m--) {
	cin >> x >> y;
	to[y].pb(x);
    }
    cin >> x >> y;
    F(i, 1, n) f[i] = inf, g[i] = 0;
    queue<ll> q;
    q.push(y);
    inq[y] = 1;
    f[y] = 0;
    while(!q.empty()) {
        ll u = q.front();
	q.pop();
	inq[u] = 0;
	for(auto v:to[u]) {
	    if(g[v] < f[u] || g[v] < g[u]) {
		g[v] = max(f[u], g[u]);
		if(!inq[v]) {
		    q.push(v);
		    inq[v] = 1;
		}
	    }
	    if(f[v] > f[u] + 1) {
		f[v] = f[u] + 1;
		if(!inq[v]) {
		    q.push(v);
		    inq[v] = 1;
		}
	    }
	}
    }
    cout << min(f[x], g[x]);
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define F(i, j, k) for(register ll i = j; i <= k; ++i)
#define D(i, j, k) for(register ll i = j; i >= k; --i)
#define random(x) (rand() % (x)) + 1
#define pb push_back
#define fi first
#define se second
#define pp pair<ll, ll>
#define mp make_pair
#define error(x) cerr << #x << "=" << x << endl
//***********var defination*********
const ll maxn = 2e5+5;
const ll inf = 1e18;
ll n, m, x, y, z;
vector<ll> to[maxn];
ll f[maxn], inq[maxn], out[maxn];
int main() {
#ifdef orz
    freopen("input", "r", stdin);
#endif
    cin >> n >> m;
    while(m--) {
	cin >> x >> y;
	to[y].pb(x);
	out[x]++;
    }
    cin >> x >> y;
    F(i, 1, n) f[i] = inf;
    queue<ll> q;
    q.push(y);
    inq[y] = 1;
    f[y] = 0;
    F(i, 1, n) if(out[i] == 0 && i != y) {
	q.push(i);
	inq[i] = 1;
    }
    while(!q.empty()) {
        ll u = q.front();
	q.pop();
	inq[u] = 0;
	ll tmp = 0;
	for(auto v:to[u]) {
	    if(f[u] + 1 < f[v]) {
		f[v] = f[u] + 1;
		if(!inq[v]) {
		    inq[v] = 1;
		    q.push(v);
		}
	    }
	    tmp = max(tmp, f[v]);
	}
	if(tmp < f[u]) {
	    f[u] = tmp;
	    q.push(u);
	    inq[u] = 1;
	}
    }
    if(f[x] < inf) cout << f[x];
    else cout << -1;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define F(i, j, k) for(register ll i = j; i <= k; ++i)
#define D(i, j, k) for(register ll i = j; i >= k; --i)
#define random(x) (rand() % (x)) + 1
#define pb push_back
#define fi first
#define se second
#define pp pair<ll, ll>
#define mp make_pair
#define error(x) cerr << #x << "=" << x << endl
//***********var defination*********
const ll maxn = 2e5+5;
const ll inf = 1e15;
ll n, m, x, y, z;
vector<ll> to[maxn];
ll f[maxn], inq[maxn], out[maxn];
int main() {
#ifdef orz
    freopen("input", "r", stdin);
#endif
    cin >> n >> m;
    while(m--) {
	cin >> x >> y;
	to[y].pb(x);
	out[x]++;
    }
    cin >> x >> y;
    F(i, 1, n) f[i] = inf;
    queue<ll> q;
    q.push(y);
    inq[y] = 1;
    f[y] = 0;
    F(i, 1, n) if(out[i] == 0) {
	q.push(i);
	inq[i] = 1;
    }
    while(!q.empty()) {
        ll u = q.front();
	q.pop();
	inq[u] = 0;
	for(auto v:to[u]) {
	    ll tmp = 0;
	    for(auto k:to[v]) tmp = max(tmp, f[k]);
	    if(tmp < f[v] || f[u]+1 < f[v]) {
		f[v] = min(tmp, f[u]+1);
		if(!inq[v]) {
		    inq[v] = 1;
		    q.push(v);
		}
	    }
	}
    }
    if(f[x] < inf) cout << f[x];
    else cout << -1;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define F(i, j, k) for(register ll i = j; i <= k; ++i)
#define D(i, j, k) for(register ll i = j; i >= k; --i)
#define random(x) (rand() % (x)) + 1
#define pb push_back
#define fi first
#define se second
#define pp pair<ll, ll>
#define mp make_pair
#define error(x) cerr << #x << "=" << x << endl
//***********var defination*********
const ll maxn = 2e6+5;
const ll inf = 1e18;
ll n, m, x, y, z;
vector<ll> to[maxn], fto[maxn];
ll f[maxn], inq[maxn], out[maxn];
int main() {
#ifdef orz
    freopen("input", "r", stdin);
#endif
    cin >> n >> m;
    while(m--) {
	cin >> x >> y;
	to[y].pb(x);
	out[x]++;
    }
    cin >> x >> y;
    F(i, 1, n) f[i] = inf;
    queue<ll> q;
    q.push(y);
    inq[y] = 1;
    f[y] = 0;
    while(!q.empty()) {
        ll u = q.front();
	q.pop();
	inq[u] = 0;
	for(auto v:to[u]) {
	    if(f[u]+1 < f[v]) {
		f[v] = f[u]+1;
		if(!inq[v]) {
		    inq[v] = 1;
		    q.push(v);
		}
	    }
	}
	ll tmp = 0;
	for(auto v:rto[u]) tmp = max(tmp, f[v]);
	if(tmp < f[u]) {
	    f[u] = tmp;
	    q.push(u);
	    inq[u] = 1;
	}
    }
    if(f[x] < inf) cout << f[x];
    else cout << -1;
    return 0;
}


#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
#define int long long
#define mkp make_pair
#define pb push_back
#define ff first
#define ss second
#define debug1(a) cout<<a<<endl;
#define debug2(a,b) cout<<a<<' '<<b<<endl;
#define debug3(a,b,c) cout<<a<' '<<b<<' '<<c<<endl;
#define rep(i,n) for(int i=0;i<n;i++)
#define repr(i,a,b)for(int i=a;i<b;i++)
#define repre(i,a,b)for(int i=a;i<=b;i++)
#define clr1(arr) memset(arr,-1,sizeof(arr));
#define clr0(arr) memset(arr,0,sizeof(arr));
#define pi pair<int,int>
#define pii pair<int,pi>
#define sortV(v) v.begin(),v.end()
#define pq priority_queue
#define mpq priority_queue<int,vector<int>,greater<int> >
#define fastio ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
using namespace __gnu_pbds;
using namespace std;
typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> orderedSet;
typedef tree<int,null_type,less_equal<int>,rb_tree_tag,tree_order_statistics_node_update> orderedMSet;
//*p.find_by_order(index) return value at index
//p.order_of_key(key) return index
// DSU
/*
const int maxn=100009;
int arr[maxn];
int sz[maxn];
void init(){rep(i,maxn){arr[i]=i;sz[i]=1;}}
int root(int a){while(a!=arr[a]){arr[a]=arr[arr[a]];a=arr[a];}return a;}
int check(int a,int b){if(root(a)==root(b))return 1;return 0;}
int un(int a,int b)
{
  int roota=root(a);int rootb=root(b);
  if(sz[roota]>=sz[rootb]){sz[roota]+=sz[rootb];arr[rootb]=roota;}
  else {sz[rootb]+=sz[roota];arr[roota]=rootb;}
}
*/

//Dijkstra
/*
const int maxn=0x3f3f3f3f;
void dijkstra(int n,int dist[],vector<pi >adj[],int s)
{
  int visited[n+1];
  repre(i,0,n)
  {
    visited[i]=0;
    dist[i]=maxn;
  }
  dist[s]=0;
  priority_queue<pi ,vector<pi>,greater<pi> >p;
  p.push(mkp(0,s));
  while(p.empty()==false)
  {
    pi temp=p.top();
    p.pop();
    if(visited[temp.ss])continue;
    visited[temp.ss]=1;
    int u=temp.ss;
    rep(i,adj[u].size())
    {
      if(dist[u]+adj[u][i].ss<dist[adj[u][i].ff])
      {
        dist[adj[u][i].ff]=dist[u]+adj[u][i].ss;
        p.push(mkp(dist[adj[u][i].ff],adj[u][i].ff));
      }
    }
  }
}*/
int32_t main()
{
  //freopen("input.txt", "r", stdin);
  //freopen("output.txt", "w", stdout);
  fastio
  int n,m;
  cin>>n>>m;
  vector<int>adj[n+1];
  int in[n+1]={0};
  rep(i,m)
  {
    int u,v;
    cin>>u>>v;
    adj[v].pb(u);
    in[u]++;
  }
  int s,t;
  cin>>s>>t;
  int ans[n+1];
  clr1(ans);
  ans[t]=0;
  deque<int>q;
  q.push_front(t);
  int visited[n+1]={0};
  while(q.empty()==false)
  {
    int temp=q.front();
    q.pop_front();
    if(temp==s)break;
    if(visited[temp])continue;
    visited[temp]=1;
    for(auto u:adj[temp])
    {
      in[u]--;
      if(in[u]==0)
      {
        if((ans[temp]<ans[u]) || ans[u]==-1)
        {
          ans[u]=ans[temp];
          q.push_front(u);
        }
      }
      else
      {
        if(ans[u]==-1)ans[u]=ans[temp]+1;
        q.push_back(u);
      }
    }
  }
  cout<<ans[s]<<'\n';

  return 0;
}


#include <cstdio>
#include <iostream>
#include <vector>
#include <set>
#include <map>
#include <list>
#include <queue>
#include <stack>
#include <string>
#include <cstring>
#include <algorithm>

using namespace std;

deque<int> dq;
vector<int> adj[1000005];
int DP[1000005];
int deg[1000005];

int main(){
	int n, m, s, t;
	scanf("%d%d", &n, &m);
	for(int i=0;i<m;i++){
		scanf("%d%d", &s, &t);
		adj[t].push_back(s);
		deg[s]++;
	}
	scanf("%d%d", &s, &t);
	memset(DP, -1, sizeof(DP));

	DP[t] = 0;
	dq.push_front(t);
	while(dq.size()){
		int u = dq.front();
		dq.pop_front();

		for(auto v:adj[u]){
			deg[v]--;
			if(deg[v] == 0){
				if(DP[v] == -1 || DP[v] > DP[u]){
					DP[v] = DP[u];
					dq.push_front(v);
				}
			}
			else{
				if(DP[v] == -1 || DP[v] > DP[u]+1){
					DP[v] = DP[u]+1;
					dq.push_back(v);
				}
			}
		}
	}
	printf("%d", DP[s]);
}


#include <cstdio>
#include <iostream>
#include <vector>
#include <set>
#include <map>
#include <list>
#include <queue>
#include <stack>
#include <string>
#include <cstring>
#include <algorithm>

using namespace std;

deque<int> dq;
vector<int> adj[1000005];
int DP[1000005];
int deg[1000005];

int main(){
	int n, m, s, t;
	scanf("%d%d", &n, &m);
	for(int i=0;i<m;i++){
		scanf("%d%d", &s, &t);
		adj[t].push_back(s);
		deg[s]++;
	}
	scanf("%d%d", &s, &t);
	memset(DP, -1, sizeof(DP));

	DP[t] = 0;
	dq.push_front(t);
	while(dq.size()){
		int u = dq.front();
		dq.pop_front();

		for(auto v:adj[u]){
			deg[v]--;
			if(deg[v] == 0){
				if(DP[v] == -1 || DP[v] > DP[u]){
					DP[v] = DP[u];
					dq.push_front(v);
				}
			}
			else{
				if(DP[v] == -1){
					DP[v] = DP[u]+1;
					dq.push_back(v);
				}
			}
		}
	}

	printf("%d", DP[s]);
}


#include <cstdio>
#include <iostream>
#include <vector>
#include <set>
#include <map>
#include <list>
#include <queue>
#include <stack>
#include <string>
#include <cstring>
#include <algorithm>

using namespace std;

vector<int> adj[1000005];
int deg[1000005];
int DP[1000005];

int main(){
	int n, m, s, t;
	scanf("%d%d", &n, &m);
	for(int i=0;i<m;i++){
		int a, b;
		scanf("%d%d", &a, &b);
		adj[a].push_back(b);
	}
	scanf("%d%d", &s, &t);

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

	for(int i=1;i<=n;i++) deg[i] = adj[i].size();

	queue<int> q;
	q.push(s);
	DP[s] = 0;
	while(!q.empty()){
		int u = q.front();
		q.pop();
		for(auto v:adj[u]){
			if(deg[v] == 0){
				DP[u]++;
				break;
			}
		}
		for(auto v:adj[u]){
			if(DP[v] == -1 || DP[v] > DP[u]){
				DP[v] = DP[u];
				q.push(v);
			}
		}
	}
	printf("%d", DP[t]);
}


#include <cstdio>
#include <iostream>
#include <vector>
#include <set>
#include <map>
#include <list>
#include <queue>
#include <stack>
#include <string>
#include <cstring>
#include <algorithm>

using namespace std;

deque<int> dq;
vector<int> adj[1000005];
int DP[1000005];
int deg[1000005];
bool vis[1000005];

int main(){
	int n, m, s, t;
	scanf("%d%d", &n, &m);
	for(int i=0;i<m;i++){
		scanf("%d%d", &s, &t);
		adj[t].push_back(s);
		deg[s]++;
	}
	scanf("%d%d", &s, &t);
	memset(DP, -1, sizeof(DP));

	DP[t] = 0;
	dq.push_front(t);
	while(dq.size()){
		int u = dq.front();
		dq.pop_front();
		if(vis[u]) continue;
		vis[u] = 1;

		for(auto v:adj[u]){
			deg[v]--;
			if(deg[v] == 0){
				if(DP[v] == -1 || DP[v] > DP[u]){
					DP[v] = DP[u];
					dq.push_front(v);
				}
			}
			else{
				if(DP[v] == -1 || DP[v] > DP[u]+1){
					DP[v] = DP[u]+1;
					dq.push_back(v);
				}
			}
		}
	}

	printf("%d", DP[s]);
}


import java.io.*;
import java.util.*;

public class CF {

	void solve() {
		int n = in.nextInt();
		int m = in.nextInt();
		ArrayList<Integer>[] g = new ArrayList[n];
		ArrayList<Integer>[] gR = new ArrayList[n];
		for (int i = 0; i < n; i++) {
			g[i] = new ArrayList<>();
			gR[i] = new ArrayList<>();
		}
		for (int i = 0; i < m; i++) {
			int fr = in.nextInt() - 1;
			int to = in.nextInt() - 1;
			g[fr].add(to);
			gR[to].add(fr);
		}
		int s = in.nextInt() - 1;
		int t = in.nextInt() - 1;
		int[] ans = new int[n];
		Arrays.fill(ans, Integer.MAX_VALUE / 2);
		ans[t] = 0;
		int[] cnt = new int[n];
		for (int i = 0; i < n; i++)
			cnt[i] = g[i].size();
		ArrayList<Integer> cur = new ArrayList<>();
		HashSet<Integer> nextGen = new HashSet<>();
		cur.add(t);
		while (true) {
			if (cur.size() == 0) {
				break;
			}
			nextGen.clear();
			for (int i = 0; i < cur.size(); i++) {
				int v = cur.get(i);
				for (int j = 0; j < gR[v].size(); j++) {
					int newv = gR[v].get(j);
					cnt[newv]--;
					if (cnt[newv] == 0 && ans[newv] > ans[v]) {
						ans[newv] = ans[v];
						cur.add(newv);
						nextGen.remove(newv);
					}
					if (cnt[newv] > 0 && ans[newv] > ans[v] + 1) {
						nextGen.add(newv);
						ans[newv] = ans[v] + 1;
					}
				}
			}
			cur.clear();
			for (int x : nextGen)
				cur.add(x);
		}
		if (ans[s] == Integer.MAX_VALUE / 2) {
			out.println(-1);
		} else {
			out.println(ans[s]);
		}
	}

	FastScaner in;
	PrintWriter out;

	void run() {
		in = new FastScaner(System.in);
		out = new PrintWriter(System.out);

		solve();

		out.close();
	}

	void runWithFiles() {
		in = new FastScaner(new File("input.txt"));
		try {
			out = new PrintWriter(new File("output.txt"));
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}

		solve();

		out.close();
	}

	public static void main(String[] args) {
		Locale.setDefault(Locale.US);
		new CF().run();
	}

	class FastScaner {
		BufferedReader br;
		StringTokenizer st;

		FastScaner(InputStream is) {
			br = new BufferedReader(new InputStreamReader(is));
		}

		FastScaner(File f) {
			try {
				br = new BufferedReader(new FileReader(f));
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			}
		}

		String next() {
			while (st == null || !st.hasMoreElements()) {
				String s = null;
				try {
					s = br.readLine();
				} catch (IOException e) {
					e.printStackTrace();
				}
				if (s == null)
					return null;
				st = new StringTokenizer(s);
			}
			return st.nextToken();
		}

		int nextInt() {
			return Integer.parseInt(next());
		}

		long nextLong() {
			return Long.parseLong(next());
		}

		double nextDouble() {
			return Double.parseDouble(next());
		}
	}
}


import java.io.*;
import java.util.*;

public class CF {

	void solve() {
		int n = in.nextInt();
		int m = in.nextInt();
		ArrayList<Integer>[] gR = new ArrayList[n];
		for (int i = 0; i < n; i++) {
			gR[i] = new ArrayList<>();
		}
		int[] cnt = new int[n];
		for (int i = 0; i < m; i++) {
			int fr = in.nextInt() - 1;
			int to = in.nextInt() - 1;
			cnt[fr]++;
			gR[to].add(fr);
		}
		int s = in.nextInt() - 1;
		int t = in.nextInt() - 1;
		int[] ans = new int[n];
		Arrays.fill(ans, Integer.MAX_VALUE / 2);
		ans[t] = 0;
		ArrayList<Integer> cur = new ArrayList<>();
		HashSet<Integer> nextGen = new HashSet<>();
		cur.add(t);
		boolean[] was =new boolean[n];
		while (true) {
			if (cur.size() == 0) {
				break;
			}
			nextGen.clear();
			for (int i = 0; i < cur.size(); i++) {
				int v = cur.get(i);
				if (was[v])
					continue;
				was[v] = true;
				for (int j = 0; j < gR[v].size(); j++) {
					int newv = gR[v].get(j);
					cnt[newv]--;
					if (cnt[newv] == 0 && ans[newv] > ans[v]) {
						ans[newv] = ans[v];
						cur.add(newv);
						nextGen.remove(newv);
					}
					if (cnt[newv] > 0 && ans[newv] > ans[v] + 1) {
						nextGen.add(newv);
						ans[newv] = ans[v] + 1;
					}
				}
			}
			cur.clear();
			for (Integer xx : nextGen)
				cur.add(xx);
		}
		if (ans[s] == Integer.MAX_VALUE / 2) {
			out.println(-1);
		} else {
			out.println(ans[s]);
		}
	}

	FastScaner in;
	PrintWriter out;

	void run() {
		in = new FastScaner(System.in);
		out = new PrintWriter(System.out);

		solve();

		out.close();
	}

	void runWithFiles() {
		in = new FastScaner(new File("input.txt"));
		try {
			out = new PrintWriter(new File("output.txt"));
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}

		solve();

		out.close();
	}

	public static void main(String[] args) {
		Locale.setDefault(Locale.US);
		new CF().run();
	}

	class FastScaner {
		BufferedReader br;
		StringTokenizer st;

		FastScaner(InputStream is) {
			br = new BufferedReader(new InputStreamReader(is));
		}

		FastScaner(File f) {
			try {
				br = new BufferedReader(new FileReader(f));
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			}
		}

		String next() {
			while (st == null || !st.hasMoreElements()) {
				String s = null;
				try {
					s = br.readLine();
				} catch (IOException e) {
					e.printStackTrace();
				}
				if (s == null)
					return null;
				st = new StringTokenizer(s);
			}
			return st.nextToken();
		}

		int nextInt() {
			return Integer.parseInt(next());
		}

		long nextLong() {
			return Long.parseLong(next());
		}

		double nextDouble() {
			return Double.parseDouble(next());
		}
	}
}


import java.io.OutputStreamWriter;
import java.io.BufferedWriter;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.Random;
import java.io.Writer;
import java.util.List;
import java.io.IOException;
import java.util.Arrays;
import java.util.InputMismatchException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.NoSuchElementException;
import java.math.BigInteger;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 * @author Egor Kulikov (egor@egork.net)
 */
public class Main {
	public static void main(String[] args) {
		InputStream inputStream = System.in;
		OutputStream outputStream = System.out;
		InputReader in = new InputReader(inputStream);
		OutputWriter out = new OutputWriter(outputStream);
		TaskD solver = new TaskD();
		solver.solve(1, in, out);
		out.close();
	}
}

class TaskD {
    public void solve(int testNumber, InputReader in, OutputWriter out) {
		int count = in.readInt();
		int edgeCount = in.readInt();
		int[] from = new int[edgeCount];
		int[] to = new int[edgeCount];
		IOUtils.readIntArrays(in, from, to);
		MiscUtils.decreaseByOne(from, to);
		Graph graph = Graph.createGraph(count, to, from);
		int[] degree = new int[count];
		for (int i : from)
			degree[i]++;
		int source = in.readInt() - 1;
		int destination = in.readInt() - 1;
		int[] level = new int[count];
		Arrays.fill(level, -1);
		level[destination] = 0;
		int[] queue = new int[count];
		int size = 1;
		queue[0] = destination;
		for (int i = 0; size != 0; i++) {
			IntSet next = new IntHashSet();
			IntSet thisLevel = new IntHashSet();
			for (int j = 0; j < size; j++) {
				int current = queue[j];
				for (int k = graph.firstOutbound(current); k != -1; k = graph.nextOutbound(k)) {
					int candidate = graph.destination(k);
					if (level[candidate] != -1)
						continue;
					if (--degree[candidate] == 0) {
						queue[size++] = candidate;
						level[candidate] = i;
						thisLevel.add(candidate);
					} else
						next.add(candidate);
				}
			}
			size = 0;
			for (IntIterator iterator = next.iterator(); iterator.isValid(); iterator.advance()) {
				int current = iterator.value();
				if (!thisLevel.contains(current)) {
					level[current] = i + 1;
					queue[size++] = current;
				}
			}
		}
		out.printLine(level[source]);
    }
}

class InputReader {

	private InputStream stream;
	private byte[] buf = new byte[1024];
	private int curChar;
	private int numChars;
	private SpaceCharFilter filter;

	public InputReader(InputStream stream) {
		this.stream = stream;
	}

	public int read() {
		if (numChars == -1)
			throw new InputMismatchException();
		if (curChar >= numChars) {
			curChar = 0;
			try {
				numChars = stream.read(buf);
			} catch (IOException e) {
				throw new InputMismatchException();
			}
			if (numChars <= 0)
				return -1;
		}
		return buf[curChar++];
	}

	public int readInt() {
		int c = read();
		while (isSpaceChar(c))
			c = read();
		int sgn = 1;
		if (c == '-') {
			sgn = -1;
			c = read();
		}
		int res = 0;
		do {
			if (c < '0' || c > '9')
				throw new InputMismatchException();
			res *= 10;
			res += c - '0';
			c = read();
		} while (!isSpaceChar(c));
		return res * sgn;
	}

	public boolean isSpaceChar(int c) {
		if (filter != null)
			return filter.isSpaceChar(c);
		return isWhitespace(c);
	}

	public static boolean isWhitespace(int c) {
		return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
	}

	public interface SpaceCharFilter {
		public boolean isSpaceChar(int ch);
	}
}

class OutputWriter {
	private final PrintWriter writer;

	public OutputWriter(OutputStream outputStream) {
		writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
	}

	public OutputWriter(Writer writer) {
		this.writer = new PrintWriter(writer);
	}

	public void print(Object...objects) {
		for (int i = 0; i < objects.length; i++) {
			if (i != 0)
				writer.print(' ');
			writer.print(objects[i]);
		}
	}

    public void printLine(Object...objects) {
		print(objects);
		writer.println();
	}

	public void close() {
		writer.close();
	}

	}

class IOUtils {

	public static void readIntArrays(InputReader in, int[]... arrays) {
		for (int i = 0; i < arrays[0].length; i++) {
			for (int j = 0; j < arrays.length; j++)
				arrays[j][i] = in.readInt();
		}
	}

	}

class MiscUtils {

    public static void decreaseByOne(int[]...arrays) {
		for (int[] array : arrays) {
			for (int i = 0; i < array.length; i++)
				array[i]--;
		}
	}

	}

class Graph {
	public static final int REMOVED_BIT = 0;

	protected int vertexCount;
	protected int edgeCount;

	private int[] firstOutbound;
	private int[] firstInbound;

	private Edge[] edges;
	private int[] nextInbound;
	private int[] nextOutbound;
	private int[] from;
	private int[] to;
	private long[] weight;
	private long[] capacity;
	private int[] reverseEdge;
	private int[] flags;

	public Graph(int vertexCount) {
		this(vertexCount, vertexCount);
	}

	public Graph(int vertexCount, int edgeCapacity) {
		this.vertexCount = vertexCount;
		firstOutbound = new int[vertexCount];
		Arrays.fill(firstOutbound, -1);

		from = new int[edgeCapacity];
		to = new int[edgeCapacity];
		nextOutbound = new int[edgeCapacity];
		flags = new int[edgeCapacity];
	}

	public static Graph createGraph(int vertexCount, int[] from, int[] to) {
		Graph graph = new Graph(vertexCount, from.length);
		for (int i = 0; i < from.length; i++)
			graph.addSimpleEdge(from[i], to[i]);
		return graph;
	}

	public int addEdge(int fromID, int toID, long weight, long capacity, int reverseEdge) {
		ensureEdgeCapacity(edgeCount + 1);
		if (firstOutbound[fromID] != -1)
			nextOutbound[edgeCount] = firstOutbound[fromID];
		else
			nextOutbound[edgeCount] = -1;
		firstOutbound[fromID] = edgeCount;
		if (firstInbound != null) {
			if (firstInbound[toID] != -1)
				nextInbound[edgeCount] = firstInbound[toID];
			else
				nextInbound[edgeCount] = -1;
			firstInbound[toID] = edgeCount;
		}
		this.from[edgeCount] = fromID;
		this.to[edgeCount] = toID;
		if (capacity != 0) {
			if (this.capacity == null)
				this.capacity = new long[from.length];
			this.capacity[edgeCount] = capacity;
		}
		if (weight != 0) {
			if (this.weight == null)
				this.weight = new long[from.length];
			this.weight[edgeCount] = weight;
		}
		if (reverseEdge != -1) {
			if (this.reverseEdge == null) {
				this.reverseEdge = new int[from.length];
				Arrays.fill(this.reverseEdge, 0, edgeCount, -1);
			}
			this.reverseEdge[edgeCount] = reverseEdge;
		}
		if (edges != null)
			edges[edgeCount] = createEdge(edgeCount);
		return edgeCount++;
	}

	protected final GraphEdge createEdge(int id) {
		return new GraphEdge(id);
	}

	public final int addFlowWeightedEdge(int from, int to, long weight, long capacity) {
		if (capacity == 0) {
			return addEdge(from, to, weight, 0, -1);
		} else {
			int lastEdgeCount = edgeCount;
			addEdge(to, from, -weight, 0, lastEdgeCount + entriesPerEdge());
			return addEdge(from, to, weight, capacity, lastEdgeCount);
		}
	}

	protected int entriesPerEdge() {
		return 1;
	}

	public final int addWeightedEdge(int from, int to, long weight) {
		return addFlowWeightedEdge(from, to, weight, 0);
	}

	public final int addSimpleEdge(int from, int to) {
		return addWeightedEdge(from, to, 0);
	}

	public final int firstOutbound(int vertex) {
		int id = firstOutbound[vertex];
		while (id != -1 && isRemoved(id))
			id = nextOutbound[id];
		return id;
	}

	public final int nextOutbound(int id) {
		id = nextOutbound[id];
		while (id != -1 && isRemoved(id))
			id = nextOutbound[id];
		return id;
	}

	public final int destination(int id) {
		return to[id];
	}

	public final boolean flag(int id, int bit) {
		return (flags[id] >> bit & 1) != 0;
	}

	public final boolean isRemoved(int id) {
		return flag(id, REMOVED_BIT);
	}

	protected void ensureEdgeCapacity(int size) {
		if (from.length < size) {
			int newSize = Math.max(size, 2 * from.length);
			if (edges != null)
				edges = resize(edges, newSize);
			from = resize(from, newSize);
			to = resize(to, newSize);
			nextOutbound = resize(nextOutbound, newSize);
			if (nextInbound != null)
				nextInbound = resize(nextInbound, newSize);
			if (weight != null)
				weight = resize(weight, newSize);
			if (capacity != null)
				capacity = resize(capacity, newSize);
			if (reverseEdge != null)
				reverseEdge = resize(reverseEdge, newSize);
			flags = resize(flags, newSize);
		}
	}

	protected final int[] resize(int[] array, int size) {
		int[] newArray = new int[size];
		System.arraycopy(array, 0, newArray, 0, array.length);
		return newArray;
	}

	private long[] resize(long[] array, int size) {
		long[] newArray = new long[size];
		System.arraycopy(array, 0, newArray, 0, array.length);
		return newArray;
	}

	private Edge[] resize(Edge[] array, int size) {
		Edge[] newArray = new Edge[size];
		System.arraycopy(array, 0, newArray, 0, array.length);
		return newArray;
	}

	protected class GraphEdge implements Edge {
		protected int id;

		protected GraphEdge(int id) {
			this.id = id;
		}

		}

	}

abstract class IntSet extends IntCollection {
	}

class IntHashSet extends IntSet {
	private static final Random RND = new Random();
	private static final int[] SHIFTS = new int[4];
	private static final byte PRESENT_MASK = 1;

	static {
		for (int i = 0; i < 4; i++)
			SHIFTS[i] = RND.nextInt(31) + 1;
	}

	private int size;
	private int[] values;
	private byte[] present;
	private int step;
	private int ratio;

	public IntHashSet() {
		this(3);
	}

	public IntHashSet(int capacity) {
		capacity = Math.max(capacity, 1);
		values = new int[capacity];
		present = new byte[capacity];
		ratio = capacity;
		initStep(capacity);
	}

	private void initStep(int capacity) {
		step = RND.nextInt(capacity - 2) + 1;
		while (IntegerUtils.gcd(step, capacity) != 1)
			step++;
	}

	public IntIterator iterator() {
		return new IntIterator() {
			private int position = size == 0 ? values.length : -1;

			public int value() throws NoSuchElementException {
				if (position == -1)
					advance();
				if (position >= values.length)
					throw new NoSuchElementException();
				return values[position];
			}

			public void advance() throws NoSuchElementException {
				if (position >= values.length)
					throw new NoSuchElementException();
				position++;
				while (position < values.length && (present[position] & PRESENT_MASK) == 0)
					position++;
			}

			public boolean isValid() {
				return position < values.length;
			}
		};
	}

	public void add(int value) {
		ensureCapacity((size + 1) * ratio);
		int current = getHash(value);
		while ((present[current] & PRESENT_MASK) != 0) {
			if (values[current] == value)
				return;
			current += step;
			if (current >= values.length)
				current -= values.length;
		}
		present[current] = PRESENT_MASK;
		values[current] = value;
		size++;
	}

	private int getHash(int value) {
		int result = value;
		for (int i : SHIFTS)
			result ^= value >> i;
		result %= values.length;
		if (result < 0)
			result += values.length;
		return result;
	}

	private void ensureCapacity(int capacity) {
		if (values.length < capacity) {
			capacity = Math.max(capacity, 2 * values.length);
			rebuild(capacity);
		}
	}

	private void rebuild(int capacity) {
		initStep(capacity);
		int[] oldValues = values;
		byte[] oldPresent = present;
		values = new int[capacity];
		present = new byte[capacity];
		size = 0;
		for (int i = 0; i < oldValues.length; i++) {
			if ((oldPresent[i] & PRESENT_MASK) == PRESENT_MASK)
				add(oldValues[i]);
		}
	}

	public boolean contains(int value) {
		int current = getHash(value);
		while (present[current] != 0) {
			if (values[current] == value && (present[current] & PRESENT_MASK) != 0)
				return true;
			current += step;
			if (current >= values.length)
				current -= values.length;
		}
		return false;
	}

}

abstract class IntCollection {
	public abstract IntIterator iterator();
	public abstract void add(int value);

	public boolean contains(int value) {
		for (IntIterator iterator = iterator(); iterator.isValid(); iterator.advance()) {
			if (iterator.value() == value)
				return true;
		}
		return false;
	}

	}

interface IntIterator {
	public int value() throws NoSuchElementException;
	/*
	 * @throws NoSuchElementException only if iterator already invalid
	 */
	public void advance() throws NoSuchElementException;
	public boolean isValid();
}

interface Edge {
	}

class IntegerUtils {

    public static int gcd(int a, int b) {
        a = Math.abs(a);
        b = Math.abs(b);
        while (b != 0) {
            int temp = a % b;
            a = b;
            b = temp;
        }
        return a;
    }

    }


import java.io.InputStreamReader;
import java.io.IOException;
import java.util.Arrays;
import java.io.BufferedReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.StringTokenizer;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 */
public class Main {
	public static void main(String[] args) {
		InputStream inputStream = System.in;
		OutputStream outputStream = System.out;
		InputReader in = new InputReader(inputStream);
		PrintWriter out = new PrintWriter(outputStream);
		TaskD solver = new TaskD();
		solver.solve(1, in, out);
		out.close();
	}
}

class TaskD {
    public void solve(int testNumber, InputReader in, PrintWriter out) {
        int n = in.nextInt();
        int m = in.nextInt();
        int[] src = new int[m + 10];
        int[] next = new int[m + 10];
        int[] firstIncoming = new int[n + 10];
        int[] numOutgoing = new int[n + 10];
        int[] answer = new int[n + 10];
        Arrays.fill(answer, -1);
        Arrays.fill(firstIncoming, -1);
        for (int i = 0; i < m; ++i) {
            int u = in.nextInt() - 1;
            int v = in.nextInt() - 1;
            ++numOutgoing[u];
            next[i] = firstIncoming[v];
            src[i] = u;
            firstIncoming[v] = i;
        }
        int s = in.nextInt() - 1;
        int t = in.nextInt() - 1;
        int[] ready = new int[n + 10];
        int numReady = 0;
        int[] cur = new int[n + 10];
        int numCur = 1;
        cur[0] = t;
        answer[t] = 0;
        int[] nxt = new int[n + 10];
        while (numCur > 0) {
            for (int i = 0; i < numCur; ++i) {
                int tmp = firstIncoming[cur[i]];
                while (tmp >= 0) {
                    if (answer[src[tmp]] < 0) {
                        --numOutgoing[src[tmp]];
                        if (numOutgoing[src[tmp]] == 0) {
                            ready[numReady++] = src[tmp];
                        }
                    }
                    tmp = next[tmp];
                }
            }
            while (numReady > 0) {
                cur[numCur++] = ready[--numReady];
                answer[cur[numCur - 1]] = answer[cur[0]];
                int tmp = firstIncoming[cur[numCur - 1]];
                while (tmp >= 0) {
                    if (answer[src[tmp]] < 0) {
                        --numOutgoing[src[tmp]];
                        if (numOutgoing[src[tmp]] == 0) {
                            ready[numReady++] = src[tmp];
                        }
                    }
                    tmp = next[tmp];
                }
            }
            int numNxt = 0;
            for (int i = 0; i < numCur; ++i) {
                int tmp = firstIncoming[cur[i]];
                while (tmp >= 0) {
                    if (answer[src[tmp]] < 0) {
                        answer[src[tmp]] = answer[cur[0]] + 1;
                        nxt[numNxt++] = src[tmp];
                    }
                    tmp = next[tmp];
                }
            }
            int[] ta = cur;
            cur = nxt;
            nxt = ta;
            numCur = numNxt;
        }
        out.println(answer[s]);
    }
}

class InputReader {
    public BufferedReader reader;
    public StringTokenizer tokenizer;

    public InputReader(InputStream stream) {
        reader = new BufferedReader(new InputStreamReader(stream));
        tokenizer = null;
    }

    public String next() {
        while (tokenizer == null || !tokenizer.hasMoreTokens()) {
            try {
                tokenizer = new StringTokenizer(reader.readLine());
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return tokenizer.nextToken();
    }

    public int nextInt() {
        return Integer.parseInt(next());
    }

    }


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define For(i,j,k) for(int i=(int)(j);i<=(int)(k);i++)
#define Rep(i,j,k) for(int i=(int)(j);i>=(int)(k);i--)
inline ll read(){
	ll x=0;char ch=getchar();bool f=0;
	for(;!isdigit(ch);ch=getchar()) if(ch=='-') f=1;
	for(;isdigit(ch);ch=getchar()) x=x*10+ch-'0';
	return f?-x:x;
}
void write(ll x){
	if(x<0) putchar('-'),x=-x;
	if(x>=10) write(x/10);putchar(x%10+'0');
}
void writeln(ll x){write(x);puts("");}
void writep(ll x){write(x);putchar(' ');}
const int N=1e6+5,inf=1<<30;
int n,m,S,T,f[N],head[N],opt,in[N];
struct info{int to,nxt;}e[N];
void add(int x,int y){
	in[y]++;
	e[++opt]=(info){y,head[x]};head[x]=opt;
}
deque<int> q;
bool used[N];
void bfs(){
	For(i,1,n) f[i]=-1;
	q.push_back(T);f[T]=0;
	while(q.size()){
		int u=q.front();q.pop_front();
		if(used[u]) continue;
		used[u]=1;
		for(int i=head[u];i;i=e[i].nxt){
			int k=e[i].to;
			in[k]--;
			if(!in[k]){
				if(f[k]==-1||f[k]>f[u]) f[k]=f[u],q.push_front(k);
			}else if(f[k]==-1) f[k]=f[u]+1,q.push_back(k);
		}
	}
}
int main(){
	n=read();m=read();
	For(i,1,m){
		int x=read(),y=read();
		add(y,x);
	}
	S=read();T=read();
	bfs();
	if(f[S]==inf) f[S]=-1;
	cout<<f[S];
}
/*
4 6
1 2
2 1
1 3
3 1
2 4
3 4
1 4

*///hello


//In The Name of Beauty
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<ll> vll;
#define IB std::ios::sync_with_stdio(0);
#define pb(x) push_back(x);
#define mp(x,y) make_pair(x,y)
#define pll pair<ll,ll>
#define F first
#define S second
ll const MAXN = 1e6 + 8;
ll const INF  = 1e12 + 8;
ll const delta = 1000000007;
vll G[MAXN];
ll dp[MAXN], deg[MAXN];
bool mark[MAXN];
int main()
{
    IB;
    cin.tie(0);
    cout.tie(0);
    ll n , m , s, t;
    cin >> n >> m;
    ll ff, ss;
    for(ll i = 0;i < m;i++){
        cin >> ff >> ss;
        G[ss].pb(ff);
        deg[ff]++;
    }
    for(ll i = 1;i <= n;i++)dp[i] = INF;
    cin >> s >> t;
    dp[t] = 0;
    deque<ll> dq;
    dq.pb(t);
    while(dq.size()){
        ll v = dq.front();
        dq.pop_front();
        if(mark[v])continue;
        mark[v] = 1;
        for(ll i = 0;i < G[v].size();i++){
            ll u = G[v][i];
            deg[u]--;
            if(!deg[u] && dp[u] > dp[v]){
                dp[u] = dp[v];
                dq.push_front(u);
            }
            else if(dp[u] > dp[v] + 1){
                dp[u] = dp[v] + 1;
                dq.pb(u);
            }
        }
    }
    if(dp[s] == INF)return cout << -1,0;
    cout << dp[s];
    return 0;
}
//Written by M_H_H_7


#include <bits/stdc++.h>
#define inf 10000005

using namespace std;

int wych[1000005], odl[1000005];
queue < int > Q[1000001];

vector < vector < int > > E, ER;

int main()
{
    ios_base::sync_with_stdio(0); cin.tie(0);
    for(int i=0; i<1000001; i++)
        odl[i] = inf;
    int n, m;
    cin>>n>>m;
    E.resize(n+1);
    ER.resize(n+1);
    while(m--)
    {
        int a, b;
        cin>>a>>b;
        E[a].push_back(b);
        wych[a]++;
        ER[b].push_back(a);
    }
    int p, k, akt = 0;
    cin>>p>>k;
    Q[0].push(k);
    while(odl[p] > 1000001)
    {
        while(!Q[akt].empty())
        {
            int aktv = Q[akt].front();
            Q[akt].pop();
            if (odl[aktv] < akt)
                continue;
            for(auto& sas: ER[aktv])
            {
                wych[sas]--;
                if (wych[sas]==0)
                {
                    odl[sas] = akt;
                    Q[akt].push(sas);
                }
                if (odl[sas] > akt+1)
                {
                    odl[sas] = akt+1;
                    Q[akt+1].push(sas);
                }
            }
        }
        akt++;
    }
    if (odl[p] < inf)
        cout<<odl[p];
    else
        cout<<"-1";
}


#include <bits/stdc++.h>
#define inf 10000005

using namespace std;

int wych[1000005], odl[1000005];
queue < int > Q[2];

vector < vector < int > > E, ER;

int main()
{
  //  ios_base::sync_with_stdio(0); cin.tie(0);
    for(int i=0; i<1000001; i++)
        odl[i] = inf;
    int n, m;
    scanf("%d%d", &n, &m);
  //  cin>>n>>m;
    E.resize(n+1);
    ER.resize(n+1);
    while(m--)
    {
        int a, b;
        scanf("%d%d", &a, &b);
       // cin>>a>>b;
        E[a].push_back(b);
        wych[a]++;
        ER[b].push_back(a);
    }
    int p, k, akt = 0;
    scanf("%d%d", &p, &k);
 //   cin>>p>>k;
    Q[0].push(k);
    while(odl[p] > 1000001 && (!Q[0].empty() || !Q[1].empty()))
    {
        queue < int > &Q1 = Q[akt&1], &Q2 = Q[akt^1];
        while(!Q2.empty())
            Q2.pop();
        while(!Q1.empty())
        {
            int aktv = Q1.front();
            Q[akt].pop();
            if (odl[aktv] < akt)
                continue;
            for(auto& sas: ER[aktv])
            {
                wych[sas]--;
                if (wych[sas]==0)
                {
                    odl[sas] = akt;
                    Q1.push(sas);
                }
                if (odl[sas] > akt+1)
                {
                    odl[sas] = akt+1;
                    Q2.push(sas);
                }
            }
        }
        akt++;
    }
    if (odl[p] < inf)
        printf("%d", odl[p]);
     //   cout<<odl[p];
    else
        printf("-1");
    //    cout<<"-1";
}


#include <bits/stdc++.h>
#define inf 10000005

using namespace std;

int wych[1000005], odl[1000005];
queue < int > Q[2];

vector < vector < int > > E, ER;

int main()
{
    ios_base::sync_with_stdio(0); cin.tie(0);
    for(int i=0; i<1000001; i++)
        odl[i] = inf;
    int n, m;
    cin>>n>>m;
    E.resize(n+1);
    ER.resize(n+1);
    while(m--)
    {
        int a, b;
        cin>>a>>b;
        E[a].push_back(b);
        wych[a]++;
        ER[b].push_back(a);
    }
    int p, k, akt = 0;
    cin>>p>>k;
    Q[0].push(k);
    while(odl[p] > 1000001 && akt < n+1)
    {
        queue < int > &Q1 = Q[akt&1], &Q2 = Q[akt^1];
        while(!Q2.empty())
            Q2.pop();
        while(!Q1.empty())
        {
            int aktv = Q1.front();
            Q[akt].pop();
            if (odl[aktv] < akt)
                continue;
            for(auto& sas: ER[aktv])
            {
                wych[sas]--;
                if (wych[sas]==0)
                {
                    odl[sas] = akt;
                    Q1.push(sas);
                }
                if (odl[sas] > akt+1)
                {
                    odl[sas] = akt+1;
                    Q2.push(sas);
                }
            }
        }
        akt++;
    }
    if (odl[p] < inf)
        cout<<odl[p];
    else
        cout<<"-1";
}


#include <bits/stdc++.h>
#define inf 10000005

using namespace std;

int wych[1000005], odl[1000005];
queue < int > Q[2];

vector < vector < int > > E, ER;

int main()
{
    for(int i=0; i<1000001; i++)
        odl[i] = inf;
    int n, m;
    scanf("%d%d", &n, &m);
  //  cin>>n>>m;
    E.resize(n+1);
    ER.resize(n+1);
    while(m--)
    {
        int a, b;
        scanf("%d%d", &a, &b);
        E[a].push_back(b);
        wych[a]++;
        ER[b].push_back(a);
    }
    int p, k, akt = 0;
    scanf("%d%d", &p, &k);
    Q[0].push(k);
    odl[k] = 0;
    while(odl[p] > 1000001 && (!Q[0].empty() || !Q[1].empty()))
    {
        queue < int > &Q1 = Q[akt&1], &Q2 = Q[akt^1];
        while(!Q2.empty())
            Q2.pop();
        while(!Q1.empty())
        {
            int aktv = Q1.front();
            Q[akt].pop();
            if (odl[aktv] < akt)
                continue;
            for(auto& sas: ER[aktv])
            {
                wych[sas]--;
                if (wych[sas]==0)
                {
                    odl[sas] = akt;
                    Q1.push(sas);
                }
                if (odl[sas] > akt+1)
                {
                    odl[sas] = akt+1;
                    Q2.push(sas);
                }
            }
        }
        akt++;
    }
    if (odl[p] < inf)
        printf("%d", odl[p]);
    else
        printf("-1");
}


#include <bits/stdc++.h>
#define inf 10000005

using namespace std;

int wych[1000005], odl[1000005];
queue < int > Q[2];

vector < vector < int > > E, ER;

int main()
{
    for(int i=0; i<1000001; i++)
        odl[i] = inf;
    int n, m, mm;
    scanf("%d%d", &n, &m);
    mm = m;
  //  cin>>n>>m;
    E.resize(n+1);
    ER.resize(n+1);
    while(m--)
    {
        int a, b;
        scanf("%d%d", &a, &b);
        E[a].push_back(b);
        wych[a]++;
        ER[b].push_back(a);
    }
    int p, k, akt = 0;
    scanf("%d%d", &p, &k);
    Q[0].push(k);
    odl[k] = 0;
    int a=0, b=0, c=0;
    while(odl[p] > 1000001 && akt <= 1000000 && (!Q[0].empty() || !Q[1].empty()))
    {
        queue < int > &Q1 = Q[akt&1], &Q2 = Q[1 - (akt&1)];
        while(!Q2.empty())
            Q2.pop();
        while(!Q1.empty())
        {
            b++;
            if (b > 2*n)
                return -2;
            int aktv = Q1.front();
            Q[akt].pop();
            if (odl[aktv] < akt)
                continue;
            for(auto& sas: ER[aktv])
            {
                c++;
                wych[sas]--;
                if (wych[sas]==0 && odl[sas] > akt)
                {
                    odl[sas] = akt;
                    Q1.push(sas);
                }
                if (odl[sas] > akt+1)
                {
                    odl[sas] = akt+1;
                    Q2.push(sas);
                }
            }
        }
        akt++;
    }
    if (odl[p] < inf)
        printf("%d", odl[p]);
    else
        printf("-1");
}


#include <bits/stdc++.h>
#define inf 10000005

using namespace std;

int wych[1000005], odl[1000005];
queue < int > Q[2];

vector < vector < int > > E, ER;

int main()
{
    for(int i=0; i<1000001; i++)
        odl[i] = inf;
    int n, m, mm;
    scanf("%d%d", &n, &m);
    mm = m;
  //  cin>>n>>m;
    E.resize(n+1);
    ER.resize(n+1);
    while(m--)
    {
        int a, b;
        scanf("%d%d", &a, &b);
        E[a].push_back(b);
        wych[a]++;
        ER[b].push_back(a);
    }
    int p, k, akt = 0;
    scanf("%d%d", &p, &k);
    Q[0].push(k);
    odl[k] = 0;
    int a=0, b=0, c=0;
    while(odl[p] > 1000001 && akt <= 1000000 && (!Q[0].empty() || !Q[1].empty()))
    {
        queue < int > &Q1 = Q[akt&1], &Q2 = Q[1 - (akt&1)];
        while(!Q2.empty())
            Q2.pop();
        while(!Q1.empty())
        {
            b++;
            if (b > 2*n)
                return -2;
            int aktv = Q1.front();
            Q[akt].pop();
            if (odl[aktv] < akt)
                continue;
            for(auto& sas: ER[aktv])
            {
                c++;
                if (c > 2*mm)
                    return -1;
                wych[sas]--;
                if (wych[sas]==0 && odl[sas] > akt)
                {
                    odl[sas] = akt;
                    Q1.push(sas);
                }
                if (odl[sas] > akt+1)
                {
                    odl[sas] = akt+1;
                    Q2.push(sas);
                }
            }
        }
        akt++;
    }
    if (odl[p] < inf)
        printf("%d", odl[p]);
    else
        printf("-1");
}