#include<stdio.h>
#include<iostream>
#include<math.h>
#include<vector>
#include<string>
#include<algorithm>
using namespace std;

vector<pair<int, int>> a[200001];
int v;

int solv(bool crm, vector<int> prev, int crnt) {
	if (crnt == v)return 0;
	int i, n = 0, lp = prev.size(), lc = a[crnt].size();
	bool p = 0;
	vector<int> cr;
	for (i = 0; i < lc; i++) {
		if (!binary_search(prev.begin(), prev.end(), a[crnt][i].first)) {
			p = 1;
			vector<int> prevv = prev;
			prevv.insert(upper_bound(prevv.begin(), prevv.end(), a[crnt][i].first), a[crnt][i].first);
			if ((crm == 0 && a[crnt][i].second == -1) || (crm == 1 && a[crnt][i].second == 0)) {
				crm = !crm;
				cr.push_back(1 + solv(crm, prevv, a[crnt][i].first));
			}
			else {
				cr.push_back(solv(crm, prevv, a[crnt][i].first));
			}
		}
	}
	if (!p)return -1;
	int min = 2000000;
	for (i = 0; i < cr.size(); i++) {
		if (cr[i] < min && cr[i] != -1)min = cr[i];
	}
	return min;
}

int main() {
	int n, m, p[200001][3], i, j, u;
	cin >> n >> m;
	for (i = 0; i < m; i++) {
		cin >> p[i][0] >> p[i][1] >> p[i][2];
		a[p[i][0] - 1].push_back(make_pair(p[i][1] - 1, p[i][2]));
		a[p[i][1] - 1].push_back(make_pair(p[i][0] - 1, p[i][2]));
	}
	cin >> u >> v;
	vector<int> a;
	cout << solv(0, a, u);
}