using System;
using System.Collections.Generic;
class ConsoleApp8
{
    static int br = 0;
    static int stanje = 0;
    struct tip
    {
        public int p, k, t;
    }
    public static void Main(String[] args)
    {
        int N, M;
        string[] s=Console.ReadLine().Split(' ');
        N=int.Parse(s[0]);
        M=int.Parse(s[1]);
        tip[] t=new tip[M];
        List<List<int>> lista = new List<List<int>>(N);
        for (int i = 0; i < N; i++)
        {
            lista.Add(new List<int>());
        }
        for (int i = 0; i < M; i++)
        {
            s = Console.ReadLine().Split();
            int a, b, c;
            a=int.Parse(s[0])-1;
            b=int.Parse(s[1])-1;
            c=int.Parse(s[2]);
            ivica(lista, a, b);
            t[i].p = a;
            t[i].k = b;
            t[i].t = c;
        }
        s=Console.ReadLine().Split(' ');
        int pocetak = int.Parse(s[0]), kraj = int.Parse(s[1]);
        ispis(lista, pocetak, kraj, N,t);
    }
    private static void ivica(List<List<int>> lista,int i, int j)
    {
        lista[i].Add(j);
        lista[j].Add(i);
    }
    private static void ispis(List<List<int>> lista,int pocetak, int kraj, int v,tip[] t)
    {
        int[] pred = new int[v];
        int[] udalj = new int[v];
        if (BFS(lista, pocetak, kraj, v, pred, udalj) == false)
        {
            Console.WriteLine("-1");
            return;
        }
        List<int> put = new List<int>();
        int puzi = kraj;
        put.Add(puzi);

        while (pred[puzi] != -1)
        {
            dodaj(pred, put, puzi);
            puzi = pred[puzi];
        }
        for (int i = put.Count - 1;i >= 0; i--)
        {
            if (t[i].t != stanje)
            {
                br++;
                if (stanje == 1)
                    stanje = 0;
                else stanje++;
            }
        }
        Console.WriteLine(br);
    }

    private static void dodaj(int[] pred, List<int> put, int puzi)
    {
        put.Add(pred[puzi]);
    }

    private static bool BFS(List<List<int>> lista, int iz, int kraj,int v, int[] pred,int[] udalj)
    {
        List<int> red = new List<int>();
        bool[] poseceno = new bool[v];
        for (int i = 0; i < v; i++)
        {
            poseceno[i] = false;
            udalj[i] = int.MaxValue;
            pred[i] = -1;
        }
        poseceno[iz] = true;
        udalj[iz] = 0;
        red.Add(iz);
        while (red.Count != 0)
        {
            int u = red[0];
            red.RemoveAt(0);

            for (int i = 0;
                     i < lista[u].Count; i++)
            {
                if (poseceno[lista[u][i]] == false)
                {
                    poseceno[lista[u][i]] = true;
                    udalj[lista[u][i]] = udalj[u] + 1;
                    pred[lista[u][i]] = u;
                    red.Add(lista[u][i]);
                    if (lista[u][i] == kraj)
                        return true;
                }
            }
        }
        return false;
    }
}