#include<iostream>
#include<vector>
#include<map>

using namespace std;

#define MAXN 200005
vector<vector<int>> G(MAXN);
bool visited[MAXN];
int minPut = MAXN;
int trenPut = 0;
typedef pair<int, int> MyKey;
typedef map<MyKey, int> Mapa;
Mapa opisPuta;

void dfs(int node, int finish, int prev, int trenPut)
{
    if (node == finish)
    {
        if (trenPut < minPut)
            minPut = trenPut;
        //return;
    }
    visited[node] = true;
    for (auto i : G[node])
    {
        if (!visited[i])
        {
            if (opisPuta[MyKey(node, i)] == 1 && prev == 0)
            {
                trenPut++;
                prev = 1;
            }
            // if(opisPuta[MyKey(node, i)] == 1 && prev == 1)
            //nista
            else if (opisPuta[MyKey(node, i)] == 0 && prev == 1)
            {
                trenPut++;
                prev = 0;
            }
            dfs(i, finish, prev, trenPut);
            visited[i] = false;
        }
    }
}

int main()
{

    ios_base::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);
    int n, m, a, b, c, u, v;
    cin >> n >> m;
    for (int i = 0; i < m; i++)
    {
        cin >> a >> b >> c;
        G[a].push_back(b);
        G[b].push_back(a);
        opisPuta.insert(make_pair(MyKey(a, b), c));
        opisPuta.insert(make_pair(MyKey(b, a), c));
    }
    cin >> u >> v;
    //cout << opisPuta[MyKey(2, 3)] << endl;
    dfs(u, v, 0, 0);
    if (minPut == MAXN)
        cout << -1 << endl;
    else
        cout << minPut << endl;
    return 0;
}