#include <bits/stdc++.h>

#define ll unsigned long long

using namespace std;

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    ll n, m, a, b, t, u, w;
    cin >> n >> m;
    vector<ll> paths[n];
    ll water[n][n];
    vector<ll> distances1(n, numeric_limits<ll>::max());
    vector<ll> distances2(n, numeric_limits<ll>::max());

    for (ll i = 0; i < m; i++)
    {
        cin >> a >> b >> t;
        a--;
        b--;
        paths[a].push_back(b);
        paths[b].push_back(a);
        water[a][b] = t;
        water[b][a] = t;
    }
    cin >> u >> w;
    u--;
    w--;

    distances1[u] = 0;
    distances2[u] = numeric_limits<ll>::max();

    queue<ll> q;
    q.push(u);
    bool visited[n] = {false};
    visited[u] = true;
    
    while (!q.empty()) {
        ll i = q.front();

        q.pop();
        for (auto j : paths[i]) {
            if (visited[j])
                continue;

            visited[j] = true;

            if (distances1[i] < distances2[i]) {
                if (water[i][j] == 0) {
                    distances1[j] = min(distances1[j], distances1[i]);
                } else {
                    distances2[j] = min(distances2[j], distances1[i] + 1);
                }
            } else {
                if (water[i][j] == 0) {
                    distances1[j] = min(distances1[j], distances2[i] + 1);
                } else {
                    distances2[j] = min(distances2[j], distances2[i]);
                } 
            }

            q.push(j);
        }
    }

    cout << min(distances1[w], distances2[w]) << "\n";
}