#include <bits/stdc++.h>
#define rep(i,n) for(int i = 0; i < (int)(n); i++)
#define rrep(ri,n) for(int ri = (int)(n-1); ri >= 0; ri--)
#define rep2(i,x,n) for(int i = (int)(x); i < (int)(n); i++)
#define repit(itr,x) for(auto itr = x.begin(); itr != x.end(); itr++)
#define rrepit(ritr,x) for(auto ritr = x.rbegin(); ritr != x.rend(); ritr++)
#define ALL(n) begin(n), end(n)
using ll = long long;
using namespace std;

void dfs(vector<vector<int>> &tree, vector<int> &d, int p){
  for(auto i : tree.at(p)){
    if(d.at(i) != -1) continue;
    d.at(i) = d.at(p) + 1;
    dfs(tree, d, i);
  }
}

int main(){
  int n, ta, ao;
  cin >> n >> ta >> ao;
  ta--; ao--;
  vector<vector<int>> tree(n, vector<int>());
  rep(i, n-1){
    int a, b;
    cin >> a >> b;
    a--; b--;
    tree.at(a).push_back(b);
    tree.at(b).push_back(a);
  }

  vector<int> dta(n, -1), dao(n, -1);
  dta.at(ta) = 0; dao.at(ao) = 0;
  dfs(tree, dta, ta);
  dfs(tree, dao, ao);
  // rep(i, n){
  //   cerr << dta.at(i) << " ";
  // }
  // cerr << endl;
  // rep(i, n){
  //   cerr << dao.at(i) << " ";
  // }
  // cerr << endl;

  int ans = 0;
  rep(i, n){
    if(dao.at(i) > dta.at(i)){
      ans = max(ans, dao.at(i)-1);
    }
  }
  cout << ans << endl;

  return 0;
}
#include <bits/stdc++.h>
#define mod 1000000007
#define sp ' '
#define intmax 2147483647
#define llmax 9223372036854775807
#define nyan "(=^･ω･^=)"
#define mkp make_pair
#define lP pair<ll, ll>
#define iP pair<int,int>
typedef long long ll;
using namespace std;

int N, M, K, A, B, C, D[100000], vis[100000];
map<int, vector<pair<int,bool>>>v[100000];
queue<int>q;


void dfs(int x, int a, int t) {
	vector<pair<int,bool>>&V = v[x][a];
	for (int i = 0; i < V.size(); ++i) {
		if (!V[i].second) {
			V[i].second = true;
			if (D[V[i].first] == t + 1 && vis[V[i].first] != a) {
				vis[V[i].first] = a;
				dfs(V[i].first, a, t);
			}
			if (D[V[i].first] == -1) {
				D[V[i].first] = t + 1;
				q.push(V[i].first);
				dfs(V[i].first, a, t);
			}
		}
	}
}

int main() {
	cin >> N >> M;
	while (M--) {
		cin >> A >> B >> C;
		--A;
		--B;
		v[A][C].push_back(mkp(B, false));
		v[B][C].push_back(mkp(A, false));
	}
	memset(D, -1, sizeof D);
	D[0] = 0;
	q.push(0);
	while (!q.empty()) {
		int x = q.front();
		q.pop();
		for (auto it = v[x].begin(); it != v[x].end(); ++it) {
			dfs(x, it->first, D[x]);
		}
	}
	cout << D[N - 1] << endl;
}
