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

namespace ConsoleApp7
{
    internal class Program
    {
        static void sante(int[,] putevi, bool[] predjeno, int n, int U)
        {
            predjeno[U - 1] = true;
            for (int i = 0; i < n; i++)
            {
                if (!predjeno[i])
                {
                    if (putevi[U - 1, i] > 0)
                    {
                        sante(putevi, predjeno, n, i + 1);
                    }
                }
            }

        }

        static void Main(string[] args)
        {
            string u = Console.ReadLine();
            string[] ulaz = new string[2];
            ulaz = u.Split(' ');

            int n = Convert.ToInt32(ulaz[0]);
            int m = Convert.ToInt32(ulaz[1]);

            int[,] put = new int[n, 3];
            ulaz = new string[3];

            for (int j = 0; j < m; j++)
            {
                u = Console.ReadLine();
                ulaz = u.Split(' ');

                put[j, 0] = Convert.ToInt32(ulaz[0]);
                put[j, 1] = Convert.ToInt32(ulaz[1]);
                put[j, 2] = Convert.ToInt32(ulaz[2]);
            }

            u = Console.ReadLine();
            ulaz = new string[2];
            ulaz = u.Split(' ');
            int U = Convert.ToInt32(ulaz[0]);
            int V = Convert.ToInt32(ulaz[1]);
            bool[] predjeno = new bool[n];

            int[,] putevi = new int[n, n];
            int i = 0;
            while (i < m)
            {
                putevi[put[i, 0] - 1, put[i, 1] - 1] = put[i, 2] + 1;
                putevi[put[i, 1] - 1, put[i, 0] - 1] = put[i, 2] + 1;
                i++;
            }
            

            sante(putevi, predjeno, n, U);

            if (predjeno[V - 1]) Console.WriteLine(2);
            else Console.WriteLine(-1);
            //Console.ReadLine();

        }
    }
}