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

namespace C_Sante
{
    class Program
    {
        struct Veza
        {
            public int b, k;
        }
        static bool DFS(int a, int b, bool[] p, List<Veza>[] v, ref int t, int pk)
        {
            if (a == b)
                return true;
            if (p[a])
                return false;
            p[a] = true;
            foreach (Veza u in v[a])
            {
                if (DFS(u.b, b, p, v, ref t, u.k))
                {
                    if (u.k != pk)
                        t++;
                    return true;
                }
            }
            return false;
        }
        static int Br(int a, int b, List<Veza>[] v, int n)
        {
            bool[] p = new bool[n];
            int t = 0;
            if (!DFS(a, b, p, v, ref t, 0))
                return -1;
            return t;
        }
        static void Main(string[] args)
        {
            string[] s = Console.ReadLine().Split();
            int n = int.Parse(s[0]);
            int m = int.Parse(s[1]);
            List<Veza>[] v = new List<Veza>[n];
            for (int i = 0; i < n; i++)
            {
                v[i] = new List<Veza>();
            }
            for (int i = 0; i < m; i++)
            {
                s = Console.ReadLine().Split();
                int a = int.Parse(s[0])-1;
                int b = int.Parse(s[1])-1;
                int k = int.Parse(s[2]);
                v[a].Add(new Veza { b=b,k=k});
            }
            s = Console.ReadLine().Split();
            int aa = int.Parse(s[0]) - 1;
            int bb = int.Parse(s[1]) - 1;
            Console.WriteLine(Br(aa,bb,v,n));
        }
    }
}