/// In The Name Of God

#include <bits/stdc++.h>

#define f first
#define s second

#define pb push_back
#define pp pop_back
#define mp make_pair

#define sz(x) (int)x.size()
#define sqr(x) ((x) * 1ll * (x))
#define all(x) x.begin(), x.end()

#define rep(i, l, r) for (int i = (l); i <= (r); i++)
#define per(i, l, r) for (int i = (l); i >= (r); i--)

#define Kazakhstan ios_base :: sync_with_stdio(0), cin.tie(0), cout.tie(0);

#define nl '\n'
#define ioi exit(0);

typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;

const int N = (int)5e5 + 7;
const int inf = (int)1e9 + 7;
const int mod = (int)1e9 + 7;
const ll linf = (ll)1e18 + 7;

const int dx[] = {-1, 0, 1, 0, 1, -1, -1, 1};
const int dy[] = {0, 1, 0, -1, 1, -1, 1, -1};

using namespace std;

int n, m;

unordered_map <int, vector <int > > g[N];

unordered_map <int, int> d[N];

int c[N];

int main() {
	#ifdef IOI2018
		freopen ("in.txt", "r", stdin);
    freopen ("E.out", "w", stdout);
	#endif
	Kazakhstan
	cin >> n >> m;
	rep(i, 1, m) {
		int a, b, c;
		cin >> a >> b >> c;
		g[a][c].pb(b);
    g[b][c].pb(a);
	}
	d[1][0] = 0;
	set < pair <int, pair <int, int> > > st;
	st.insert({0, {1, 0}});
  vector <bool> was(n + 1, 0);
	while (sz(st)) {
		int dist = st.begin() -> f;
		int v = st.begin() -> s.f, last = st.begin() -> s.s;
		st.erase(st.begin());
    
    if (!was[v]) {
      for (auto it : g[v]) {
        int C = it.f;
        for (auto to : it.s) {
          if (!d[to].count(C) || d[to][C] > dist + 1) {
            st.erase({d[to][C], {to, C}});
            d[to][C] = dist + 1;
            st.insert({d[to][C], {to, C}});
          }
        }
      }
      was[v] = 1;
    }

    if (sz(g[v][last])) {
      int C = last;
      for (auto to : g[v][last]) {
        if (!d[to].count(last) || d[to][last] > dist) {
          st.erase({d[to][C], {to, C}});
          d[to][C] = dist;
          st.insert({d[to][C], {to, C}});
        }
      }
      g[v][last].clear();
      g[v].erase(last);
    }
  }
  int ans = inf;
  for (auto it : d[n]) ans = min(ans, it.s);
  cout << (ans == inf ? -1 : ans);
	ioi
}

#include<bits/stdc++.h>

#define rep(i,n) for(int i = 0; i < (n); i++)
#define rrep(i,n) for(int i = (n)-1; i >= 0; i--)
#define rep1(i,n) for(int i = 1; i <= (n); i++)
#define rrep1(i,n) for(int i = (n); i > 0; i--)

#define ll long long
#define pi pair<int, int>
#define pll pair<ll, ll>

#define MOD 1000000007
#define INF 10000000

using namespace std;

int main(){
  int n,u,v; cin>>n>>u>>v; u--; v--;
  vector<vector<pi>> g( n, vector<pi>() );
  rep(i, n-1){
    int a,b; cin>>a>>b; a--; b--;
    g[a].push_back( {1,b} );
    g[b].push_back( {1,a} );
  }

  if(g[u].size()==1 && g[u][0].second==v) { cout<<0<<endl; return 0; }

  set<int>s;
  vector<int>d(n);
  rep(i, n)d[i]=INF; 
  d[u]=0;

  priority_queue<pi, vector<pi>, greater<pi>> pq;
  pq.push( {0, u} );
  while( pq.size() ){
    auto cn = pq.top(); pq.pop();
    if(d[cn.second] < cn.first)continue;
    for(auto nn :g[cn.second]){
      if(cn.first+1 >= d[nn.second] || nn.second==v)continue;

      d[nn.second]=cn.first+1;
      pq.push( {d[nn.second], nn.second} );
    }
  }

  rep(i, n)if(d[i]!=INF)s.insert(i);
  s.insert(v);

  vector<int>d2(d);
  rep(i, n)d[i]=INF; 
  d[v]=0;
  pq.push( {0, v} );
 
  while( pq.size() ){
    auto cn = pq.top(); pq.pop();
    if(d[cn.second] < cn.first)continue;

    for(auto nn :g[cn.second]){
      if(cn.first+1 >= d[nn.second] || s.count(nn.second)==0 )continue;

      d[nn.second]=cn.first+1;
      pq.push( {d[nn.second], nn.second} );
    }
  }

//for(auto t: d2)cout<<t<<' ';cout<<endl;
//for(auto t: d)cout<<t<<' ';cout<<endl;

bool f=true;
rep(i, n)if((d[i]==1 || d2[i]==1) && d[i]!=d2[i])f=false;
if(f) { cout<<1<<endl; return 0; }

  int result=0;
  rep(i, n)if(d[i]!=INF && d2[i]!=INF){
    if(d[i]<=d2[i])continue;
    result = max(result, d[i]-1);
  }

  cout<<result<<endl;

  return 0;
}