#include <bits/stdc++.h>
using namespace std;

const int MAXN = 200002;
const int INF = 200000000;
vector<pair<int, bool>> g[MAXN];
bool visited[MAXN];
bool state[MAXN];
int dist[MAXN];

int main(int argc, char **argv) {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);

	int n, m;
	cin >> n >> m;
	for(int i = 0; i < m; i++) {
		int a, b, c;
		cin >> a >> b >> c;
		g[a].push_back({b, c == 1});
		g[b].push_back({a, c == 1});
	}
	for(int i = 0; i < MAXN; i++){
		dist[i] = INF;
	}

	int s, e;
	cin >> s >> e;

	queue<int> q;

	q.push(s);
	visited[s] = true;
	dist[s] = 0;

	while (!q.empty()) {
		int v = q.front();
		q.pop();
		for (auto p : g[v]) {
			int u = p.first;
			if(visited[u]) continue; 
			int d = dist[v] + ((int)(state[v] != p.second));
			//cout << v << " " << u << " " << d << endl;
			q.push(u);
			visited[u] = true;
			if(dist[u] > d){
				dist[u] = d;
				state[u] = p.second;
			}

		}
	}
	//for(int i = 0; i < 10; i++) {
	//	cout << state[i] << " ";
	//}
	//cout << endl;
	//for(int i = 0; i < 10; i++) {
	//	cout << dist[i] << " ";
	//}	
	//cout << endl;

	cout << dist[e] << endl;

	return 0;
}