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

namespace Sante
{
    class Program
    {
        static int sadrzi(int[] a, int x)
        {
            for (int i = 0; i < a.Length; i++)
            {
                if (a[i] == x)
                    return i;
            }
            return -1;
        }
        static int brPuteva(int[] a, int[] b, int[] t, int u, int v)
        {
            int m = a.Length;
            int k = 0;
            int r = sadrzi(b, v);
            for (int i = r; i > u; i--)
            {
                int p = sadrzi(b, a[i]);
                int x = a[p];
                if (t[i] != t[p])
                    k++;
                i = p + 1;
            }
            return k + 1;
        }
        static void sortiraj(int[] a, int[] b, int[] t)
        {
            int n = b.Length;
            for (int i = 0; i < n - 1; i++)
            {
                for (int j = i + 1; j < n; j++)
                {
                    if (b[i] > b[j])
                    {
                        int pom = b[i];
                        b[i] = b[j];
                        b[j] = pom;
                        pom = a[i];
                        a[i] = a[j];
                        a[j] = pom;
                        pom = t[i];
                        t[i] = t[j];
                        t[j] = pom;
                    }
                }
            }
        }
        static void Main(string[] args)
        {
            string[] s = Console.ReadLine().Split();
            int n = int.Parse(s[0]);
            int m = int.Parse(s[1]);
            int[] a = new int[m];
            int[] b = new int[m];
            int[] t = new int[m];
            for (int i = 0; i < m; i++)
            {
                s = Console.ReadLine().Split();
                a[i] = int.Parse(s[0]);
                b[i] = int.Parse(s[1]);
                t[i] = int.Parse(s[2]);
            }
            s = Console.ReadLine().Split();
            int u = int.Parse(s[0]);
            int v = int.Parse(s[1]);
            sortiraj(a, b, t);
            Console.WriteLine(brPuteva(a, b, t, u, v));
        }
    }
}