#include <iostream>
#include <set>
#include <map>
#include <algorithm>
#include <vector>
#include <string>
#include <deque>
#include <queue>
#include <cmath>
#include <iomanip>
#include <utility>
#define pb push_back
#define ll long long
#define yes cout<<"YES"<<endl;
#define no cout<<"NO"<<endl;
using namespace std;
// Milovan > Aleksamaster
// find_by_order-kti element skupa
// order_of_key(k)-broj elemenata striktno manji od
vector<int>adj[200005];
bool visited[200005];
int dis[200005]={0};
queue<int>q;
int ans=0;
int p[200005];
void bfs(int x){
    visited[x] = true;
    dis[x] = 0;
    q.push(x);
    while (!q.empty()) {
    int s = q.front();
    q.pop();
    for (int i=0;i<adj[s].size();i++) {
    if(visited[adj[s][i]])continue;
    visited[adj[s][i]]=1;
    dis[adj[s][i]] = dis[s]+1;
    p[adj[s][i]]=s;
    q.push(adj[s][i]);
    }
    }
}
int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	int n,m;
	cin>>n>>m;
	map<pair<int,int>,int>kec;
	for(int i=1;i<=m;i++){
		int u,v,c;
		cin>>u>>v>>c;
		adj[u].pb(v);
		adj[v].pb(u);
		kec[{u,v}]=c;
		kec[{v,u}]=c;
	}
	int l,r;
	cin>>l>>r;
	bfs(l);
	if(visited[r]==0){
		cout<<-1;
		return 0;
	}
	int u = r;
    int K = dis[r];
    vector<int> ans(K+1);
    for(int i = K; i >= 0; i--){
        ans[i] = u;
        u = p[u];
    }
    int rez=0;
    int upaljen=0;
    for(int i=0;i<K;i++){
		if(kec[{ans[i],ans[i+1]}]){
			if(upaljen)continue;
			upaljen=1;
			rez++;
		}
		else {
			if(!upaljen)continue;
			upaljen=0;
			rez++;
		}
	}
	cout<<rez;
}