#include <bits/stdc++.h>

using namespace std;

void optimizeio()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
}

struct Node
{
    int X = -1;
    vector<pair<int, int> > connected;
};

int n, m;
int M, L;
vector<Node> sante(200000);
vector<bool> mark(200000, false);

void bojenje(queue<int> q, int broj, int stanje)
{
    queue<int> sledeci;
    if(broj == 0) sledeci = q;
    while(!q.empty())
    {
        int t = q.front();
        q.pop();
//        cout << t << "\n";
        for(int i = 0; i < sante[t].connected.size(); i++)
        {
//            cout << sante[t].connected[i].second << " " << stanje << " " << mark[sante[t].connected[i].first] << "\n";
            if(sante[t].connected[i].second == stanje && mark[sante[t].connected[i].first] == false)
            {
//                cout << "BLA ";
                sante[sante[t].connected[i].first].X = broj;
                q.push(sante[t].connected[i].first);
                sledeci.push(sante[t].connected[i].first);
                mark[sante[t].connected[i].first] = true;
//                cout << "STA ";
            }
        }
    }

    broj++;
    if(!sledeci.empty()) bojenje(sledeci, broj, 1 - stanje);
}

int main()
{
    optimizeio();
    cin >> n >> m;
    for(int i = 0; i < m; i++)
    {
        int pov1, pov2, t;
        cin >> pov1 >> pov2 >> t;
        sante[pov1 - 1].connected.push_back(make_pair(pov2 - 1, t));
        sante[pov2 - 1].connected.push_back(make_pair(pov1 - 1, t));
    }
    cin >> M >> L;

//    for(int i = 0; i < n; i++)
//    {
//        for(int j = 0; j < sante[i].connected.size(); j++)
//        {
//            cout << i << " " << sante[i].connected[j].first << " " << sante[i].connected[j].second << "\n";
//        }
//    }
    queue<int> q;
    q.push(M - 1);
    sante[M - 1].X = 0;
    bojenje(q, 0, 0);
    cout << sante[L - 1].X << "\n";
    return 0;
}