#include <iostream>
#include <algorithm>
#include <vector>
#include <tuple>
#include <set>
using namespace std;

bool to_bool(int a)
{
    return a != 0;
}

int in(vector<tuple<int, int, bool>> frontier, int f)
{
    for(int i = 0; i < frontier.size(); i++)
    {
        if(get<0>(frontier.at(i)) == f)
        {
            return i;
        }
    }
    return -1;
}

bool sort_func(tuple<int, int, bool> a, tuple<int, int, bool> b)
{
    return get<1>(a) > get<1>(b);
}

void prosiri_tacku(vector<tuple<int, int, bool>>* frontier, set<int>* visited,
        const vector<vector<pair<int, bool>>> veze)
{
    // razvijamo (prosirujemo) tacku frontier[0], jer je ona najobecavajuca
    for(int i = 0; i < veze.at(get<0>(frontier->at(0))).size(); i++)
    {
        if(visited->count(veze.at( get<0>(frontier->at(0)) ).at(i).first) < 1) {
            if(veze.at( get<0>(frontier->at(0)) ).at(i).second != get<2>(frontier->at(0)))
            {
                frontier->push_back(make_tuple(veze.at( get<0>(frontier->at(0)) ).at(i).first,
                        get<1>(frontier->at(0)) + 1, !get<2>(frontier->at(0))));
            }
            else
            {
                frontier->push_back(make_tuple(veze.at( get<0>(frontier->at(0)) ).at(i).first,
                                               get<1>(frontier->at(0)), get<2>(frontier->at(0))));
            }
            visited->insert(veze.at( get<0>(frontier->at(0)) ).at(i).first);
        }
    }

    // i na kraju brisemo tacku iz frontiera
    frontier->erase(frontier->begin());
}

int main()
{
    int n, a, t1, t2, t3, m, l;
    cin >> n >> a;
    vector<vector<pair<int, bool>>> veze(n + 1);
    for(int i = 0; i < a; i++)
    {
        cin >> t1 >> t2 >> t3;
        veze.at(t1).push_back(make_pair(t2, to_bool(t3)));
        veze.at(t2).push_back(make_pair(t1, to_bool(t3)));
    }
    cin >> m >> l;

    vector<tuple<int, int, bool>> frontier;
    set<int> visited;

    frontier.push_back(make_tuple(m, 0, false));
    visited.insert(m);

    while(true)
    {
        if(frontier.empty())
        {
            goto nema;
        }
        if(in(frontier, l) != -1)
        {
            break;
        }
        sort(frontier.begin(), frontier.end(), sort_func);
        prosiri_tacku(&frontier, &visited, veze);
    }
    // gotovo!
    cout << get<1>(frontier.at(in(frontier, l))) << endl;
    return 0;
    nema:
    cout << -1 << endl;
    return 0;
}