#include <iostream>
using namespace std;

int main()
{
 typedef struct
 {
  int a;
  int b;
  int t;
 } put;
 int N, M, u, v, p, x, g = 0, d;
 cin >> N >> M;
 put putevi[M];
 for (int i = 0; i < M; i++)
 {
  cin >> putevi[i].a >> putevi[i].b >> putevi[i].t;
 }
 cin >> u >> v;
 x = u;
 for (int i = 0; i < M; i++)
 {
  for (int j = i; j < M; j++)
  {
   if (putevi[j].a == u)
   {
    u = putevi[j].b;
    if (putevi[j].t != g)
    {
     g = putevi[j].t;
     p++;
    }
   }
   if (u == v)
   {
    d = j;
    break;
   }
  }
  if (putevi[d].b == v)
  {
   break;
  }
  u = x;
  if (i = M)
  {
   cout << "-1";
   return 0;
  }
 }
 cout << p;
 return 0;
}