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

namespace Sante
{
    internal class Program
    {
        struct susret
        {
            public int mika;
            public int laza;
            public int t;
        }
        static void Razmeni(ref susret a, ref susret b)
        {
            susret t = a;
            a = b;
            b = t;
        }
        static void Main(string[] args)
        {
            string[] s=Console.ReadLine().Split();
            int n = int.Parse(s[0]);
            int m = int.Parse(s[1]);
            susret[] a = new susret[m];

            for (int i = 0; i < m; i++)
            {
                s = Console.ReadLine().Split();
                a[i].mika = int.Parse(s[0]);
                a[i].laza = int.Parse(s[1]);
                a[i].t = int.Parse(s[2]);
            }
            s= Console.ReadLine().Split();
            int u = int.Parse(s[0]);
            int v= int.Parse(s[1]);
            for (int i = 0; i < m-1; i++)
            {
                for (int j = i + 1; j < m; j++)
                {
                    if (a[i].mika < a[j].mika) Razmeni(ref a[i], ref a[j]);
                    if (a[i].mika == a[j].mika && a[i].laza < a[j].laza) Razmeni(ref a[i], ref a[j]);

                }
            }
            int br = 1;
            int maximum = -1000; 
            int p = a[0].t;
            for (int i = 0; i < m-1; i++)
            {
                if (a[i].mika == u && a[i].laza > maximum) { maximum = a[i].laza; }
                else if (a[i+1].mika != u)
                {
                    u = maximum;
                    if (a[i].t != p) { br++; p = a[i].t; }
                }

            }
            if (u != v) Console.WriteLine(br);
            else Console.WriteLine("-1");
        }

    }
}