using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading.Tasks;

namespace Sante
{
    internal class Program
    {   
        struct santa
        {
            public int a;
            public int b;
            public int s;
        }
        static void unos(out int n,out int m,ref santa[] a,out int u,out int v)
        {
            string[] red= Console.ReadLine().Split(' ');
            n=int.Parse(red[0]);
            m=int.Parse(red[1]);
            a = new santa[m];
            for(int i=0;i<m;i++)
            {
                red = Console.ReadLine().Split(' ');
                a[i].a=int.Parse(red[0]);
                a[i].b=int.Parse(red[1]);
                a[i].s=int.Parse(red[2]);
            }
            red = Console.ReadLine().Split(' ');
            u=int.Parse(red[0]);
            v=int.Parse(red[1]);
        }
        static void komsije(int n, santa[] a,ref santa[] k,int[] z)
        {
            for(int i=0;i<a.Length;i++)
            {
                if (a[i].a == n && z[i]==0)
                {
                    prosiri(ref k, a[i]);
                    z[i] = i;
                }
            }
        }
        static void prosiri(ref santa[] a,santa n)
        {
            santa[] b = new santa[a.Length + 1];
            for(int i=0;i<a.Length;i++)
            {
                b[i+1] = a[i];
            }
            b[0] = n;
            a = new santa[b.Length];
            for (int i = 0; i < a.Length; i++)
            {
                a[i] = b[i];
            }
        }
        static void skupi(ref santa[]  a)
        {
            santa[] b= new santa[a.Length-1];
            for(int i=1;i<a.Length;i++)
            {
                b[i-1]=a[i];
            }
            a = new santa[b.Length];
            for(int i=0;i<a.Length;i++)
            {
                a[i] = b[i];
            }
        }
        static void put(int u, int v, santa[] a, int[] z,int p,int m, ref int min,int h)
        {   
            santa[] k = new santa[0];
            komsije(u, a,ref k,z);
            if (k.Length!=0)
            {
                foreach(santa x in k)
                {
                    if(h==0)
                    {
                        m = -1;
                        p = 0;
                    }
                    //if (z[x]==0)
                    {   
                        if(x.s!=m)
                        {
                            p++;
                        }
                        //Console.WriteLine("h je:" + h);
                        //Console.WriteLine(m);
                        //Console.WriteLine("p je:" + p);
                        //Console.WriteLine(x.s);
                        //Console.WriteLine(v);
                        //z[x]=1;
                        skupi(ref k);
                        if(x.b==v)
                        {
                            //Console.WriteLine(min);
                            //Console.WriteLine("Mdmaaaaaaaa"+ min);
                            if (p < min||min==-1)
                            {
                                min = p;
                                //Console.WriteLine("min je:" + min);
                            }
                            //Console.WriteLine(p);
                        }
                        put(x.b, v, a, z,p,x.s,ref min,h+1);
                    }
                }
                if(min==int.MaxValue)
                {
                    min = -1;
                }
            }
        }
        static void Main(string[] args)
        {
            int n;
            int m;
            int u, v;
            int min = int.MaxValue;
            int[] k=new int[0];
            santa[] a=new santa[1];
            unos(out n, out m, ref a, out u, out v);
            int[] z = new int[n];
            put(u, v, a, z,0,-1,ref min,0);
            Console.WriteLine(min);
            //komsije(u, a, ref k, z);
            //foreach (int x in k)
            //{
            //    Console.WriteLine(x);

            //}
        }
    }
}