#include <bits/stdc++.h>

using namespace std;

int main(){

    ios_base::sync_with_stdio(0);
    cin.tie(0);

    int n, m;

    cin >> n >> m;

    vector<vector<pair<int, int>>> al(n);

    for(int i = 0; i < m; ++i){
        int u, v, t;

        cin >> u >> v >> t;

        --u, --v;
        al[u].emplace_back(v, t);
        al[v].emplace_back(u, t);
    }

    int s, e;
    cin >> s >> e;
    --s, --e;

    bool st = 0;
    int ans = 0;
    queue<int> bfs, newBfs;
    bfs.push(s);

    vector<bool> vis(n);
    vis[s] = 1;

    while(!bfs.empty()){
        int t = bfs.front();
        vis[t] = 1;
        bfs.pop();

        if(t == e) break;
        
        for(pair<int, int> ed : al[t]){
            if(vis[ed.first]) continue;

            if(ed.second == st) bfs.push(ed.first), vis[ed.first] = 1;
            else newBfs.push(ed.first);
        }

        if(bfs.empty()) st ^= 1, ++ans, bfs = newBfs, newBfs = queue<int>();
    }

    cout << ans << '\n';
    return 0;
}