#include <bits/stdc++.h>

#define MAXN 200010

using namespace std;

vector<pair<int,int>> adj[MAXN];

int foo(int node1, int node2, int state) {
    if(node1 == node2) {
        return 0;
    }

    int r = MAXN;
    for(auto node: adj[node1]) {
        int r1;
        if(node.second == state)
            r1 = foo(node.first, node2, state);
        else {
            r1 = 1 + foo(node.first, node2, node.second);
        }
        r = min(r, r1);
    }
    return r;
}

int main()
{
    int n,m;
    cin >> n >> m;
    for(int i = 0; i < m; i++) {
        int a,b,c;
        cin >> a >> b >> c;
        adj[a].push_back({b,c});
    }
    int x,y;
    cin >> x >> y;
    int r = foo(x,y,0);
    cout << r << endl;
    return 0;
}