#include <iostream>
using namespace std;
int*& dijkstra(int** matrica, int pocetak,int br_cvorova)
{
	int MAX =INT_MAX;
	int* res = new int[br_cvorova];
	bool* poseceni = new bool[br_cvorova];
	for (int i = 0; i < br_cvorova; i++)
	{
		res[i] = (i == pocetak) ? 0 : MAX;
		poseceni[i] = false;
	}
	int trenutni = pocetak, tr_duzina = 0, broj_preostalih = 0;
	bool Stanje = false;
	while (broj_preostalih < br_cvorova - 1)
	{
		poseceni[trenutni] = true;
		broj_preostalih++;
		for (int i = 0; i < br_cvorova; i++)
		{
			if (poseceni[i] || matrica[trenutni][i] == -1 || i == trenutni) continue;
			if (tr_duzina + abs(tr_duzina % 2 - matrica[trenutni][i]) < res[i]) res[i] = tr_duzina + abs(tr_duzina % 2 - matrica[trenutni][i]);
		}
		int sledeci = -1;
		for (int i = 0; i < br_cvorova; i++)
		{
			if (poseceni[i] || res[i] == MAX) continue;
			if (sledeci == -1) sledeci = i;
			else if (res[i] < res[sledeci]) sledeci = i;
		}
		if (sledeci == -1)
		{
			for (int i = 0; i < br_cvorova; i++) if (res[i] == MAX) res[i] = -1;
			return res;
		}
		trenutni = sledeci;
		tr_duzina = res[sledeci];
	}
	return res;
}
struct grana {
	int cvor1, cvor2, led;
};
int main()
{
	int n, g;
	cin >> n >> g;
	int** matrix = new int* [n];
	for (int i = 0; i < n; i++)matrix[i] = new int[n];
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < n; j++) {
			matrix[i][j] = (i == j) ? 0 : -1;
		}
	}
	grana tmp;
	for (int i = 0; i < g; i++)
	{
		cin >> tmp.cvor1 >> tmp.cvor2 >> tmp.led;
		matrix[tmp.cvor1 - 1][tmp.cvor2 - 1] = tmp.led;
		matrix[tmp.cvor2 - 1][tmp.cvor1] = tmp.led;
	}
	int pocetni, zavrsni;
	cin >> pocetni >> zavrsni;
	int* res = dijkstra(matrix, pocetni - 1,n);
	cout << res[zavrsni - 1];
	delete[] res;
	for (int i = 0; i < n; i++) {
		delete[] matrix[i];
	}
	delete[]matrix;
}