using System;
using System.Collections.Generic;
using System.Linq;

namespace santeCS
{
    internal class Program
    {
        static void FeikDFS(List<int> kejovi,Cvor[] a, bool[] vis, int v, int w, int k, bool st, int laza)
        {
            if (!st && w == 1)
            {

                st = true;
                k++;
            }
            else if (st && w == 0)
            {
                st = false;
                k++;
            }
            if (laza == v)
            {
                kejovi.Add(k);
                return;
            }
            if (vis[v]) return;
            vis[v] = true;
            foreach (Grana y in a[v].granice)
            {
                FeikDFS(kejovi, a, vis, y.kraj.ind, y.w, k, st, laza);
            }
        }
        class Cvor
        {
            public int ind;
            public List<Grana> granice;
            public Cvor(int i)
            {
                this.granice = new List<Grana>();
                this.ind = i;
            }

        }
        class Grana
        {
            public int w;
            public Cvor kraj;
            public Grana(int w,Cvor k)
            {
                this.w = w;
                this.kraj = k;
            }
        }
        static void Main(string[] args)
        {
            int n, m;
            string[] str = Console.ReadLine().Split();
            n = int.Parse(str[0]);
            m = int.Parse(str[1]);
            Cvor[] sante = new Cvor[n];
            for(int i = 0; i < n; i++)
            {
                sante[i] = new Cvor(i);
            }
            int x, y, z;
            for(int i = 0; i < m; i++)
            {
                str = Console.ReadLine().Split();
                x = int.Parse(str[0]);
                y = int.Parse(str[1]);
                z = int.Parse(str[2]);
                sante[x - 1].granice.Add(new Grana(z, sante[y - 1]));
                sante[y - 1].granice.Add(new Grana(z, sante[x - 1]));
            }
            str = Console.ReadLine().Split();
            int mika = int.Parse(str[0]) - 1;
            int laza = int.Parse(str[1]) - 1;
            bool[] obidjen = new bool[n];
            for(int i = 0; i < n; i++)
            {
                obidjen[i] = false;
            }
            List<int> intovi = new List<int>();
            FeikDFS(intovi, sante, obidjen, mika, 0, 0, false, laza);
            if (intovi.Any()) Console.WriteLine(intovi.Min());
            else Console.WriteLine(-1);
        }
    }
}