#include<bits/stdc++.h>
using namespace std;

int V;

struct node
{
    int to, weight;
};

vector <node> edges[200000];

unordered_map <int, pair<int, int>> dist;
void BFS(int src, int kraj)
{
    for (int i=0; i<V; i++)
    dist [i] = {INT_MAX, 0};
    deque <int> Q;
    dist[src] = {0, 0};

    Q.push_back(src);

    while (!Q.empty())
    {
        int v = Q.front();
        Q.pop_front();

        for (int i=0; i<edges[v].size(); i++)
        {
            int w;
            if(dist[v].second == edges[v][i].weight){
                w = 0;
            }
            else {
                w = 1;
            }
            if (dist[edges[v][i].to].first > dist[v].first + w)
            {
                dist[edges[v][i].to].first = dist[v].first + w;
                dist[edges[v][i].to].second = edges[v][i].weight;

                if (w == 0)
                    Q.push_front(edges[v][i].to);
                else
                    Q.push_back(edges[v][i].to);
            }
        }
        if (v == kraj){
            cout << dist[v].first;
            break;
        }
    }
}
void dodaj(int u, int v, int wt)
{
   edges[u].push_back({v, wt});
   edges[v].push_back({u, wt});
}

int main()
{
    cin >> V;
    int k;
    cin >> k;
    int a, b, c;

    for (int i = 0; i < k; i++){
        cin >> a >> b >> c;
        dodaj(a - 1, b - 1, c);
    }

    int src;
    int kraj;
    cin >> src >> kraj;
    BFS(src - 1, kraj - 1);
}