#include <iostream>
#include <vector>
#include <bits/stdc++.h>
using namespace std;

struct Jegtabla{

    int start;
    int dest;
    int path;
    int marked;
    Jegtabla(int start_, int dest_, int path_,int marked_){
        start = start_;
        dest = dest_;
        path = path_;
        marked=marked_;
    }


};


int DFS(vector<Jegtabla>& tablak,int u,int v,int steps, int swetch){

    if(u == v)
        return steps+1;

    //cout << steps << '\n';
   // cout << "new start : "<<u << '\n';
    //cout << tablak[u].start << ' ' << tablak[u].dest << '\n';
    if(tablak[u].marked == 0){
          //  cout << tablak[u].start << ' ' << tablak[u].dest << '\n';
            if(tablak[u].path != swetch){
                 steps = steps + 1;
                swetch = tablak[u].path;
            }
            //tablak[u].marked = 1;//Jegtabla(tablak[u].start,tablak[u].dest,tablak[u].path,1);
        }
    int newstart = 0;
    for(int i = 0; i < tablak.size(); i++)
    {
        if(tablak[i].start == tablak[u].dest){
            newstart = i;
            break;
        }


    }
  //  cout << "Second new start : "<<newstart <<'\n';
    if(tablak[newstart].dest == v)
        return steps+1;
    else{
         bool valid = false;
        for(int i = 0; i < tablak.size(); i++){
             if(tablak[i].start == tablak[newstart].dest){
                valid = true;
                break;
             }
        }
        if(!valid)
            tablak[u].marked = 1;

      //  cout << tablak[u].dest << '\n';
        if(valid)
            DFS(tablak, newstart, v, steps,swetch);
        else
            return 1e10;
        }

}

int main()
{
    int n,m;
    cin >>n >>m;
   // int matrix[n+1][n+1] = {0};
    int a,b,t;
    vector<Jegtabla> tablak;

    for(int i = 0; i < m; i++){

        cin >>a >>b >>t;

        tablak.push_back(Jegtabla(a,b,t,0));

    }
    int u, v;
    cin >> u >> v;

    int minnn = 1e9;
    int steps = 0;
    int swetch =0;
    int currentsteps = 0;
    for(int i = 0; i<m;i++){

        if(tablak[i].start==u)
        {
           currentsteps = DFS(tablak,i,v,steps,swetch);
        }
        if(currentsteps < minnn)
            minnn = currentsteps;

    }
    cout << minnn;
  /*  for(int i = 0; i < tablak.size(); i++){

        cout << '\n' << tablak[i].start << " " <<tablak[i].dest<<'\n';

    }*/
    return 0;
}