using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PetljaTakmicenje
{
    class Program
    {

        static void Santa(List<int>[] a, bool[] t, bool r, int x, int y, int b, ref int min)
        {
            foreach (int i in a[x])
            {
                if (r != t[x + i])
                {
                    b++;
                }
                if (i == y && b < min)
                {
                    min = b;
                }
                Santa(a, t, t[x + i], i, y, b, ref min);
            }
        }

        static void Main(string[] args)
        {
            string[] s = Console.ReadLine().Split();
            int n = int.Parse(s[0]), m = int.Parse(s[1]), min = int.MaxValue;
            bool[] t = new bool[400010];
            List<int>[] a = new List<int>[n];
            for (int i = 0; i < n; i++)
            {
                a[i] = new List<int>();
            }
            for (int i = 0; i < m; i++)
            {
                s = Console.ReadLine().Split();
                int x = int.Parse(s[0]) - 1, y = int.Parse(s[1]) - 1, z = int.Parse(s[2]);
                a[x].Add(y);
                t[x + y] = (z == 0) ? false : true;
            }
            s = Console.ReadLine().Split();
            Santa(a, t, false, int.Parse(s[0]) - 1, int.Parse(s[1]) - 1, 0, ref min);
            if (min == int.MaxValue)
            {
                Console.WriteLine(-1);
            }
            else
            {
                Console.WriteLine(min);
            }
        }
    }
}