#include <algorithm>
#include <cmath>
#include <vector>
#include <queue>
#include <iostream>
using namespace std;



struct  Path
{
	int A;
	int B;
	bool ws;
};
int n, p;
vector <Path> paths;
int Mik, Laz;
vector <bool> visited;
vector <int> dist;
void Pathing(int ci,int cw,bool ds)
{
	for (int i = 0; i < p; i++)
	{
		
		if(paths[i].A == ci && paths[i].B == ci)continue;
		if (paths[i].A == paths[i].B)continue;
		if (paths[i].A == ci && paths[i].B != ci)
		{
			if (paths[i].B == Laz)
			{
				if (ds != paths[i].ws)
				{
					cw++;
					ds = !ds;
				}
				if (cw < dist[Laz])dist[Laz] = cw;
			}
			else if (!visited[paths[i].B])
			{
				visited[paths[i].B] = true;
				Path np = paths[i];
				if (ds != np.ws)
				{
					cw++;
					ds = !ds;
				}
				dist[np.B] = cw;
				Pathing(np.B, cw,ds);
			}
			else continue;
			
		}
		else if (paths[i].B == ci && paths[i].A != ci)
		{
			if (paths[i].A == Laz)
			{
				if (ds != paths[i].ws)
				{
					cw++;
					ds = !ds;
				}
				if (cw + 1 < dist[Laz])dist[Laz] = cw;
			}
			if (!visited[paths[i].A])
			{
				visited[paths[i].A] = true;
				Path np = paths[i];
				if (ds != np.ws)
				{
					cw++;
					ds = !ds;
				}
				dist[np.A] = cw;
				Pathing(np.A, cw,ds);
			}
			else continue;

		}
	}
}

void printArr(vector <int> dist, int n)
{
	printf("Vertex   Distance from Source\n");
	for (int i = 1; i < n; ++i)
		printf("%d \t\t %d\n", i, dist[i]);
}

void Sante()
{
	
	cin >> n >> p;
	n++;
	vector <bool> r(n, false);
	visited = r;
	vector <int> e(n, INT_MAX);
	dist = e;
	for (int i = 0; i < p; i++)
	{
		Path a;
		int y, z;
		bool e;
		cin >> y >> z >> e;
		a.A = y;
		a.B = z;
		a.ws = e;
		paths.push_back(a);
	}
	
	cin >> Mik >> Laz;
	dist[Mik] = 0;
	visited[Mik] = true;
	Pathing(Mik,0,false);
	cout << dist[Laz];
}



int main()
{
	Sante();
	return 69420;
}