#include <iostream>
#include <vector>
using namespace std;

int nacin(int u, int v, int n, vector<vector<int>> putevi) {
    int k=0,l=0,t,c;
    vector<int> index_u;
    vector<int> index_v;
    for(int i=0;i<n;i++) {
        if(putevi[i][1]==v) {k=1; index_v.push_back(i);}
        if(putevi[i][0]==u) {l=1; index_u.push_back(i);}
    }
    if (k!=0 && l!=0)
    {
        return 1;
    } else return -1;
}

int main()
{
    int n,m,u,v;
    cin >> n >> m;
    vector<vector<int>> putevi(m,vector<int>(3,0));
    for(int i=0;i<m;i++) {
        for(int j=0; j<3; j++) {cin >> putevi[i][j];}
    }
    cin >> u >> v;
    cout << nacin(u,v,m,putevi) << endl;
    return 0;
}