// sante.cpp : This file contains the 'main' function. Program execution begins and ends there.
//

#include <iostream>
#include <list>
#include <iterator>
using namespace std;

list<int>::iterator  getilist(list<int> l, int index)
{

    list<int>::iterator it;
    it = l.begin();
    for (int i = 0; i < index; i++)
    {
        // cout << endl;
      //   cout << index << endl;
      //   cout << *it<<endl;
        it++;
    }
    return it;
}
int main()
{
    int n;
    cin >> n;
    int nv;
    cin >> nv;
    
    
    int** arr = new int* [nv];
    for (int i = 0; i < nv; i++)
        arr[i] = new int[3];

    for (int i = 0; i < nv; i++)
    {
        int b, c, d;

        cin >> b;
        cin >> c;
        cin >> d;
        arr[i][0] = b-1;
        arr[i][1] = c-1;
        arr[i][2] = d;
    }
    int** sante = new int* [n];
    for (int i = 0; i < n; i++)
        sante[i] = new int[2];
    
    int p;
    int kr;
    cin >> p;
    cin >> kr;
    
    for (int i = 0; i < n; i++)
    {
        if (i + 1 == p)
        {
            sante[i][0] = 0;
            sante[i][1] = 0;//na pocetku ugasena
        }
        else
        {
            sante[i][0] = -1;
            sante[i][1] = -1;
        }

    }
    
    for (int i = 0; i < nv; i++)
    {
        for (int j = 0; j < nv; j++)
        {
           // cout << "Pocetna Santa: " << arr[j][0] << endl;
          //   cout << "pod: " << sante[arr[j][0]][0] << " "<< sante[arr[j][0]][0] << endl;
         //   cout << "Krajnja Santa: " << arr[j][1] << endl;
        //    cout << "pod: " << sante[arr[j][1]][0] << " " << sante[arr[j][1]][0] << endl;
            
            if(sante[arr[j][0]][0] != -1)//ako nije ne nadjen nacin da se dodje do nje
            {
                


                if (sante[arr[j][1]][1] == -1)//ako se prvi put susrecemo  sa santom
                {

                   // cout << "PRCU PUT" << endl;
                    sante[arr[j][1]][1] = arr[j][2];//stanje kad smo dosli do nje je isto kao ono potrebno da se predje put
                    if (sante[arr[j][1]][1] == sante[arr[j][0]][1])//ako je stanje potrebno isto kao i stanje sadasnje
                    {
                        sante[arr[j][1]][0] = sante[arr[j][0]][0];//
                    }
                    else
                    {
                        sante[arr[j][1]][0] = sante[arr[j][0]][0] + 1;
                    }

                }
                else
                {
                    if (sante[arr[j][1]][1] == sante[arr[j][0]][1])
                    {
                        if (sante[arr[j][1]][0] > sante[arr[j][0]][0])
                        {
                            sante[arr[j][1]][0] = sante[arr[j][0]][0];
                        }              
                    }
                    else
                    {
                        if (sante[arr[j][1]][0] > sante[arr[j][0]][0])
                        {
                            sante[arr[j][1]][0] = sante[arr[j][0]][0]+1;
                        }
                    }

                }
               // cout << "Promenjena krajnja Santa: " << arr[j][1] << endl;
               // cout << "pod: " << sante[arr[j][1]][0]<< " " << sante[arr[j][1]][0] << endl;
               // cout << endl;

            }
        }
    }
    cout << sante[kr-1][0]<<endl;


    for (int i = 0; i < nv; i++)
            delete[] arr[i];
        delete[] arr;
        delete[] sante;


}