#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int brojac = 0, minimalanBroj = INT_MAX;
int n, m;
void f(int tr, int cilj, bool ukljuceno, vector<vector<pair<int, bool>>> susedi, vector<bool>posecen)
{
	if (tr == cilj) {
		cout << brojac << endl;
		brojac = min(brojac, minimalanBroj);
		return;
	}
	posecen[tr] = true;
	for (pair<int, bool> x : susedi[tr]) {
		if (!posecen[x.first]) {
			if (ukljuceno != x.second) {
				brojac++;
				f(x.first, cilj, !ukljuceno, susedi, posecen);
				brojac--;
			}
			else f(x.first, cilj, ukljuceno, susedi, posecen);
		}
	}
}
int main()
{
	cin >> n >> m;
	vector<vector<pair<int, bool>>> susedi(n);
	vector<bool> posecen(n);
	for (int i = 0; i < m; i++)
	{
		int a, b; cin >> a >> b; bool t; cin >> t;
		susedi[a - 1].push_back({ b - 1, t });
		susedi[b - 1].push_back({ a - 1, t });
	}
	int tr, cilj; cin >> tr >> cilj;
	f(tr-1, cilj-1, false, susedi, posecen);
}