#include <bits/stdc++.h>
#define ll long long

using namespace std;

vector<pair<int, int>> G[200005];
int posecen[200005] = {0};
int udaljenost[200005];
int stigao = 0;
queue<int> kandidati;

void dfs(int c, int stanje, int tipPuta, int trenutno, int kraj) {
    if(posecen[c]) return;
    posecen[c] = 1;

    ll da = 0;

    if(stanje != tipPuta) {
        stanje = tipPuta;
        da = 1;
    }

    if(c - kraj == 0) {
        stigao = 1;
        kandidati.push(trenutno+da);
    }

    for(auto u : G[c]) {
       dfs(u.first, stanje, u.second, trenutno + da, kraj);
    }
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);

    int n, m;
    cin >> n >> m;

    for(int i=0; i<m; i++) {
        int a, b, t;
        cin >> a >> b >> t;

        G[a].push_back(make_pair(b, t));
        G[b].push_back(make_pair(a, t));
    }

    int M, L;
    cin >> M >> L;

    dfs(M, 0, 0, 0, L);
    ll res = INT_MAX;

    while(!kandidati.empty()) {
        int e = kandidati.front(); kandidati.pop();
        if(e < res)
            res = e;
    }

    if(stigao) cout << res << endl;
    else cout << "-1" << endl;

    return 0;
}