#include <iostream>
#include <bits/stdc++.h>
using namespace std;
/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int mini;

void dodaj(vector<pair<int, int> > v[], int a, int b, int t){
    v[a].push_back(make_pair(b, t));
    v[b].push_back(make_pair(a, t));
}

void konacan(vector<int>& p, vector<pair<int, int> > v[]){
    int n = p.size(), j, f=0, br=0;
    for(int i=0;i<n-1;i++){
    	for(j=0;j<v[i].size();j++){
    		if(v[p[i]][j].first==p[i+1]){
    			break;
			}
		}
		int m=v[p[i]][j].second;
		if(f==0 and m==1){
			br++;
			f=1;
		}
		if(f==1 and m==0){
			br++;
			f=0;
		}
	}
	if(br<mini){
		mini=br;
	}
}

bool nijePosecen(int x, vector<int> p){
    int n = p.size();
    for(int i=0;i<n;i++){
    	if(p[i] == x){
    		return false;
		}
            
	}
    return true;
}

void prolazak(vector<pair<int,int> > tren[], int poc, int kraj, int v){
    queue<vector<int> > q;
    vector<int> put, stanje;
    //stanje.push_back(0);
    put.push_back(poc);
    q.push(put);
    while(!q.empty()){
        put=q.front();
        //stanje=
        q.pop();
        int pos=put[put.size()-1];
        if(pos == kraj){
        	konacan(put,tren);
		}
        for(int i=0;i<tren[pos].size();i++){
            if(nijePosecen(tren[pos][i].first, put)){
                vector<int> novi(put);
                novi.push_back(tren[pos][i].first);
                q.push(novi);
            }
        }
    }
}

int main(int argc, char** argv) {
	int n, m, a, b, t, x, y;
	ios_base::sync_with_stdio(false);
	mini=INT_MAX;
	cin>>n>>m;
	vector<pair<int, int> > v[100];
	for(int i=0;i<m;i++){
		cin>>a>>b>>t;
		dodaj(v, a-1, b-1, t);
	}
	cin>>x>>y;
	prolazak(v,x-1,y-1,n);
	cout<<mini;
	return 0;
}