#include "cstdio"
#include "vector"
#include "algorithm"
using namespace std;
struct path
{
    int node1,node2,difficulty;
};

bool getPaths(int goal,path paths[],int n,vector<int> avoid)
{
    for(int i = 0; i < n;i++)
    {
        if(paths[i].node2 == goal && !count(avoid.begin(),avoid.end(),paths[i].node1))
        {
            return true;
        }
    }
    return false;
}

int main()
{
    int n,m;
    scanf("%d %d",&n,&m);
    int status = 0;
    path paths[m];
    for(int i = 0; i < m;i++)
    {
        int x,y,z;
        scanf("%d %d %d",&x,&y,&z);
        path p;p.node1=x;p.node2=y;p.difficulty = z;
        paths[i] = p;
    }
    vector<int> visited;
    vector<int> avoid;
    int counter = 0;
    int u,v;
    scanf("%d %d",&u,&v);
    while(1)
    {
        bool needToChange = false;
        bool stuck = false;
        for(int i = 0;i < m;i++)
        {
            stuck = true;
            path p = paths[i];
            if(p.node1 == u && !count(visited.begin(),visited.end(),p.node2))
            {
                stuck = false;
                if(status != p.difficulty)
                {
                    needToChange = true;
                }
                else
                {
                    visited.push_back(p.node2);
                    needToChange = false;
                    u = p.node2;
                    continue;
                }
            }
            if(stuck)
            {
                while(getPaths(u,paths,m,avoid))
                {
                    u = visited.at(visited.size()-1);
                }
            }
        }
        status = needToChange == !status;
        if(needToChange) counter++;
        if(v == u) break;
    }
    printf("%d", counter);
}