#include <iostream>
#include <vector>
#include <algorithm>

struct Put {
	int a;
	int b;
	int t;
};

using namespace std;

int solve(vector<Put> putevi, int state, int dest, int current) {
	int ans = 1000000;
	for (int i = 0; i < putevi.size(); i++) {
		Put put = putevi[i];
		if (put.a != current) continue;
		if (put.b == dest) {
			return 1-(state==put.t);
		}
		vector<Put> putevi2(putevi);
		putevi2.erase(putevi2.begin() + i);
		ans = min(ans, solve(putevi2, put.t, dest, put.b)) + 1 - (state == put.t);
	}
	return ans;
}

int main() {
	int n, m;
	cin >> n >> m;
	vector<Put> putevi(m);
	for (int i = 0; i < m; i++) {
		int a, b, t;
		cin >> a >> b >> t;
		Put put;
		put.a = a;
		put.b = b;
		put.t = t;
		putevi[i] = put;
	}
	int u, v;
	cin >> u >> v;
	cout<<solve(putevi, 0, v, u);
}