#include <bits/stdc++.h>
using namespace std;
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    bool postoji = true;
    vector<int> va; 
    vector<int> vb;
    vector<int> vt;
    vector<int>::iterator it; 
    int N, M, Mika, Laza, a, b, t; 
    int brojac = 0; 
    int i = 0;
    cin >> N >> M;
    for(int i = 0; i < M; i++){
        cin >> a; 
        va.push_back(a);
        cin >> b; 
        vb.push_back(b);
        cin >> t;
        vt.push_back(t);
    }
    cin >> Mika >> Laza; 
    while(i < vb.size()){
        if(vb[i] != Laza){
            postoji = false;
        }
        else{
            postoji = true; 
        }
        i++;
    }
    if(postoji == false){
        cout << "-1";
    }
    int tra = Mika;
    int trb; 
    for(it = va.begin(); it != va.end(); it++){
        if(vb[0] == va[i]){
            trb = i;
            brojac++;
            if(vb[i] == Laza){
                brojac++;
            }
        }
        else{
            i++; 
        }
    }
    cout << brojac;
    return 0;
}