#include <bits/stdc++.h>
using namespace std;
const long long MAXN = 200010;
vector<pair<long long, long long>> g[MAXN];
long long dp[MAXN];
bool visited[MAXN];
void dfs2(long long x){
	visited[x] = true;
	for(auto m : g[x]){
		if(!visited[m.first]){
			dfs2(m.first);
		}
	}
}
int main(){
	long long n, m;
	scanf("%lld %lld", &n, &m);
	for(int i = 0; i < m; i++){
		long long a, b, t;
		scanf("%lld %lld %lld", &a, &b, &t);
		g[a].push_back({b,t});
		g[b].push_back({a,t});
	}
	long long a, b;
	cin >> a >> b;
	dfs2(a);
	if(!visited[b]){
		cout << -1;
		return 0;
	}
	else
		cout << 1;
	return 0;
}