#include<bits/stdc++.h>
using namespace std;
vector<pair<int,int>> v[200001];
int d[200001];
void dfs(int index,int status,int di)
{
    for(int i=0;i<v[index].size();i++)
    {
        if(v[index][i].second==status)
        {
            if(d[v[index][i].first]>di)
            {   d[v[index][i].first]=di;
                dfs(v[index][i].first,status,di);
            }
        }
        else
        {
            if(d[v[index][i].first]>di)
            {
                d[v[index][i].first]=di+1;
                dfs(v[index][i].first,v[index][i].second,di+1);
            }
        }
    }
}
int main()
{
    int n,m;
     cin >> n >> m;
     for(int i=0;i<=n;i++)
     {
         d[i]=200002;
     }
     int a,b,t;
     for(int i=0;i<m;i++)
     {
         cin >> a >> b >>t;
         v[a].push_back(make_pair(b,t));
         v[b].push_back(make_pair(a,t));
     }
     int mi,l;
     cin >> mi >> l;
     dfs(mi,0,0);
     cout << d[l];
}