using System;
using System.Collections.Generic;
namespace takmicenjeKvalifikacije
{
    internal class Program
    {
        struct cvor
        {
            public int a;
            public int b;
            public int t;
        }
        static void Main(string[] args)
        {
            string[] sr = Console.ReadLine().Split();
            int n = int.Parse(sr[0]), m = int.Parse(sr[1]);
            cvor[] a = new cvor[m];
            for(int i = 0; i < m; i++)
            {
                sr = Console.ReadLine().Split();
                a[i].a = int.Parse(sr[0]);
                a[i].b = int.Parse(sr[1]);
                a[i].t = int.Parse(sr[2]);
            }
            sr = Console.ReadLine().Split();
            int u = int.Parse(sr[0]), v = int.Parse(sr[1]);

            List<int> r = new List<int>();
            int br = 0;
            if (a[0].t != 0) br++;
            for (int i = 0; i < m - 1; i++)
            {
                for (int j = 1; j < m; j++)
                {
                    if (a[i].b == a[j].a)
                    {
                        if (a[i].t != a[j].t)
                        {
                            br++;
                        }
                        r.Add(br);
                    }
                }
            }
            int min = 0;
            foreach (int x in r)
            {
                if (x <= min)
                {
                    min = x;
                }
            }
            Console.WriteLine(min);
        }
    }
}