using System;
using System.Linq;

namespace ConsoleApp23
{
    internal class Program
    {
        struct Path
        {
            public int a;
            public int b;
            public int t;
        }

        private static void gomb(ref Path[] ut)
        {
            for (int i = 0; i < ut.Length; i++)
            {
                if (ut[i].t == 1)
                {
                    ut[i].t = 0;
                }
                else
                {
                    ut[i].t = 1;
                }
            }
        }

        private static void gomb(ref Path cur)
        {
            if (cur.t == 1)
            {
                cur.t = 0;
            }
            else
            {
                cur.t = 1;
            }
        }

        static int pathkereses(Path[] ut, ref Path cur, int goal)
        {
            int c = 0;
            while (true)
            {
                if (cur.t == 1 && cur.b == goal)
                {
                    c++;
                    break;
                }
                if (cur.t == 0 && cur.b == goal)
                {
                    break;
                }
                if (cur.t == 1)
                {
                    gomb(ref ut);
                    gomb(ref cur);
                    c++;
                }
                bool van = false;
                for (int i = 0; i < ut.Length; i++)
                {
                    if (ut[i].a == cur.b)
                    {
                        cur = ut[i];
                        van = true;
                        break;
                    }
                }
                if (van == false)
                {
                    return -1;
                }
            }
            return c;
        }

        static void Main(string[] args)
        {
            string bemenet = Console.ReadLine();
            string[] adatok = bemenet.Split(' ').ToArray();
            int n = Convert.ToInt32(adatok[0]);
            int m = Convert.ToInt32(adatok[1]);

            Path[] ut = new Path[m];
            for (int i = 0; i < m; i++)
            {
                bemenet = Console.ReadLine();
                adatok = bemenet.Split(' ').ToArray();
                ut[i].a = Convert.ToInt32(adatok[0]);
                ut[i].b = Convert.ToInt32(adatok[1]);
                ut[i].t = Convert.ToInt32(adatok[2]);
            }

            bemenet = Console.ReadLine();
            adatok = bemenet.Split(' ').ToArray();
            int start = Convert.ToInt32(adatok[0]);
            int goal = Convert.ToInt32(adatok[1]);

            Path cur = new Path();
            Path[] def = new Path[m];
            for (int i = 0; i < def.Length; i++)
            {
                def[i].a = ut[i].a;
                def[i].b = ut[i].b;
                def[i].t = ut[i].t;
            }

            bool van = false;
            for (int i = 0; i < ut.Length; i++)
            {
                if (ut[i].a == start)
                {
                    cur = ut[i];
                    int res = pathkereses(ut, ref cur, goal);
                    if (res == -1)
                    {
                        ut = def;
                    }
                    else
                    {
                        van = true;
                        Console.WriteLine(res);
                        break;
                    }
                }
            }
            if (van == false)
            {
                Console.WriteLine(-1);
            }
            //Console.ReadKey();
        }
    }
}