#include<bits/stdc++.h>
using namespace std;
struct node
{
    int k, t;
};
vector <node> e[200000];
void BFS(int m,int l,int N)
{
    int V = N;
    int dist[200000];
    for (int i = 1; i <= V; i++)
        dist[i] = INT_MAX;
    bool d[200000];
    int ma = 0;
    deque <int> Q;
    dist[m] = 0;
    Q.push_back(m);

        while (!Q.empty())
    {
        int v = Q.front();
        Q.pop_front();
        for (int i=0; i<e[v].size(); i++)
        {
            if (dist[e[v][i].k] > dist[v] + e[v][i].t)
            {
                dist[e[v][i].k] = dist[v];
                if(d[v]!=e[v][i].t){
                        dist[e[v][i].k]++;
                        if(d[v])d[e[v][i].k]=false;
                        else d[e[v][i].k]=true;
                    }
                if (e[v][i].t == 0)
                    Q.push_front(e[v][i].k);
                else
                    Q.push_back(e[v][i].k);
            }
        }
    }
        cout << dist[l];

}

void dodaj(int u, int v, int t)
{
    e[u].push_back({ v, t });
    e[v].push_back({ u, t });
}
int main()
{
    int N, M;
    cin >> N >> M;
    int x, y, t;
    for (int i = 0; i < M; i++) {
        cin >> x >> y >> t;
        dodaj(x, y, t);
    }
    int m, l;
    cin >> m >> l;
    BFS(m,l,N);
    return 0;
}