#include <bits/stdc++.h>
#define ll long long
#define MAXN 200010
#define INF 2000000010

using namespace std;

int N, M, X, Y, T, A, B, ima = 1;
vector <vector<pair<int, int> > > graf;
vector <bool> pos;
vector <int> dp;

void DFS(int cvor, int boja)
{
    pos[cvor] = true;
    for(auto par : graf[cvor])
    {
        int sled = par.first;
        int boja_sad = par.second;
        if(pos[sled]) continue;
        if(boja == -1) {DFS(sled, boja_sad);}
        if(boja_sad != boja) ima++;
        DFS(sled, boja_sad);

    }
    dp[cvor] = min(dp[cvor], ima);
    ima = 1;
}

int main()
{
    ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
    cin >> N >> M;
    pos.resize(N + 5);
    dp.resize(N + 5, INF);
    graf.resize(N + 5);
    for(int i = 0; i < M; i++)
    {
        cin >> X >> Y >> T;
        graf[X].push_back({Y, T});
        graf[Y].push_back({X, T});
    }
    cin >> A >> B;
    DFS(A, -1);
    cout << dp[B] << endl;
    return 0;
}