#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define debug(x) cout<<#x<< " = " << x << endl;
#define node tuple<int, int, int>
const int N = 2e5 + 3;
const ll MOD = 998244353;
const int INF = 1e9;
unordered_map<int,int> adj[N];
//---------------------------------

int main() {
    ios::sync_with_stdio(false);
    int n, m; cin >> n >> m;

    for (int i = 0; i < m; i++) {
        int a,b,t; cin >> a >> b >> t;
        if (a == b) continue;
        adj[a][b] = t;
        adj[b][a] = t;
    }
    int mika, laza; cin >> mika >> laza;

    int minDist[N][2];
    for (int i = 0; i <= n; i++) {
        minDist[i][0] = INF;
        minDist[i][1] = INF;
    }
    minDist[mika][0] = 0;
    priority_queue<node, vector<node>, greater<>> nodes;
    nodes.push({0, 0, mika});

    while (!nodes.empty()) {
        auto gornje = nodes.top();
        int w1 = get<0>(gornje), t1 = get<1>(gornje), v1 = get<2>(gornje);
        nodes.pop();
        if (min(minDist[v1][0], minDist[v1][1]) < w1) continue;

        for (auto neighbor : adj[v1]) {
            int v2 = neighbor.first, t2 = neighbor.second;
            int prolaz = (t1 != t2);
            if (minDist[v2][t2] <= minDist[v1][t1] + prolaz) continue;
            minDist[v2][t2] = minDist[v1][t1] + prolaz;
            nodes.push({minDist[v2][t2], t2, v2});
        }
    }

    cout << min(minDist[laza][0], minDist[laza][1]) << "\n";

    return 0;
}
/*
8 9
1 3 1
1 2 1
2 4 1
2 5 0
3 5 1
5 6 1
6 8 0
3 7 0
7 8 1
1 8
*/