using System;
using System.Collections.Generic;

namespace santeleda
{
    class Program
    {
        static int masina = 0;
        static long br = 0;
        static long mini = long.MaxValue;
        static long put(long r1, long r2, List<Tuple<long, long>>[] lista, bool prolaz)
        {
            if (prolaz)
            {
                if (r1 == r2)
                {
                    if (br < mini) mini = br;
                    br = 0;
                    return mini;
                }
                foreach (var p in lista[r1])
                {
                    if (p.Item2 == 1 && masina == 0)
                    { 
                        masina = 1; br++;
                    }
                    else if (p.Item2 == 0 && masina == 1)
                    {
                        masina = 0; br++;
                    }
                    put(p.Item1, r2, lista, true);
                }
                return put(r1, r2, lista, false);
            }
            else return mini;
        }
        static void Main(string[] args)
        {
            string[] unos = Console.ReadLine().Split();
            int N = int.Parse(unos[0]);
            int M = int.Parse(unos[1]);
            List<Tuple<long, long>>[] lista = new List<Tuple<long, long>>[N];

            for (int i = 0; i < N; i++) lista[i] = new List<Tuple<long, long>>();

            for(int i = 0; i < M; i++)
            {
                string[] unos1 = Console.ReadLine().Split();
                lista[long.Parse(unos1[0]) - 1].Add(Tuple.Create(long.Parse(unos1[1]) - 1, long.Parse(unos1[2])));
            }
            string[] unos2 = Console.ReadLine().Split();
            int a = int.Parse(unos2[0]);
            int b = int.Parse(unos2[1]);

            long pom = put(a - 1, b - 1, lista, true);
            if (pom == long.MaxValue) Console.WriteLine(-1);
            else Console.WriteLine(pom);
        }
    }
}