#include <bits/stdc++.h>


using namespace std;





int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int N, M;
    cin >> N >> M;
    vector <vector<pair<int , int>>> Putevi(N + 1);
    int a , b , t;
    for(int i = 0; i < M; i++)
    { 
        cin >> a >> b >>t;
        Putevi[a -1].push_back(make_pair(b - 1,t));
        Putevi[b - 1].push_back(make_pair(a - 1,t));
    }
    int firste, seconde;
    cin >> firste >> seconde;
    firste--;
    seconde--;
    vector <int> MinChanges(N, 2147483640);
    vector <bool> Steps(N, false);
    vector <int> state(N, 3);

    MinChanges[firste] = 0;
    priority_queue<pair<int, int>> PRQ;
    state[firste] = 0;
    for(int i = 0; i < Putevi[firste].size(); i++)
    { 
        if(Putevi[firste][i].second == 1)
        {
            MinChanges[Putevi[firste][i].first] =min (MinChanges[firste] + 1 , MinChanges[Putevi[firste][i].first]) ;
            if(state[Putevi[firste][i].first] != 0 && state[Putevi[firste][i].first] != -1)
            { 
                state[Putevi[firste][i].first] = 1;
            }
            PRQ.push(make_pair(Putevi[firste][i].first, MinChanges[Putevi[firste][i].first]));
        }
        if(Putevi[firste][i].second == 0)
        {
            MinChanges[Putevi[firste][i].first] = min(MinChanges[firste], MinChanges[Putevi[firste][i].first]) ;
            if(state[Putevi[firste][i].first] != 0 && state[Putevi[firste][i].first] != -1)
            { 
                state[Putevi[firste][i].first] = 0;
            }
            PRQ.push(make_pair(Putevi[firste][i].first, MinChanges[Putevi[firste][i].first]));
        }
    }
    Steps[firste] = true;
    while(! PRQ.empty())
    { 

        int element = PRQ.top().first;
        int Min = PRQ.top().second;
        Steps[element] = true;
        PRQ.pop();
        for(int i = 0; i < Putevi[element].size(); i++)
        { 
            if(Steps[Putevi[element][i].first] == false)
            { 
                if(state[element] == -1 || state[element] == Putevi[element][i].second)
                { 
                    int MinForI = Min;
                    if(MinChanges[Putevi[element][i].first] > MinForI)
                    { 
                        MinChanges[Putevi[element][i].first] = MinForI;
                    }
                    else if(MinChanges[Putevi[element][i].first] == MinForI)
                    {   
                        if(state[Putevi[element][i].first] != Putevi[element][i].second)
                            state[Putevi[element][i].first] = -1;
                    }
                }
                else 
                { 
                    int MinForI = Min + 1;
                    if(MinChanges[Putevi[element][i].first] > MinForI)
                    { 
                        MinChanges[Putevi[element][i].first] = MinForI;
                        state[Putevi[element][i].first] = Putevi[element][i].second;
                    }
                    else if(MinChanges[Putevi[element][i].first] == MinForI)
                    {   
                        if(state[Putevi[element][i].first] != Putevi[element][i].second && state[Putevi[element][i].first] != 3)
                            state[Putevi[element][i].first] = -1;
                        else 
                        { 
                            state[Putevi[element][i].first] = Putevi[element][i].second;
                        }
                    }
                }
                PRQ.push(make_pair(Putevi[element][i].first, MinChanges[Putevi[element][i].first]));
            }
        

        }

    }

    if(MinChanges[seconde] == 2147483640)
    { 
        cout << -1;
    }
    else 
    { 
        cout << MinChanges[seconde];
    }







    return 0;
}