#include <bits/stdc++.h>

using namespace std;
int INF=INT_MAX;
int mika, laza;
int n, m;
vector<pair<int, int> >graff[200020];
priority_queue<pair<int, int> >red;
bool check[200020];
int dist[200020], state[200020];

void dijkstra()
{
    for(int i=1; i<=n; i++){dist[i]=INF; check[i]=false; state[i]=-1;}
    dist[mika]=0;
    red.push({0, mika});
    state[mika]=0;

    while(!red.empty())
    {
        int a=red.top().second;
        red.pop();
        if(!check[a])
        {
            check[a]=true;
            for(int j=0; j<graff[a].size(); j++)
            {
                int b=graff[a][j].first, w=graff[a][j].second;
                if(w==state[a])
                {
                    if(dist[b]>dist[a])
                    {dist[b]=dist[a]; red.push({-dist[b], b}); state[b]=w;}
                }
                else
                {
                    if(dist[b]>dist[a]+1)
                    {dist[b]=dist[a]+1; red.push({-dist[b], b}); state[b]=w;}
                }
            }
        }
    }
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    cin>>n>>m;
    for(int i=0; i<m; i++)
    {
        int a, b, c;
        cin>>a>>b>>c;
        graff[a].push_back({b, c});
        graff[b].push_back({a, c});
    }
    cin>>mika>>laza;
    dijkstra();
    if(check[laza])cout<<dist[laza];
    else cout<<-1;
    return 0;
}