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

namespace santeee
{
    internal class Program
    {
        struct santaputevi
        {
            public int a;
            public int b;
            public int t;
        }
        static void Main(string[] args)
        {
            int t0 = 0;
            string[] s = Console.ReadLine().Split(' ');
            int n = int.Parse(s[0]), m = int.Parse(s[1]);
            if (m != 0) 
            {
                santaputevi[] mm = new santaputevi[m];
                for (int i = 0; i < m; i++)
                {
                    s = Console.ReadLine().Split(' ');
                    mm[i].a = int.Parse(s[0]); mm[i].b = int.Parse(s[1]); mm[i].t = int.Parse(s[2]);
                }
                s = Console.ReadLine().Split(' ');
                int u = int.Parse(s[0]), v = int.Parse(s[1]);
                int br = 0;
                    for (int i = 0; i < m; i++)
                {
                    if (u == mm[i].a && u < mm[i].b)
                    { u = mm[i].b; if (mm[i].t != t0) { t0 = mm[i].t; br++;  } }
                    if (u == mm[i].b && u < mm[i].a)
                    { u = mm[i].a; if (mm[i].t != t0) { t0 = mm[i].t; br++; } }                }
                 
                if(u==v) Console.WriteLine(br);
                else Console.WriteLine("-1");
            } 
            else Console.WriteLine("-1");
        }
    }
}