#include <bits/stdc++.h>

using namespace std;
int minDistance(int dist[], bool sptSet[], int n)
{
    int min = INT_MAX, min_index;
 
    for (int v = 0; v < n; v++)
        if (sptSet[v] == false && dist[v] <= min)
        {
            min = dist[v];
            min_index = v;
        }
    return min_index;
}
int main()
{
    int n, m;
    cin >> n >> m;
    int graph[n][n];
    memset(graph, -1, sizeof(graph));
    for (int i = 0; i < m; i++)
    {
        int a, b, t;
        cin >> a >> b >> t;
        graph[a - 1][b - 1] = t;
        graph[b - 1][a - 1] = t;
    }
    int str, end;
    cin >> str >> end;
    int dist[n];
    bool sptSet[n];
    int masina[n];
    for (int i = 0; i < n; i++)
        dist[i] = INT_MAX, sptSet[i] = false, masina[i] = 0;
    dist[str - 1] = 0;
    for (int count = 0; count < n - 1; count++) {
        int u = minDistance(dist, sptSet, n);
        sptSet[u] = true;
        for (int v = 0; v < n; v++)
        {
            int k = (graph[u][v] == masina[u]) ? 0 : 1;
            if (graph[u][v] != -1 && !sptSet[v]
                && dist[u] != INT_MAX
                && dist[u] + k < dist[v])
                {
                    dist[v] = dist[u] + k;
                    masina[v] = graph[u][v];
                }
        }
    }
    if (dist[end - 1] != INT_MAX)
    cout << dist[end - 1] << '\n';
    else
    cout << -1 << '\n';
    return 0;
}