#include <bits/stdc++.h>
#define ff(i,a,b) for(int (i) = (a); (i) < (b); ++ (i))
using namespace std;
bool visited[200007] = {false};
vector <vector<int> > adj;
void dfs(int v){
	visited[v] = true;
	for(int u = 0;u<adj[v].size();u++){
		if(!visited[adj[v][u]])
            dfs(adj[v][u]);
	}
}
void solve(){
    int n,m;cin>>n>>m;
    ff(i,0,m){
    	int f,t,y;
        cin>>f>>t>>y;
        adj[f].push_back(t);
    }
    int mika, laza;
    cin>>mika>>laza;
    bool x = false;
    ff(i,0,adj[mika].size()){
    	if(adj[mika][i] == laza)x = true;
	}
    dfs(mika);
    if(visited[laza] == false){
        cout<<-1;return;
    }else if(x){
            cout<<0;return;
    }else{
    	cout<<1;return;
	}
}
int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    solve();
}