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

namespace ConsoleApplication317
{
    class Program
    {
        struct veza
        {
            public int st;
            public int c;

        }
        static int put(List<veza>[] a, bool[] posecen, int s, int kc, int tr, int[] putevi)
        {
            if (tr == kc) return 0;
            int l = a[tr].Count();
            int min = int.MaxValue;
            for (int i = 0; i < l; i++)
            {
                int p = 0;
                if (!posecen[a[tr][i].c])
                {
                    posecen[a[tr][i].c] = true;
                    if (a[tr][i].st != s) p++;
                    s = a[tr][i].st;
                    int pp = put(a, posecen, s, kc, tr, putevi);
                    p += pp;
                    if (p < min) min = p;
                    if (pp < putevi[a[tr][i].c])
                    {
                        putevi[a[tr][i].c] = pp;
                    }
                }
                else
                {
                    if (putevi[a[tr][i].c] > 0)
                    {
                        if (a[tr][i].st != s) p++;
                        s = a[tr][i].st;
                        p += putevi[a[tr][i].c];
                    }
                }
            }
            if (!posecen[kc])
            {
                putevi[tr] = -1;
                return -1;
            }
            return min;
        }
        static void Main(string[] args)
        {
            string[] str1 = Console.ReadLine().Split();
            int n = int.Parse(str1[0]);
            int m = int.Parse(str1[1]);
            int[] putevi = new int[n + 1];
            List<veza>[] a = new List<veza>[n + 1];
            bool[] posecen = new bool[n + 1];
            for (int i = 0; i <= n; i++)
            {
                a[i] = new List<veza>();
                posecen[i] = false;
                putevi[i] = int.MaxValue;
            }
            for (int i = 0; i < m; i++)
            {
                string[] str2 = Console.ReadLine().Split();
                int x = int.Parse(str2[0]);
                int y = int.Parse(str2[1]);
                int stnj = int.Parse(str2[2]);
                veza con;
                con.st = stnj;
                con.c = y;
                a[x].Add(con);
                con.c = x;
                a[y].Add(con);
            }
            str1 = Console.ReadLine().Split();
            int pc = int.Parse(str1[0]);
            int kc = int.Parse(str1[1]);
            int s = 0;
            int res = put(a, posecen, s, kc, pc, putevi);
            Console.WriteLine(res);
            Console.ReadKey();
        }
    }
}