#include <iostream>
#include <vector>

using namespace std;

static int broj = 0;

void dfs(int* from,int f,int* to,int t,int* weight, bool* visited,int last,int m,int br)
{
	if (f == t)
	{
		if (broj == 0) {
			broj = br;
		}
		else if(br<broj)
		{
			broj = br;
		}
	}
	else
		for (size_t i = 0; i < m; i++)
		{
			if (!visited[i] && f == from[i])
			{
				if (weight[i] != last)
					br++;
				//cout << from[i] << " " << to[i] << " "<<weight[i] << endl;

				visited[i] = true;
				dfs(from, to[i], to, t, weight, visited, weight[i], m, br);
				visited[i] = false;
				br=0;
			}
		}
}

int main()
{
	int n, m,f,t;
	cin >> n >> m;
	int* from = new int[m];
	int* to = new int[m];
	int* weight = new int[m];
	bool* visited = new bool[m];
	for (size_t i = 0; i < m; i++)
	{
		visited[i] = false;
		cin >> from[i] >> to[i] >> weight[i];
	}
	cin >> f >> t;
	dfs(from,f,to,t,weight,visited,0,m,0);
	if (broj == 0)
		broj--;
	cout << broj;
}