We have a tree with N vertices. The i-th edge connects Vertex A_i and B_i bidirectionally.
Takahashi is standing at Vertex u, and Aoki is standing at Vertex v.
Now, they will play a game of tag as follows:
 - 1. If Takahashi and Aoki are standing at the same vertex, the game ends. Otherwise, Takahashi moves to a vertex of his choice that is adjacent to his current vertex.
 - 2. If Takahashi and Aoki are standing at the same vertex, the game ends. Otherwise, Aoki moves to a vertex of his choice that is adjacent to his current vertex.
 - 3. Go back to step 1.
Takahashi performs his moves so that the game ends as late as possible, while Aoki performs his moves so that the game ends as early as possible.
Find the number of moves Aoki will perform before the end of the game if both Takahashi and Aoki know each other's position and strategy.
It can be proved that the game is bound to end.

-----Constraints-----
 - 2 \leq N \leq 10^5
 - 1 \leq u,v \leq N
 - u \neq v
 - 1 \leq A_i,B_i \leq N
 - The given graph is a tree.

-----Input-----
Input is given from Standard Input in the following format:
N u v
A_1 B_1
:
A_{N-1} B_{N-1}

-----Output-----
Print the number of moves Aoki will perform before the end of the game.

-----Sample Input-----
5 4 1
1 2
2 3
3 4
3 5

-----Sample Output-----
2

If both players play optimally, the game will progress as follows:
 - Takahashi moves to Vertex 3.
 - Aoki moves to Vertex 2.
 - Takahashi moves to Vertex 5.
 - Aoki moves to Vertex 3.
 - Takahashi moves to Vertex 3.
Here, Aoki performs two moves.
Note that, in each move, it is prohibited to stay at the current vertex.

Difficulty: interview

Starter code:

Solutions:
import sys
from collections import deque


def main():
    N, p, q, *AB = list(map(int, sys.stdin.buffer.read().split()))
    p -= 1
    q -= 1

    G = [[] for _ in range(N)]
    for a, b in zip(*[iter(AB)] * 2):
        G[a - 1].append(b - 1)
        G[b - 1].append(a - 1)

    if len(G[p]) == 1 and G[p][0] == q:
        print((0))
        return

    dist1 = [-1] * N
    dist1[p] = 0
    queue = deque([p])
    while queue:
        v = queue.popleft()
        for nv in G[v]:
            if dist1[nv] == -1:
                dist1[nv] = dist1[v] + 1
                queue.append(nv)

    dist2 = [-1] * N
    dist2[q] = 0
    queue = deque([q])
    while queue:
        v = queue.popleft()
        for nv in G[v]:
            if dist2[nv] == -1:
                dist2[nv] = dist2[v] + 1
                queue.append(nv)

    max_d = 0
    for d1, d2 in zip(dist1, dist2):
        if d1 < d2 and max_d < d2:
            max_d = d2

    print((max_d - 1))
    return


def __starting_point():
    main()

__starting_point()

ma = lambda :list(map(int,input().split()))
n,u,v = ma()
u,v = u-1,v-1
tree = [[] for i in range(n)]
import collections

for i in range(n-1):
    a,b = ma()
    tree[a-1].append(b-1)
    tree[b-1].append(a-1)
que = collections.deque([(v,0)])
vis = [False]*n
dist_v = [0]*n
while que:
    now,c = que.popleft()
    vis[now] = True
    dist_v[now] = c
    for node in tree[now]:
        if not vis[node]:
            que.append((node,c+1))
que = collections.deque([(u,0)])
vis = [False]*n
dist_u = [0]*n
while que:
    now,c = que.popleft()
    vis[now] = True
    dist_u[now] = c
    for node in tree[now]:
        if not vis[node]:
            que.append((node,c+1))
ans = 0
for i in range(n):
    if dist_u[i] < dist_v[i]:
        ans = max(ans,dist_v[i])
print((ans-1))


# AtCoder Beginner Contest 148
# F - Playing Tag on Tree
# https://atcoder.jp/contests/abc148/tasks/abc148_f
from collections import deque

import sys
input = sys.stdin.readline
N, U, V = list(map(int, input().split()))
U -= 1
V -= 1
G = [[] for _ in [0]*N]
for _ in [0]*(N-1):
    a, b = list(map(int, input().split()))
    a -= 1
    b -= 1
    G[a].append(b)
    G[b].append(a)

INF = 10**10

T = [INF]*N
T[U] = 0
A = [INF]*N
A[V] = 0


def bfs(v, dist):
    q = deque()
    q.append(v)
    k = 0
    visited = [0]*N
    while q:
        v = q.popleft()
        visited[v] = 1
        k = dist[v]

        for u in G[v]:
            if visited[u]:
                continue
            q.append(u)
            dist[u] = k+1


bfs(U, T)
bfs(V, A)

if A[U] <= 1:
    print((0))
    return

farthest = 0
node = -1
for i, (t, a) in enumerate(zip(T, A)):
    if t < a and farthest < a:
        farthest = a
        node = i

print((farthest-1))


import sys 
sys.setrecursionlimit(10**6)
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
N,u,v = list(map(int,readline().split()))
AB = [[] for _ in range(N+1)]
for _ in range(N-1):
    a,b = list(map(int,readline().split()))
    AB[a].append(b)
    AB[b].append(a)
    
tdist = [0]*(N+1)
adist = [0]*(N+1)

def tdfs(x,pre):
    for to in AB[x]: 
        if to == pre:
            continue
        tdist[to] = tdist[x]+1
        tdfs(to,x)

def adfs(x,pre):
    for to in AB[x]: 
        if to == pre:
            continue
        adist[to] = adist[x]+1
        adfs(to,x)

tdfs(u,-1)
adfs(v,-1)

ans = 0
for i in range(1,N+1):
    if tdist[i] < adist[i]:
        k = adist[i] - tdist[i]
        ans = max(ans,tdist[i] + k-1)
print(ans)


import sys
sys.setrecursionlimit(10 ** 9)

n, u, v = list(map(int, input().split()))
u -= 1
v -= 1
ab = [[] for _ in range(n)]
for _ in range(n - 1):
    a, b = list(map(int, input().split()))
    ab[a - 1].append(b - 1)
    ab[b - 1].append(a - 1)

def dfs1(pos, before_pos):
    for next_pos in ab[pos]:
        if next_pos == before_pos:
            continue
        if next_pos == u:
            return True, [pos]
        tf, root = dfs1(next_pos, pos)
        if tf:
            root.append(pos)
            return True, root
    return False, []
    
root = dfs1(v, -1)[1]
root = root[::-1]
root.append(u)
n = (len(root) + 1) // 2
pos = root[n]
before_pos = root[n - 1]

max_len = n

def dfs2(pos, before_pos, cnt):
    nonlocal max_len
    cnt += 1
    max_len = max(max_len, cnt)
    if cnt < n - 1:
        dfs2(root[cnt + 1], pos, cnt)
    else:
        for next_pos in ab[pos]:
            if next_pos == before_pos:
                continue
            dfs2(next_pos, pos, cnt)


dfs2(pos, before_pos, n - 1)
ans = max_len - 1
ans = max(ans, 0)
print(ans)


import sys
sys.setrecursionlimit(10000000)
import math
import bisect
def input():
    return sys.stdin.readline()[:-1]

n,u,v = list(map(int,input().split()))
u-=1
v-=1
g = [[]for i in range(n)]
for i in range(n-1):
    a,b = list(map(int,input().split()))
    a-=1
    b-=1
    g[a].append(b)
    g[b].append(a)
seen = [-1 for i in range(n)]
taka = []
aok = []
def dfs(s,lis):
    nec = g[s]
    if len(nec)==1:
        lis.append(s)
    for i in nec:
        if seen[i]!=-1:
            continue
        seen[i] = seen[s]+1
        dfs(i,lis)

seen[u]=0
dfs(u,taka)
takadist = [i for i in seen]
seen = [-1 for i in range(n)]
seen[v]=0
dfs(v,aok)
ans = 0
for i in aok:
    if takadist[i]<seen[i]:
        ans = max(ans,seen[i])
if g[u]==[v]:
    print((0))
else:
    print((ans-1))


from sys import stdin
nii=lambda:map(int,stdin.readline().split())
lnii=lambda:list(map(int,stdin.readline().split()))
from collections import deque

n,u,v=nii()
u-=1
v-=1

tree=[[] for i in range(n)]
for i in range(n-1):
  a,b=nii()
  a-=1
  b-=1
  tree[a].append(b)
  tree[b].append(a)

dist_a=[-1 for i in range(n)]
dist_a[v]=0

dist_t=[-1 for i in range(n)]
dist_t[u]=0


def BFS(dist,s):
  que=deque()
  que.append(s)

  while que:
    x=que.popleft()
    for i in tree[x]:
      if dist[i]==-1:
        que.append(i)
        dist[i]=dist[x]+1
  return dist

dist_a=BFS(dist_a,v)
dist_t=BFS(dist_t,u)

ans=0
for i in range(n):
  if dist_a[i]>dist_t[i]:
    ans=max(ans,dist_a[i]-1)

print(ans)

f=lambda:map(int,input().split())
n,st,sa=f()
st-=1
sa-=1
g=[[] for _ in range(n)]
for _ in range(n-1):
  a,b=f()
  g[a-1].append(b-1)
  g[b-1].append(a-1)
def bfs(s):
  l=[-1]*n; l[s]=0; q=[s]
  while q:
    v=q.pop(); d=l[v]+1
    for c in g[v]:
      if l[c]<0: l[c]=d; q+=[c]
  return l
lt=bfs(st)
la=bfs(sa)
m=0
for i in range(n):
  if lt[i]<la[i]: m=max(m,la[i])
print(m-1)

import sys
sys.setrecursionlimit(10**6)
n, a, b = list(map(int, input().split()))
D = [[0]*2 for _ in range(101010)]
E = [[] for _ in range(n)]


def dfs(cur, pre, d, id):
    D[cur][id] = d
    for nv in E[cur]:
        if nv != pre:
            dfs(nv, cur, d+1, id)


a -= 1
b -= 1
for i in range(n-1):
    x, y = list(map(int, input().split()))
    x -= 1
    y -= 1
    E[x].append(y)
    E[y].append(x)
dfs(a, a, 0, 0)
dfs(b, b, 0, 1)
max_d = 0
for i in range(n):
    if D[i][0] < D[i][1]:
        max_d = max(max_d, D[i][1]-1)
print(max_d)


import sys

sys.setrecursionlimit(10**5)
n, u, v = list(map(int, input().split()))
u -= 1
v -= 1
m_mat = [[] for i in range(n)]
for _ in range(n-1):
    a, b = list(map(int, input().split()))
    a -= 1
    b -= 1
    m_mat[a].append(b)
    m_mat[b].append(a)
u_map = [-1]*n
v_map = [-1]*n
u_map[u] = 0
v_map[v] = 0


def dfs(current, depth, ma):
    for nex in m_mat[current]:
        if ma[nex] > -1:
            continue
        ma[nex] = depth
        dfs(nex, depth+1, ma)


dfs(u, 1, u_map)
dfs(v, 1, v_map)

ans = -1
for i in range(n):
    if u_map[i] < v_map[i] and v_map[i] > ans:
        ans = v_map[i]

print((ans-1))


import sys
sys.setrecursionlimit(10**5)
input = sys.stdin.readline

N, u, v = map(int, input().split())
u -= 1
v -= 1
Tree = [[] for _ in range(N)]

for _ in range(N-1):
  A, B = map(lambda x:int(x)-1, input().split())
  Tree[A].append(B)
  Tree[B].append(A)

#u-v間の単純pathを用意する
path = []
def connected(v, tv, p=-1):
  if v == tv:
    return True
  for w in Tree[v]:
    if w == p:
      continue
    elif connected(w, tv, v):
      path.append(w)
      return True
  return False

connected(v, u)
path.append(v)

#u-v間の点からvとは逆方向にdfs
#u-vの中点から調べれば十分(中点よりvに寄ると、青木君は捕まってしまう)

def dfs(v, p):
  d = -1
  for w in Tree[v]:
    if w == p:
      continue
    else:
      d = max(d, dfs(w, v))
  return d + 1

dist = len(path)
mid = path[dist//2 - 1]
par = path[dist//2]
ans = dfs(mid, par)

print(ans + (dist-1)//2)

from collections import deque, defaultdict
from copy import deepcopy
import sys
import resource

sys.setrecursionlimit(10**5)

N ,u, v = list(map(int,input().split()))
graph = defaultdict(deque)
for _ in range(1,N):
    a, b = list(map(int,input().split()))
    graph[a].append(b)
    graph[b].append(a)

depth = dict(list(zip(list(range(1,N+1)),[-1]*N)))

def bfs(s):
    if graph[s]:
        for v in graph[s]:
            if depth[v] == -1:
                depth[v] = depth[s] + 1
                bfs(v)

depth = dict(list(zip(list(range(1,N+1)),[-1]*N)))
depth[v] = 0
bfs(v)
depth_v = deepcopy(depth)

depth = dict(list(zip(list(range(1,N+1)),[-1]*N)))
depth[u] = 0
bfs(u)
depth_u = depth
depth_v = sorted(list(depth_v.items()), key = lambda x:x[0])
depth_u = sorted(list(depth_u.items()), key= lambda x:x[0])

MAX = 0
for v,u in zip(depth_v, depth_u):
    if u[1] < v[1]:
        MAX = max(MAX,v[1])
print((MAX-1))


from collections import deque
n,u,v=list(map(int,input().split()))
g=[set([]) for _ in range(n+1)]
for i in range(n-1):
    a,b=list(map(int,input().split()))
    g[a].add(b)
    g[b].add(a)

leaf=[]
for i in range(1,n+1):
    if(len(g[i])==1):
        leaf.append(i)

d_u=[-1 for _ in range(n+1)]
d_v=[-1 for _ in range(n+1)]

def bfs(start,d):
    d[start]=0
    q=deque([start])

    while(len(q)>0):
        qi=q.popleft()
        di=d[qi]

        next_qi=g[qi]
        for i in next_qi:
            if(d[i]==-1):
                d[i]=di+1
                q.append(i)

    return d


d_u=bfs(u, d_u)
d_v=bfs(v, d_v)


if(u in leaf and list(g[u])[0]==v):
    print((0))
else:
    ans=0
    for li in leaf:
        if(d_u[li]<d_v[li]):
            ans=max(ans,d_v[li]-1)
    print(ans)


import sys
sys.setrecursionlimit(10**6)
n, u, v = map(int, input().split())
u -= 1
v -= 1
graph = [[] for _ in range(n)]
for _ in range(n-1):
    a, b = map(int, input().split())
    graph[a-1].append(b-1)
    graph[b-1].append(a-1)
d1 = [None]*n
d2 = [None]*n
def dfs(node, d, p_node=-1):
    for c_node in graph[node]:
        if c_node==p_node:
            continue
        d[c_node] = d[node] + 1
        dfs(c_node, d, node)
d1[u] = 0
d2[v] = 0
dfs(u, d1)
dfs(v, d2)
ans = 0
for i, j in zip(d1, d2):
    if i<j:
        ans = max(ans, j-1)
print(ans)

def resolve():
    N, u, v = list(map(int, input().split()))
    u, v = u-1, v-1
    adjs = [ [] for _ in range(N)]
    for _ in range(N-1):
        a, b = list([int(x)-1 for x in input().split()])
        adjs[a].append(b)
        adjs[b].append(a)
    dist_from_u = [float("inf") for _ in range(N)]
    dist_from_u[u] = 0
    import collections
    q = collections.deque([u])
    while q:
        node = q.pop()
        for adj in adjs[node]:
            if dist_from_u[adj] > dist_from_u[node]+1:
                dist_from_u[adj] = dist_from_u[node]+1
                q.appendleft(adj)
    #print(dist_from_u)
    dist_from_v = [float("inf") for _ in range(N)]
    dist_from_v[v] = 0
    import collections
    q = collections.deque([v])
    while q:
        node = q.pop()
        for adj in adjs[node]:
            if dist_from_v[adj] > dist_from_v[node]+1:
                dist_from_v[adj] = dist_from_v[node]+1
                q.appendleft(adj)
    #print(dist_from_v)
    length = 0
    for i in range(N):
        if dist_from_u[i] < dist_from_v[i] and length < dist_from_v[i]:
            length = dist_from_v[i]
    print((length-1))
    
def __starting_point():
    resolve()


__starting_point()

import sys
sys.setrecursionlimit(200000)

N, s, t = map(int, input().split())
s -= 1; t -= 1
to = [[] for _ in range(N)]
for i in range(N-1):
    a, b = map(int, input().split())
    a -= 1; b -= 1
    to[a].append(b)
    to[b].append(a)

def calcDist(u):
    dfs(u)
    return dist

def dfs(v, d=0, p=-1):
    dist[v] = d
    for u in to[v]:
        if u == p:
            continue
        dfs(u, d+1, v)

dist = [0 for _ in range(N)]
distS = calcDist(s)
dist = [0 for _ in range(N)]
distT = calcDist(t)

ans = 0
for i in range(N):
    if distS[i] < distT[i]:
        ans = max(ans, distT[i])

print(ans-1)

#!/usr/bin/env python3
import sys

sys.setrecursionlimit(10 ** 5 + 1)


def dfsu(s, p):
    for t in g[s]:
        if t == p:
            continue
        du[t] = du[s] + 1
        dfsu(t, s)


def dfsv(s, p):
    for t in g[s]:
        if t == p:
            continue
        dv[t] = dv[s] + 1
        dfsv(t, s)


n, u, v = list(map(int, input().split()))
u -= 1
v -= 1
g = [[] for _ in range(n)]
for _ in range(n - 1):
    a, b = [int(x) - 1 for x in input().split()]
    g[a].append(b)
    g[b].append(a)

du = [0] * n
dv = [0] * n
dfsu(u, -1)
dfsv(v, -1)

mx = 0
for i in range(n):
    if du[i] < dv[i]:
        mx = max(mx, dv[i])
print((mx - 1))


from collections import deque
import sys
sys.setrecursionlimit(10**7)
N,u,v=map(int, input().split())
D=[[] for i in range(N)]
for i in range(N-1):
  a,b=map(int, input().split())
  a-=1
  b-=1
  D[a].append(b)
  D[b].append(a)
Taka=[0]*(N+1)
Aoki=[0]*(N+1)
def f(n,p,A):
  for i in D[n]:
    if i!=p:
      A[i]=A[n]+1
      f(i,n,A)
      
f(u-1,-1,Taka)
f(v-1,-1,Aoki)
ans=0
for i in range(N):
  if Taka[i]<Aoki[i]:
    ans=max(ans,Aoki[i]-1)
print(ans)

import sys
input = sys.stdin.readline

N,u,v=map(int,input().split())
E=[tuple(map(int,input().split())) for i in range(N-1)]

EDGE=[[] for i in range(N+1)]

for x,y in E:
    EDGE[x].append(y)
    EDGE[y].append(x)

from collections import deque
T=[-1]*(N+1)

Q=deque()
Q.append(u)
T[u]=0

while Q:
    x=Q.pop()
    for to in EDGE[x]:
        if T[to]==-1:
            T[to]=T[x]+1
            Q.append(to)

A=[-1]*(N+1)

Q=deque()
Q.append(v)
A[v]=0

while Q:
    x=Q.pop()
    for to in EDGE[x]:
        if A[to]==-1:
            A[to]=A[x]+1
            Q.append(to)


OK=[0]*(N+1)

for i in range(N+1):
    if T[i]<A[i]:
        OK[i]=1

ANS=0
for i in range(N+1):
    if OK[i]==1:
        ANS=max(ANS,A[i])

print(max(0,ANS-1))

from sys import stdin
nii=lambda:map(int,stdin.readline().split())
lnii=lambda:list(map(int,stdin.readline().split()))
from collections import deque

n,u,v=nii()
u-=1
v-=1

tree=[[] for i in range(n)]
for i in range(n-1):
  a,b=nii()
  a-=1
  b-=1
  tree[a].append(b)
  tree[b].append(a)

def BFS(s):
  dist=[-1 for i in range(n)]
  dist[s]=0

  que=deque()
  que.append(s)

  while que:
    x=que.popleft()
    for i in tree[x]:
      if dist[i]==-1:
        que.append(i)
        dist[i]=dist[x]+1
  return dist

dist_t=BFS(u)
dist_a=BFS(v)

ans=0
for i in range(n):
  if dist_a[i]>dist_t[i]:
    ans=max(ans,dist_a[i]-1)

print(ans)

n, u, v = map(int, input().split())
u, v = u-1, v-1

tree = [[] for _ in range(n)]
for _ in range(n-1):
    a, b = map(int, input().split())
    tree[a-1].append(b-1)
    tree[b-1].append(a-1)

from queue import Queue

# 青木君によるWFS
aoki = [-1 for _ in range(n)]
aoki[v] = 0
todo = Queue()
todo.put(v)
while not todo.empty():
    i = todo.get()
    for j in tree[i]:
        if aoki[j] == -1:
            todo.put(j)
            aoki[j] = aoki[i] + 1

# 高橋君によるWFS
taka = [-1 for _ in range(n)]
taka[u] = 0
todo = Queue()
todo.put(u)
while not todo.empty():
    i = todo.get()
    for j in tree[i]:
        if taka[j] == -1:
            if taka[i] + 1 < aoki[j]:
                todo.put(j)
            taka[j] = taka[i] + 1

# search
ans = 0
for i in range(n):
    if taka[i] != -1:
        if aoki[i] - 1 > ans:
            ans = aoki[i] - 1

print(ans)

import sys
# import re
import math
import collections
# import decimal
import bisect
import itertools
import fractions
# import functools
import copy
# import heapq
import decimal
# import statistics
import queue

# import numpy as np

sys.setrecursionlimit(10000001)
INF = 10 ** 16
MOD = 10 ** 9 + 7
# MOD = 998244353

ni = lambda: int(sys.stdin.readline())
ns = lambda: list(map(int, sys.stdin.readline().split()))
na = lambda: list(map(int, sys.stdin.readline().split()))


# ===CODE===

def main():
    n, u, v = ns()
    edge = [[] for _ in range(n)]

    for _ in range(n - 1):
        a, b = ns()
        a -= 1
        b -= 1
        edge[a].append(b)
        edge[b].append(a)

    dist_takahashi = [-1] * n
    dist_aoki = [-1] * n

    def bfs(pos, dist):
        que = queue.Queue()
        que.put(pos)
        dist[pos] = 0

        while not que.empty():
            current = que.get()
            for e in edge[current]:
                if dist[e] == -1:
                    dist[e] = dist[current] + 1
                    que.put(e)

        return dist

    dist_takahashi = bfs(u - 1, dist_takahashi)
    dist_aoki = bfs(v - 1, dist_aoki)

    ans = 0
    for dt, da in zip(dist_takahashi, dist_aoki):
        if dt < da:
            ans = max(ans, da - 1)

    print(ans)


def __starting_point():
    main()

__starting_point()

N, u, v = map(int, input().split())
AB = [list(map(int, input().split())) for _ in range(N - 1)]

graph = [[] for _ in range(N)]
for i, (Ai, Bi) in enumerate(AB):
    graph[Ai - 1].append(Bi - 1)
    graph[Bi - 1].append(Ai - 1)

import heapq
MY_INF = 10 ** 18

q = [(0, u - 1)]
d_u = [MY_INF] * N
d_u[u - 1] = 0
while len(q) > 0:
    dist, src = heapq.heappop(q)
    for dst in graph[src]:
        dist_tmp = dist + 1
        if d_u[dst] > dist_tmp:
            d_u[dst] = dist_tmp
            heapq.heappush(q, (dist_tmp, dst))

q = [(0, v - 1)]
d_v = [MY_INF] * N
d_v[v - 1] = 0
while len(q) > 0:
    dist, src = heapq.heappop(q)
    for dst in graph[src]:
        dist_tmp = dist + 1
        if d_v[dst] > dist_tmp:
            d_v[dst] = dist_tmp
            heapq.heappush(q, (dist_tmp, dst))

# print("#", d_u)
# print("#", d_v)
            
ans = 0
for i in range(N):
    if d_u[i] < d_v[i] and ans < d_v[i] - 1:
        ans = d_v[i] - 1

print(ans)

import sys
from collections import deque

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

in_n = lambda: int(readline())
in_nn = lambda: list(map(int, readline().split()))
in_s = lambda: readline().rstrip().decode('utf-8')
in_nl = lambda: list(map(int, readline().split()))
in_nl2 = lambda H: [in_nl() for _ in range(H)]
in_map = lambda: [s == ord('.') for s in readline() if s != ord('\n')]
in_map2 = lambda H: [in_map() for _ in range(H)]
in_all = lambda: list(map(int, read().split()))


def bfs(N, v0, edge):

    search = [-1] * N
    search[v0] = 0
    q = deque()
    q.append(v0)

    while q:
        v = q.popleft()
        for nv in edge[v]:
            if search[nv] == -1:
                q.append(nv)
                search[nv] = search[v] + 1

    return search


def bfs2(N, v0, edge, dis):

    q = deque()
    q.append(v0)

    max_dis = dis[v0]
    while q:
        v = q.popleft()
        for nv in edge[v]:
            if dis[v] < dis[nv]:
                q.append(nv)
                max_dis = max(max_dis, dis[nv])

    return max_dis


def main():

    N, taka, aoki = in_nn()
    taka, aoki = taka - 1, aoki - 1

    edge = [[] for _ in range(N)]
    for i in range(N - 1):
        x, y = in_nn()
        x, y = x - 1, y - 1
        edge[x].append(y)
        edge[y].append(x)

    dis = bfs(N, aoki, edge)

    if dis[taka] > 2:
        x = (dis[taka] + 1) // 2 - 1
        for _ in range(x):
            for v in edge[taka]:
                if dis[v] < dis[taka]:
                    taka = v
                    break

    ans = bfs2(N, taka, edge, dis) - 1
    print(ans)


def __starting_point():
    main()

__starting_point()

import sys
sys.setrecursionlimit(10**7)

n,u,v=map(int, input().split())
u-=1
v-=1
 
graph = [[] for _ in range(n+1)]
for i in range(n-1):
    a, b=map(int, input().split())
    graph[a-1].append(b-1)
    graph[b-1].append(a-1)
 

dist=[[-1, -1] for _ in range(n)]
dist[v][0]=0
dist[u][1]=0
 
def dfs(graph,v,k):
    ver=graph[v]
    for vers in ver:
        if dist[vers][k]==-1:
            dist[vers][k]=dist[v][k]+1
            dfs(graph,vers,k)
 
 
dfs(graph,v,0)
dfs(graph,u,1)
dist.sort(reverse=True)
 
for i in range(10**6):
    if dist[i][0]-dist[i][1]>=1:
        print(dist[i][0]-1)
        break

from collections import deque
n, u, v = map(int,input().split())
tree = [[] for _ in range(n+1)]
for _i in range(n-1):
    a, b = map(int, input().split())
    tree[a].append(b)
    tree[b].append(a)


def solve(x):
    visit = [-1 for _ in range(n+1)]
    visit[x] = 0
    q = deque([x])
    while q:
        p = q.popleft()
        for i in tree[p]:
            if visit[i] < 0:
                visit[i] = visit[p]+1
                q.append(i)
    return visit

visit_a = solve(v)
visit_t = solve(u)

x, y = [], []
for i in range(1, n+1):
    if visit_a[i] >= visit_t[i]:
        x.append(visit_a[i])
        y.append(visit_t[i])
p = x.index(max(x))

print(x[p]-1)

import sys
input=sys.stdin.readline
sys.setrecursionlimit(10**6)
n,u,v=map(int,input().split())
node=[[]for _ in range(n)]
for _ in range(n-1):
	a,b=map(int,input().split())
	node[a-1].append(b-1)
	node[b-1].append(a-1)
def dfs(i):
	visited[i]=1
	for x in node[i]:
		if visited[x]==0:
			dis[x]=dis[i]+1
			dfs(x)
inf=10**9
dis=[inf]*n;dis[u-1]=0;visited=[0]*n
dfs(u-1)
dis2=[]
from copy import copy
dis_dash=copy(dis)
dis2.append(dis_dash)
dis[v-1]=0;visited=[0]*n
dfs(v-1)
dis2.append(dis)
cnt=0
for i in range(n):
	if dis2[0][i]<dis2[1][i]:
		cnt=max(cnt,dis2[1][i])
print(cnt-1)

from collections import deque
n, u, v = map(int, input().split())
u -= 1
v -= 1
edges = [[] for i in range(n)]
for i in range(n - 1):
    a, b = map(int, input().split())
    edges[a - 1].append(b - 1)
    edges[b - 1].append(a - 1)
def bfs(u):
    visited = [-1] * n
    Q = deque()
    visited[u] = 0
    Q.append(u)
    while Q:
        p = Q.popleft()
        for v in edges[p]:
            if visited[v] == -1:
                visited[v] = visited[p] + 1
                Q.append(v)
    return visited
tak_dist = bfs(u)
aok_dist = bfs(v)
ans = []
for i in range(n):
    if tak_dist[i] < aok_dist[i]:
        ans.append(aok_dist[i])
print(max(ans) - 1)

from collections import deque,defaultdict
import copy

def main():
    from collections import defaultdict
    import copy 
    n,u,v = map(int,input().split())
    u -= 1
    v -= 1
    graph = defaultdict(deque)
    for _ in range(n-1):
        a,b = map(int,input().split())
        a -= 1
        b -= 1
        graph[a].append(b)
        graph[b].append(a)

    depth = dfs(graph,u,n)
    # print(depth)
    depth2 = dfs(graph,v,n)
    # print(depth2)
    ans = 0
    ans2 = 10**6
    for i,j in zip(depth,depth2):
        if i < j:
            ans = max(j,ans)
        # elif i == j:
        #     ans2 = min(j,ans2)

    print(ans-1)

def dfs(G,s,n):
    stack = deque([s])
    graph = copy.deepcopy(G)
    depth = [None] * n
    depth[s] = 0
    while stack:
        u = stack[-1]
        if graph[u]:
            v = graph[u].popleft()
            if depth[v] == None:
                depth[v] = depth[u] + 1
                stack.append(v)

        else:
            stack.pop()

    return depth
    

main()


def resolve():
    def dfs(v):
        dist = [-1] * N
        stack = [v]
        dist[v] = 0
        while stack:
            v = stack.pop()
            for to in G[v]:
                if dist[to] != -1:
                    continue
                dist[to] = dist[v] + 1
                stack.append(to)
        return dist

    N, taka, aoki = map(int, input().split())
    taka -= 1
    aoki -= 1
    G = [[] for _ in range(N)]
    for i in range(N - 1):
        a, b = map(lambda x: int(x) - 1, input().split())
        G[a].append(b)
        G[b].append(a)

    dist_Tk = dfs(taka)
    dist_Ao = dfs(aoki)

    ans = 0
    for i in range(N):
        if dist_Tk[i] < dist_Ao[i]:
            ans = max(ans, dist_Ao[i] - 1)

    print(ans)


def __starting_point():
    resolve()
__starting_point()

from collections import deque
n,u,v=list(map(int, input().split()))
edgelist = [[] for i in range(n)]
for i in range(n-1):
    a,b = list(map(int, input().split()))
    edgelist[a-1].append(b-1)
    edgelist[b-1].append(a-1)
dist = [[0,0] for _ in range(n)]
def distance(start, id):
    que=deque()
    visited = [0]*n

    found = False
    que.append([edgelist[start],start])
    while que:
        ver=que.popleft()
        visited[ver[1]]=1
        for next_v in ver[0]:
            if not visited[next_v]:
                dist[next_v][id]=dist[ver[1]][id]+1
                que.append([edgelist[next_v], next_v])
distance(u-1,0)
distance(v-1,1)
dist.sort(key = lambda x:x[1],reverse = True)
ans=0
for i in range(n):
    if dist[i][1]-dist[i][0]>=1:
        print((dist[i][1]-1))
        return


import sys
from collections import deque

sys.setrecursionlimit(10 ** 7)
input = sys.stdin.readline
f_inf = float('inf')
mod = 10 ** 9 + 7


def resolve():
    def bfs(start):
        dist = [f_inf] * n
        dist[start] = 0
        que = deque([start])
        while que:
            v = que.popleft()
            for u in edge[v]:
                if dist[u] == f_inf:
                    dist[u] = dist[v] + 1
                    que.append(u)
        return dist

    n, u, v = list(map(int, input().split()))
    edge = [[] for _ in range(n)]
    for _ in range(n - 1):
        a, b = list(map(int, input().split()))
        edge[a - 1].append(b - 1)
        edge[b - 1].append(a - 1)

    distT = bfs(u - 1)
    distA = bfs(v - 1)

    res = 0
    for i in range(n):
        if distT[i] < distA[i]:
            res = max(res, distA[i])
    print((res - 1))


def __starting_point():
    resolve()

__starting_point()

import sys
sys.setrecursionlimit(10**7)

def dfs(v,p,d):
    dist[v]=d
    for nv in G[v]:
        if nv==p:
            continue
        dfs(nv,v,d+1)
def getSP(v):
    p=v
    if dist[v]==lim:
        return p
    for nv in G[v]:
        if dist[nv]<dist[v]:
            p=getSP(nv)
    return p
def depth(v,p,d):
    sdist[v]=d
    for nv in G[v]:
        if dist[nv]<dist[v]:
            continue
        depth(nv,v,d+1)
    
N,u,v=map(int,input().split())
u,v=u-1,v-1
G=[[] for i in range(N)]
for i in range(N-1):
    a,b=map(lambda x:int(x)-1,input().split())
    G[a].append(b)
    G[b].append(a)
dist=[-1]*N
dfs(v,-1,0)
lim=dist[u]-(dist[u]-1)//2
SP=getSP(u)
sdist=[-1]*N
depth(SP,-1,0)
D,tmp=-1,-1
for i in range(N):
    if sdist[i]>tmp:
        D=i
        tmp=sdist[i]
print(dist[D]-1)

from collections import deque
n,u,v=map(int,input().split())
edge=[[] for i in range(n)]
for _ in range(n-1):
    a,b=map(int,input().split())
    a-=1
    b-=1
    edge[a].append(b)
    edge[b].append(a)
visitedu=[-1]*n
visitedv=[-1]*n
def bfs(s,visited):
    q=deque([s])
    visited[s]=0
    while q:
        x=q.popleft()
        for nx in edge[x]:
            if visited[nx]==-1:
                visited[nx]=visited[x]+1
                q.append(nx)
    return
bfs(u-1,visitedu)
bfs(v-1,visitedv)
a=0
b=10**10
for i in range(n):
    if visitedu[i]<visitedv[i]:
        a=max(a,visitedv[i])
    elif visitedu[i]==visitedv[i]:
        b=min(b,visitedv[i])
#print(visitedv)
#print(visitedu)
if b==10**10:
    b=-1
if a<=b:
    print(b)
else:
    print(a-1)

import sys
sys.setrecursionlimit(10**7)

def dfs(v,p,d):
    dist[v]=d
    for nv in G[v]:
        if nv==p:
            continue
        dfs(nv,v,d+1)
        
def getSP(v):
    p=v
    if dist[v]==lim:
        return p
    for nv in G[v]:
        if dist[nv]<dist[v]:
            p=getSP(nv)
    return p
    
def depth(v,p,d):
    nonlocal maxd
    maxd=max(maxd,dist[v])
    for nv in G[v]:
        if dist[nv]<dist[v]:
            continue
        depth(nv,v,d+1)
    
N,u,v=map(int,input().split());u,v=u-1,v-1

G=[[] for i in range(N)]
for i in range(N-1):
    a,b=map(lambda x:int(x)-1,input().split())
    G[a].append(b)
    G[b].append(a)
    
dist=[-1]*N
dfs(v,-1,0)

lim=dist[u]-(dist[u]-1)//2
SP=getSP(u)
 
maxd=-1
depth(SP,-1,0)

print(maxd-1)

from collections import defaultdict as d
from collections import deque

def bfs(adj, start, n):
	visited = [0] * (n + 1)
	tab = [0] * n
	q = deque([start])
	visited[start] = 1
	while q:
		s = q.popleft()
		for i in adj[s]:
			if visited[i] == 0:
				q.append(i)
				visited[i] = 1
				tab[i - 1] = tab[s - 1] + 1
	return tab

n, u, v = map(int, input().split())
adj = d(list)
for i in range(n - 1):
	a, b = map(int, input().split())
	adj[a].append(b)
	adj[b].append(a)

takahashi = bfs(adj, u, n)
aoki = bfs(adj, v, n)
res = -1
for i in range(n):
	if takahashi[i] <= aoki[i]:
		res = max(res, aoki[i] - 1)

print(res)

n,u,v=map(int,input().split())
tree=[[]for _ in range(n)]
for i in range(n-1):
    a,b = map(int,input().split())
    tree[a-1].append(b-1)
    tree[b-1].append(a-1)
va=[-1]*n
q=[v-1]
va[v-1]=0
while q:
    x=q.pop()
    for j in tree[x]:
        if va[j]==-1:
            q.append(j)
            va[j]=va[x]+1
q=[u-1]
vt=[-1]*n
vt[u-1]=0
ans=0
while q:
    x=q.pop()
    ans=max(ans,va[x])
    for j in tree[x]:
        if vt[j]==-1:
            if vt[x]+1<va[j]:
                q.append(j)
                vt[j]=vt[x]+1
            elif vt[x]+1==va[j]:
                vt[j]=vt[x]+1
print(ans-1)

import sys
sys.setrecursionlimit(10**7)


n,u,v=list(map(int, input().split()))
u-=1
v-=1

graph = [[] for _ in range(n+1)]
for i in range(n-1):
    a, b=list(map(int, input().split()))
    graph[a-1].append(b-1)
    graph[b-1].append(a-1)

# ab=[list(map(int, input().split())) for _ in range(n-1)]
# for a,b in ab:
#     graph[a-1].append(b-1)
#     graph[b-1].append(a-1)

dist=[[-1, -1] for _ in range(n)]
dist[v][0]=0
dist[u][1]=0

def dfs1(graph,v):
    vv=graph[v]
    for vvv in vv:
        if dist[vvv][0]==-1:
            dist[vvv][0]=dist[v][0]+1
            dfs1(graph,vvv)

def dfs2(graph,v):
    vv=graph[v]
    for vvv in vv:
        if dist[vvv][1]==-1:
            dist[vvv][1]=dist[v][1]+1
            dfs2(graph,vvv)

dfs1(graph,v)
dfs2(graph,u)
dist.sort(reverse=True)

for i in range(10**6):
    if dist[i][0]-dist[i][1]>=1:
        print((dist[i][0]-1))
        break


import sys
from collections import deque

sys.setrecursionlimit(10 ** 7)
input = sys.stdin.readline
f_inf = float('inf')
mod = 10 ** 9 + 7


def resolve():
    def bfs(start):
        dist = [f_inf] * n
        dist[start] = 0
        que = deque([start])
        while que:
            v = que.popleft()
            for u in edge[v]:
                if dist[u] == f_inf:
                    dist[u] = dist[v] + 1
                    que.append(u)
        return dist

    n, u, v = list(map(int, input().split()))
    edge = [[] for _ in range(n)]
    for _ in range(n - 1):
        a, b = list(map(int, input().split()))
        edge[a - 1].append(b - 1)
        edge[b - 1].append(a - 1)

    distA = bfs(u - 1)
    distT = bfs(v - 1)

    res = 0
    for i in range(n):
        if distA[i] < distT[i]:
            res = max(res, distT[i])
    print((res - 1))


def __starting_point():
    resolve()

__starting_point()

import sys
from collections import deque
sys.setrecursionlimit(10**5)
def input(): return sys.stdin.readline().strip()

"""
Pypyは再帰が遅いらしいのでPythonで通してみる
"""

def main():
    N, u, v = list(map(int, input().split()))
    u -= 1
    v -= 1
    repn = [[] for _ in range(N)]
    for i in range(N - 1):
        a, b = list(map(int, input().split()))
        repn[a - 1].append((b - 1, i))
        repn[b - 1].append((a - 1, i)) # (to, id)

    """
    まずu, vを結ぶ単純パス上にあるnodeを列挙する。
    これはvを根とする根付き木に対するdfs（再帰）により求まる（参考：ABC152F）
    """

    path = []
    def connected(v, tv, p=-1):
        if v == tv:
            return True
        for w, _ in repn[v]:
            if w == p:
                continue
            elif connected(w, tv, v):
                path.append(w)
                return True
        return False
    connected(v, u)
    path.append(v)
    #print("path={}".format(path))

    """
    次にu, vを結ぶ単純パスP上の各頂点wに対して、wからPに沿わない向きにどれだけ深く潜れるかを求める。
    しかし最終的には「頂点vから（高橋君が逃げ込める中で）最も遠い頂点」を求めればよいので、
    uvの中点からdfsすれば十分。
    """

    def dfs(v, p): # Pと異なる頂点方向にdfsして深さを求める
        d = -1
        for w, _ in repn[v]:
            if w == p:
                continue
            else:
                d = max(d, dfs(w, v))
        return d + 1
    
    dist = len(path)
    mid = path[dist // 2 - 1]
    par = path[dist // 2]
    ans = dfs(mid, par)
    print((ans + (dist - 1) // 2))
   


def __starting_point():
    main()

__starting_point()

from collections import deque
 
N,u,v=map(int,input().split())
G=[[] for _ in range(N)]
for _ in range(N-1):
  a,b = map(int,input().split())
  G[a-1].append(b-1)
  G[b-1].append(a-1)
 
def bfs(s):
    seen = [0]*N
    d = [0]*N
    todo = deque()
    seen[s]=1
    todo.append(s)
    while len(todo):
      a = todo.popleft()
      for b in G[a]:
        if seen[b] == 0:
          seen[b] = 1
          todo.append(b)
          d[b] += d[a] + 1
    return d
  
d1 = bfs(u-1)
d2 = bfs(v-1)
 
print(max([d2[i] for i in range(N) if d1[i]<=d2[i]])-1)

import os
import sys
import numpy as np

def solve(N, U, V, AB):
    G = [[] for _ in range(N+1)]
    for ab in AB:
        a, b = ab
        G[a].append(b)
        G[b].append(a)
    P1 = np.zeros(N+1, dtype=np.int64)
    def dfs1(u):
        st = [u]
        while st:
            v = st.pop()
            p = P1[v]
            for u in G[v]:
                if p != u:
                    st.append(u)
                    P1[u] = v
    dfs1(U)
    path_u2v = [U]
    v = V
    while v != U:
        v = P1[v]
        path_u2v.append(v)
    path_u2v.reverse()
    l = len(path_u2v)
    half = (l-2) // 2
    c = path_u2v[half]
    ng = path_u2v[half+1]
    Depth = np.zeros(N+1, dtype=np.int64)
    def dfs2(c):
        st = [c]
        P = np.zeros(N+1, dtype=np.int64)
        while st:
            v = st.pop()
            p = P[v]
            d = Depth[v]
            for u in G[v]:
                if p != u and u != ng:
                    st.append(u)
                    P[u] = v
                    Depth[u] = d + 1
    dfs2(c)
    c_ = path_u2v[l-1-half]
    v = c_
    while v != c:
        Depth[v] = 0
        v = P1[v]
    d = l%2
    ans = max(Depth) + half + d
    return ans

def main():
    N, u, v = list(map(int, input().split()))
    if N==2:
        print((0))
        return
    AB = np.array(sys.stdin.read().split(), dtype=np.int64).reshape(N-1, 2)
    ans = solve(N, u, v, AB)
    print(ans)

main()


import sys
def input():
    return sys.stdin.readline().strip()

n, u, v = map(int, input().split())
u, v = u-1, v-1

tree = [[] for _ in range(n)]
for _ in range(n-1):
    a, b = map(int, input().split())
    tree[a-1].append(b-1)
    tree[b-1].append(a-1)

from queue import Queue

# 青木君によるWFS
aoki = [-1 for _ in range(n)]
aoki[v] = 0
todo = Queue()
todo.put(v)
while not todo.empty():
    i = todo.get()
    for j in tree[i]:
        if aoki[j] == -1:
            todo.put(j)
            aoki[j] = aoki[i] + 1

# 高橋君によるWFS
taka = [-1 for _ in range(n)]
taka[u] = 0
todo = Queue()
todo.put(u)
while not todo.empty():
    i = todo.get()
    for j in tree[i]:
        if taka[j] == -1:
            if taka[i] + 1 < aoki[j]:
                todo.put(j)
            taka[j] = taka[i] + 1

# search
ans = 0
for i in range(n):
    if taka[i] != -1:
        if aoki[i] - 1 > ans:
            ans = aoki[i] - 1

print(ans)

import sys
from collections import defaultdict

sys.setrecursionlimit(10**7)

n, u, v = map(int, input().split())

graph = defaultdict(list)

for _ in range(n - 1):
    a, b = map(int, input().split())
    graph[a].append(b)
    graph[b].append(a)


def dfs(u):
    dist = [-1] * (n + 1)
    stack = [u]
    dist[u] = 0
    while stack:
        u = stack.pop()
        nxt = dist[u] + 1
        for v in graph[u]:
            if dist[v] >= 0:
                continue
            dist[v] = nxt
            stack.append(v)
    return dist


dist_u = dfs(u)
dist_v = dfs(v)

# 高橋くん（u）の方が近い頂点のうち、青木くん（v）から最も遠い頂点の一つ手前
ans = 0
for x, y in zip(dist_u, dist_v):
    if x < y:
        ans = max(ans, y - 1)

print(ans)

import sys
sys.setrecursionlimit(10**9)
f=lambda:map(int,input().split())
n,st,sa=f()
st-=1
sa-=1
g=[[] for _ in range(n)]
for _ in range(n-1):
  a,b=f()
  g[a-1].append(b-1)
  g[b-1].append(a-1)
def dfs(v,p=-1,d=0):
  l[v]=d
  for c in g[v]:
    if c==p: continue
    dfs(c,v,d+1)
def dist(s):
  nonlocal l
  l=[0]*n
  dfs(s)
  return l
lt=dist(st)
la=dist(sa)
m=0
for i in range(n):
  if lt[i]<la[i]: m=max(m,la[i])
print(m-1)

import sys
from collections import deque

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60
MOD = 1000000007


def main():
    N, ta, ao, *AB = list(map(int, read().split()))
    ta -= 1
    ao -= 1

    G = [[] for _ in range(N)]
    for a, b in zip(*[iter(AB)] * 2):
        G[a - 1].append(b - 1)
        G[b - 1].append(a - 1)

    dist_ta = [-1] * N
    dist_ta[ta] = 0
    queue = deque([ta])
    while queue:
        v = queue.popleft()
        for nv in G[v]:
            if dist_ta[nv] == -1:
                dist_ta[nv] = dist_ta[v] + 1
                queue.append(nv)

    dist_ao = [-1] * N
    dist_ao[ao] = 0
    queue = deque([ao])
    while queue:
        v = queue.popleft()
        for nv in G[v]:
            if dist_ao[nv] == -1:
                dist_ao[nv] = dist_ao[v] + 1
                queue.append(nv)

    d_ao = 0
    for d1, d2 in zip(dist_ta, dist_ao):
        if d1 < d2 and d_ao < d2:
            d_ao = d2

    if d_ao == 0:
        if dist_ta[ao] == 1:
            print((0))
        else:
            print((1))

    print((d_ao - 1))
    return


def __starting_point():
    main()

__starting_point()

import sys
sys.setrecursionlimit(10**8)
N,U,V = map(int,input().split())
U,V = U-1,V-1
AB = [tuple(map(int,input().split())) for i in range(N-1)]
es = [[] for _ in range(N)]
for a,b in AB:
    a,b = a-1,b-1
    es[a].append(b)
    es[b].append(a)

prev = [-1] * N
def dfs(v,p=-1):
    for to in es[v]:
        if to==p: continue
        prev[to] = v
        dfs(to,v)
dfs(U)

path = [V]
while prev[path[-1]] >= 0:
    path.append(prev[path[-1]])
ban = set(path[:(len(path)+1)//2])

reach = [0] * N
reach[U] = 1
def dfs2(v,p=-1):
    for to in es[v]:
        if to==p: continue
        if to in ban: continue
        reach[to] = 1
        dfs2(to,v)
dfs2(U)

depth = [0] * N
ans = 0
def dfs3(v,p=-1):
    nonlocal ans
    for to in es[v]:
        if to==p: continue
        depth[to] = depth[v] + 1
        if reach[to]:
            ans = max(ans, depth[v])
        dfs3(to,v)
dfs3(V)

print(ans)

from collections import deque
import sys
si = sys.stdin.readline


def BFS(g, s):
    d = 0
    dt = dict()
    visited = set()
    visited.add(s)
    Q = deque([s])

    while len(Q):
        d += 1
        Qs = len(Q)
        for _ in range(Qs):
            Qpop = Q.popleft()
            for node in g[Qpop]:
                if node not in visited:
                    Q.append(node)
                    visited.add(node)
                    dt[node] = d
    return dt


def main():
    l = [int(e) for e in si().split()]
    n, u, v = l[0], l[1], l[2]
    graph = [[] for _ in range(n+1)]
    for _ in range(n-1):
        l = [int(e) for e in si().split()]
        graph[l[0]].append(l[1])
        graph[l[1]].append(l[0])

    udt = BFS(graph, u)
    vdt = BFS(graph, v)
    udt[u], vdt[v] = 0, 0

    vdmax = 0
    for i in range(1, n+1):
        if (vdt[i]-udt[i]) > 0 and vdt[i] > vdmax:
            vdmax = vdt[i]
    print((vdmax-1))


def __starting_point():
    main()

__starting_point()

#!/usr/bin/env python3
from collections import deque

def main():
    N, u, v = map(int, input().split())
    s = u-1
    t = v-1
    adj = [[] for _ in range(N)]
    for _ in range(N-1):
        a, b = map(lambda x: int(x)-1, input().split())
        adj[a].append(b)
        adj[b].append(a)
    
    # aoki BFS
    queue = deque([t])
    visit = [-1] * N
    visit[t] = 0
    
    while queue:
        now = queue.popleft()
        for u in adj[now]:
            if visit[u] < 0:
                queue.append(u)
                visit[u] = visit[now] + 1
                # parent[u] = now

    # takahashi BFS
    queue = deque([s])
    visit2 = [-1] * N
    # parent = [-1] * N
    visit2[s] = 0
    
    
    while queue:
        now = queue.popleft()
        for u in adj[now]:
            if visit2[u] < 0:
                queue.append(u)
                visit2[u] = visit2[now] + 1

    ans = [-1,-1]
    # print(visit, visit2)
    for i in range(N):
        if visit[i] >= visit2[i] and visit[i] > ans[0]:
            ans = [visit[i], i]
    
    print(visit[ans[1]]-1)

def __starting_point():
    main()
__starting_point()

# even faster by using a comprehensive list at the end

from collections import deque
import sys
si = sys.stdin.readline


def BFS(g, n, s):
    d = 0
    dt = [0 for _ in range(n+1)]
    dt[s] = 0
    visited = set()
    visited.add(s)
    Q = deque([s])

    while len(Q):
        d += 1
        Qs = len(Q)
        for _ in range(Qs):
            Qpop = Q.popleft()
            for node in g[Qpop]:
                if node not in visited:
                    Q.append(node)
                    visited.add(node)
                    dt[node] = d
    return dt[1:]


def main():
    l = [int(e) for e in si().split()]
    n, u, v = l[0], l[1], l[2]
    graph = [[] for _ in range(n+1)]
    for _ in range(n-1):
        l = [int(e) for e in si().split()]
        graph[l[0]].append(l[1])
        graph[l[1]].append(l[0])

    udt = BFS(graph, n, u)
    vdt = BFS(graph, n, v)
    print((-1+max([v for u, v in zip(udt, vdt) if u < v])))


def __starting_point():
    main()

__starting_point()

import sys
def input(): return sys.stdin.readline().rstrip()
from collections import deque
def main():
    n, u, v = map(int,input().split())
    if u == v:
        print(0)
        return

    graph = [[] for _ in range(n)]
    depth_u = [-1] * (n) #頂点iのuから見た深さ
    depth_v = [-1] * (n) #頂点iのvから見た深さ
    u -= 1 #高橋
    v -= 1 #青木
    for i in range(n-1):
        a, b = map(int,input().split())
        graph[a - 1].append(b - 1)
        graph[b - 1].append(a - 1)
    
    # bfsで高橋から見た各頂点の深さをメモる。
    queue = deque([u])
    visited = [False]*n
    visited[u] = True
    depth_u[u] = 0
    while queue:
        node = queue.popleft()
        for xnode in graph[node]:
            if not visited[xnode]:
                visited[xnode] = True
                depth_u[xnode] = depth_u[node] + 1
                queue.append(xnode)
    # bfsで青木から見た各頂点の深さをメモる。
    queue = deque([v])
    visited = [False]*n
    visited[v] = True
    depth_v[v] = 0
    while queue:
        node = queue.popleft()
        for xnode in graph[node]:
            if not visited[xnode]:
                visited[xnode] = True
                depth_v[xnode] = depth_v[node] + 1
                queue.append(xnode)
    ans = 0
    max_d_v = 0
    for i in range(n):
        if depth_u[i] < depth_v[i]:
            max_d_v = max(max_d_v, depth_v[i])
    print(max_d_v - 1)



def __starting_point():
    main()
__starting_point()

N, S, T = list(map(int, input().split()))
S -= 1
T -= 1

edges = [[] for _ in range(N)]
for _ in range(N - 1):
    fr, to = [int(a) - 1 for a in input().split()]
    edges[fr].append(to)
    edges[to].append(fr)

def calc(s):
    minDist = [10**18] * N
    minDist[s] = 0
    st = [s]
    while st:
        now = st.pop()
        d = minDist[now] + 1
        for to in edges[now]:
            if minDist[to] > d:
                minDist[to] = d
                st.append(to)

    return minDist

minDistS = calc(S)
minDistT = calc(T)

ans = 0
for s, t in zip(minDistS, minDistT):
    if s <= t:
        ans = max(ans, t - 1)
print(ans)


import sys
sys.setrecursionlimit(10**6)

n, u, v = map(int, input().split())
u, v = u-1, v-1
edges = [[] for i in range(n)]
for i in range(n-1):
    ai, bi = map(int, input().split())
    ai, bi = ai-1, bi-1
    edges[ai].append(bi)
    edges[bi].append(ai)

def dfs(already, node, depth):
    depth += 1
    children = edges[node]
    for child in children:
        if already[child] == 0:
            already[child] = depth
            already = dfs(already, child, depth)
    return already

already_u = [0 for i in range(n)]
already_u[u] = -1
already_u = dfs(already_u, u, 0)
already_u[u] = 0
already_v = [0 for i in range(n)]
already_v[v] = -1
already_v = dfs(already_v, v, 0)
already_v[v] = 0

maxi = 0
for i in range(n):
    if already_u[i] < already_v[i]:
        maxi = max(maxi, already_v[i])
print(maxi-1)

from collections import deque
N,U,V = map(int, input().split())
U,V = U-1, V-1
E = [[] for _ in range(N)]

for _ in range(N-1):
  a,b = map(int, input().split())
  a,b = a-1, b-1
  E[a].append(b)
  E[b].append(a)

def BFS(root):
  D = [ 0 for _ in range(N) ]
  visited = [False for _ in range(N)]
  willSearch = [ False for _ in range(N)]
  Q = deque()
  Q.append(root)
  willSearch[root] = True
  while len(Q) > 0:
    now = Q.pop()
    visited[now] = True
    for nx in E[now]:
      if visited[nx] or willSearch[nx]:
        continue
      willSearch[nx] = True
      D[nx] = D[now] + 1
      Q.append(nx)
  return D

UD = BFS(U)
VD = BFS(V)

#print(UD)
#print(VD)

#初手で青木くんのPOINTにしか行けないケース
if E[U] == [V]:
  print(0)
  return
  
ans=0
 
for i in range(N):
    if UD[i]<=VD[i]:
        ans=max(ans,VD[i]-1)

print(ans)

import sys
import  math
import fractions
from collections import deque
from collections import defaultdict
stdin = sys.stdin
     
ns = lambda: stdin.readline().rstrip()
ni = lambda: int(stdin.readline().rstrip())
nm = lambda: list(map(int, stdin.readline().split()))
nl = lambda: list(map(int, stdin.readline().split()))

N,u,v=nm()
u=u-1
v=v-1
INF=10**18
edge=[[] for i in range(N)]

def bfs(start,edge):
    d=[INF]*N
    d[start]=0
    q=deque([start])
    while (q):
        e=q.popleft()
        for i in edge[e]:
            if(d[e]+1<d[i]):
                d[i]=d[e]+1
                q.append(i)   
    return d


for i in range(N-1):
    A,B=nm()
    edge[B-1].append(A-1)
    edge[A-1].append(B-1)


d_A=bfs(u,edge)
d_B=bfs(v,edge)

ans=0
for i in range(N):
    #if(d_A[0]%2==d_B[0]%2):
    #    continue
    #else:
    if(d_A[i]<d_B[i]):
        tmp=max(d_A[i],d_B[i])
        ans=max(ans,tmp)

print((ans-1))


#!/usr/bin/env python3

def main():
    n, u, v = list(map(int, input().split()))
    u -= 1
    v -= 1
    adj = [[] for i in range(n)]
    for i in range(n - 1):
        a, b = list(map(int, input().split()))
        a -= 1
        b -= 1
        adj[a].append(b)
        adj[b].append(a)

    # uv間経路を求める
    st = [u]
    prev = [None for i in range(n)]
    while st:
        x = st.pop()
        for y in adj[x]:
            if y == prev[x]: continue
            prev[y] = x
            st.append(y)
    path = [v]
    while path[-1] != u:
        path.append(prev[path[-1]])
    path = list(reversed(path))

    # crossからsquareを経由しない最遠ノードstarを求める（a59p22）
    l = len(path) - 1
    cross = path[(l - 1) // 2]
    square = path[(l - 1) // 2 + 1]
    st = [(cross, 0)]
    prev = [None for i in range(n)]
    dist = [-1 for i in range(n)]
    while st:
        x, d = st.pop()
        dist[x] = d
        for y in adj[x]:
            if y == prev[x]: continue
            if y == square: continue
            prev[y] = x
            st.append((y, d + 1))
    star_square = max(dist)

    if l % 2 == 1:
        res = (l - 1) // 2 + star_square
    else:
        res = (l - 1) // 2 + star_square + 1
    print(res)

def __starting_point():
    main()

__starting_point()

import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**9)

INF = float('inf')

N, u, v = list(map(int, input().split()))
u, v = u-1, v-1
adjL = [[] for _ in range(N)]
for _ in range(N-1):
    a, b = list(map(int, input().split()))
    a, b = a-1, b-1
    adjL[a].append(b)
    adjL[b].append(a)

def bfsMinCosts(adjList, vSt, INF):
    numV = len(adjList)
    costs = [INF] * numV
    costs[vSt] = cost = 0
    vs = [vSt]
    while vs:
        cost += 1
        v2s = []
        for v in vs:
            for v2 in adjList[v]:
                if costs[v2] == INF:
                    costs[v2] = cost
                    v2s.append(v2)
        vs = v2s
    return costs

costUs = bfsMinCosts(adjL, u, INF)
costVs = bfsMinCosts(adjL, v, INF)

ans = 0
for x in range(N):
    if costUs[x] < costVs[x]:
        if costVs[x] > ans:
            ans = costVs[x]

print((ans-1))


import sys
sys.setrecursionlimit(10**8)
N,U,V = map(int,input().split())
U,V = U-1,V-1
AB = [tuple(map(int,input().split())) for i in range(N-1)]
es = [[] for _ in range(N)]
for a,b in AB:
    a,b = a-1,b-1
    es[a].append(b)
    es[b].append(a)

dist1 = [N] * N
dist1[V] = 0
def dfs1(v,p=-1):
    for to in es[v]:
        if to==p: continue
        dist1[to] = dist1[v] + 1
        dfs1(to,v)
dfs1(V)

D = dist1[U]

dist2 = [N] * N
dist2[U] = 0
def dfs2(v,p=-1):
    for to in es[v]:
        if to==p: continue
        if (D+1)//2 > dist1[to]: continue
        dist2[to] = dist2[v] + 1
        dfs2(to,v)
dfs2(U)

ans = 0
for a,b in zip(dist1,dist2):
    if b==N: continue
    ans = max(ans, a-1)
print(ans)

import sys
sys.setrecursionlimit(10**7)

def dfs(v,p,d):
    dist[v]=d
    for nv in G[v]:
        if nv==p:
            continue
        dfs(nv,v,d+1)
        
def getSP(v):
    p=v
    if dist[v]==lim:
        return p
    for nv in G[v]:
        if dist[nv]<dist[v]:
            p=getSP(nv)
    return p
    
def depth(v,p,d):
    nonlocal maxd
    maxd=max(maxd,dist[v])
    for nv in G[v]:
        if dist[nv]<dist[v]:
            continue
        depth(nv,v,d+1)
    
N,u,v=map(int,input().split());u,v=u-1,v-1
if u==v:
    print(0)
    return

G=[[] for i in range(N)]
for i in range(N-1):
    a,b=map(lambda x:int(x)-1,input().split())
    G[a].append(b)
    G[b].append(a)
    
dist=[-1]*N
dfs(v,-1,0)

lim=dist[u]-(dist[u]-1)//2
SP=getSP(u)
 
maxd=-1
depth(SP,-1,0)

print(maxd-1)

import collections

n,u,v=map(int,input().split())
g=[[] for _ in range(n+1)]
for _ in range(n-1):
  a,b=map(int,input().split())
  g[a].append(b)
  g[b].append(a)
q=collections.deque()
q.append((v,0))
checked=[0]*(n+1)
dist1=[0]*(n+1)
while len(q)!=0:
  tv,td=q.popleft()
  checked[tv]=1
  dist1[tv]=td
  for tu in g[tv]:
    if checked[tu]==1:
      continue
    q.append((tu,td+1))
q=collections.deque()
q.append((u,0))
checked=[0]*(n+1)
dist2=[0]*(n+1)
while len(q)!=0:
  tv,td=q.popleft()
  checked[tv]=1
  dist2[tv]=td
  for tu in g[tv]:
    if checked[tu]==1:
      continue
    q.append((tu,td+1))
ans=0
for tv in range(1,n+1):
  if dist1[tv]>dist2[tv]:
    ans=max(ans,dist1[tv]-1)
print(ans)

import sys
sys.setrecursionlimit(1000000000)

n,u,v = map(int,input().split())
u -= 1
v -= 1
G = [[] for _ in range(n)]
for _ in range(n-1):
    a,b = map(int,input().split())
    a -= 1
    b -= 1
    G[a].append(b)
    G[b].append(a)

distT = [-1]*n
distA = [-1]*n
distT[u] = 0
distA[v] = 0
def dfs(now,dist):
    for nxt in G[now]:
        if dist[nxt] != -1: continue
        dist[nxt] = dist[now]+1
        dfs(nxt,dist)
dfs(u,distT)
dfs(v,distA)

mx = 0
for i in range(n):
    if distT[i] < distA[i]:
        mx = max(mx, distA[i])
ans = mx-1
print(ans)

import copy
from collections import deque

n, st, sa = map(int,input().split())
st -= 1
sa -= 1

e = [[] for i in range(n)]
for i in range(n-1):
    a, b = map(int, input().split())
    a -= 1
    b -= 1
    e[a].append(b)
    e[b].append(a)

visited = [False] * n
visited[st] = True
d = deque()
d.append([st, 0])
tx = {}
if len(e[st]) == 1:
    tx[st] = 0
cnt = 0
while d:
    f, cnt = d.popleft()
    flg = True
    for t in e[f]:
        if not visited[t]:
            flg = False
            d.append([t, cnt+1])
            visited[t] = True
    if flg:
        tx[f] = cnt

visited = [False] * n
visited[sa] = True
d = deque()
d.append([sa, 0])
ax = {}
if len(e[sa]) == 1:
    ax[sa] = 0
cnt = 0
while d:
    f, cnt = d.popleft()
    flg = True
    for t in e[f]:
        if not visited[t]:
            flg = False
            d.append([t, cnt+1])
            visited[t] = True
    if flg:
        ax[f] = cnt

ax = sorted(ax.items(), key=lambda x:x[1], reverse=True)
for i in range(len(ax)):
    x, d = ax[i][0], ax[i][1]
    if d > tx[x]:
        ans = d - 1
        break

print(ans)

from collections import deque

n, u, v = map(int, input().split())
tree = [[] for _ in range(n)]
for i in range(n-1):
  a, b = map(int, input().split())
  tree[a-1].append(b-1)
  tree[b-1].append(a-1)

not_yet = deque([u-1])
already = [False]*n
already[u-1] = True
dist = [0]*n

while not_yet:
  key = not_yet.popleft()
  for value in tree[key]:
    if already[value]:
      continue
    dist[value] = dist[key] + 1
    already[value] = True
    not_yet.append(value)
  
not_yet = deque([v-1])
already = [False]*n
already[v-1] = True
dist2 = [0]*n

while not_yet:
  key = not_yet.popleft()
  for value in tree[key]:
    if already[value]:
      continue
    dist2[value] = dist2[key] + 1
    already[value] = True
    not_yet.append(value)

judge = dist[v-1]
ans = 0
for i in range(n):
  if dist[i]+dist2[i] > judge:
    if dist[i] >= dist2[i]:
      continue
    sub = dist2[i]-1
  else:
    sub = judge-1
  if sub > ans:
    ans = sub
print(ans)

def dfs(c,lst):
 n=len(lst)
 ans=[0]*n
 stack = [c-1]
 check = [0]*n #チェック済みリスト
 while stack != [] :
  d=stack.pop()
  if check[d]==0:
   check[d]=1
   for i in lst[d]:
    if check[i]==0:
     stack.append(i)
     ans[i]=ans[d]+1
 return(ans)
import sys
input = sys.stdin.readline
N,u,v=list(map(int,input().split()))
ki=[[] for f in range(N)]
for i in range(N-1):
 a,b = list(map(int,input().split()))
 ki[a-1].append(b-1)
 ki[b-1].append(a-1)
U=dfs(u,ki)
V=dfs(v,ki)
ans=0
for i in range(N):
 if V[i]>U[i]:
  ans=max(ans,V[i]-1)
print(ans)


import sys
readline = sys.stdin.readline

# 青木君からの到達距離 - 高橋君からの到達距離が最も大きい場所に逃げるべき
# そこに到達したときに、
# 高橋君と青木君の距離が偶数のとき
# 青 - x - x - x - 高
# 青 - x - x - 高 - x
# x - 青 - x - 高 - x
# x - 青 - x - x - 高
# x - x - 青 - x - 高
# x - x - 青- 高 - x 
# x - x - x - 終 - x 
# 青木君からの距離 - 1が答え
# 高橋君と青木君の距離が奇数のとき
# 青 - x - x - 高
# 青 - x - 高 - x
# x - 青 - 高 - x
# x - 青 - x - 高
# x - x - 青 - 高
# x - x - 終 - x
# やっぱり青木君からの距離 - 1が答え
# つまり、
# ・[青木君からの距離が遠い場所について]
# ・[青木君からの距離 - 1]が答え

N,u,v = list(map(int,readline().split()))
G = [[] for i in range(N)]
for i in range(N - 1):
  a,b = list(map(int,readline().split()))
  G[a - 1].append(b - 1)
  G[b - 1].append(a - 1)

dist_from_u = [0 for i in range(N)] # 高橋君からの距離
dist_from_v = [0 for i in range(N)] # 青木君からの距離

stack = []
# 頂点, 高橋君からの距離, 親
stack.append([u - 1,0,-1])
while stack:
  e, dist, parent = stack.pop()
  dist_from_u[e] = dist
  for child in G[e]:
    if child == parent:
      continue
    stack.append([child, dist + 1, e])

stack = []
# 頂点, 青木君からの距離, 親
stack.append([v - 1,0,-1])
while stack:
  e, dist, parent = stack.pop()
  dist_from_v[e] = dist
  for child in G[e]:
    if child == parent:
      continue
    stack.append([child, dist + 1, e])
    
maxdiff_from_v = 0
for i in range(N):
  if dist_from_u[i] < dist_from_v[i]:
    if maxdiff_from_v < dist_from_v[i]:
      maxdiff_from_v = dist_from_v[i]
    
print((maxdiff_from_v - 1))


import sys
sys.setrecursionlimit(1<<30)
def dfs(x,dis):
    for y in Tree[x]:
        if parent[x] != y:
            parent[y] = x
            dis[y] = dis[x]+1
            dfs(y,dis)

N,u,v = map(int,input().split())
Tree = [[] for _ in range(N+1)]
for i in range(N-1):
    A,B = map(int,input().split())
    Tree[A].append(B)
    Tree[B].append(A)
disT = [-1]*(N+1)
disA = [-1]*(N+1)
disT[u] = 0
disA[v] = 0
parent = [-1]*(N+1)
dfs(u,disT)
parent = [-1]*(N+1)
dfs(v,disA)
ans = 0
for i in range(1,N+1):
    if disT[i] <= disA[i]:
        ans = max(ans,disA[i]-1)
print(ans)

# F - Playing Tag on Tree
# https://atcoder.jp/contests/abc148/tasks/abc148_f

from heapq import heappop, heappush

INF = float("inf")

def dijkstra(n, G, s):
    dist = [INF] * n
    dist[s] = 0
    hq = [(0, s)]
    while hq:
        d, v = heappop(hq)
        if dist[v] < d:
            continue
        for child, child_d in G[v]:
            if dist[child] > dist[v] + child_d:
                dist[child] = dist[v] + child_d
                heappush(hq, (dist[child], child))
    return dist

n, u, v = list(map(int, input().split()))
graph = [[] for _ in range(n)]

edge = [list(map(int, input().split())) for _ in range(n - 1)]

for a, b in edge:
    graph[a - 1].append((b - 1, 1))
    graph[b - 1].append((a - 1, 1))

from_u = dijkstra(n, graph, u - 1)
from_v = dijkstra(n, graph, v - 1)

# print(from_u)
# print(from_v)

fil = [x for x in [[fu, fv] for fu, fv in zip(from_u, from_v)] if x[0] < x[1]]
sfil = sorted(list(fil), key=lambda x: [-x[1], -x[0]])  

# print(sfil)

print((sfil[0][1] - 1))


import copy
import sys
sys.setrecursionlimit(10**9)

def dfs(G, v, p, d, depth):
    depth[v] = d
    for nv in G[v]:
        if nv == p:
            continue
        dfs(G, nv, v, d+1, depth)
    return G, depth


n, T, A = map(int,input().split())
T, A = T-1, A-1
g = [[] for _ in range(n)]
for i in range(n-1):
    x, y = map(int,input().split())
    x, y = x-1, y-1
    g[x].append(y)
    g[y].append(x)

depth = [0]*n
GT = copy.deepcopy(g)
GT[T].append(-1)
GT, dt = dfs(GT, T, -1, 0, depth)

depth = [0]*n
GA = copy.deepcopy(g)
GA[A].append(-1)
GA, da = dfs(GA, A, -1, 0, depth)

ans = 0
for t, a in zip(dt, da):
    if t <= a:
        ans = max(ans, a)
print(ans-1)

import sys
sys.setrecursionlimit(10**6)
n, u, v = map(int, input().split())
u -= 1
v -= 1
graph = [[] for _ in range(n)]
for _ in range(n-1):
    a, b = map(int, input().split())
    graph[a-1].append(b-1)
    graph[b-1].append(a-1)
pre = [None]*n
def dfs(node, p_node=-1):
    for c_node in graph[node]:
        if c_node==p_node:
            continue
        pre[c_node] = node
        dfs(c_node, node)
dfs(u)
path = [v]
now = v
while now!=u:
    now = pre[now]
    path.append(now)
s1 = path[len(path)//2]
p1 = path[len(path)//2-1]
def dfs2(node, depth, p_node):
    mx = depth
    for c_node in graph[node]:
        if c_node==p_node:
            continue
        mx = max(mx, dfs2(c_node, depth+1, node))
    return mx
print(dfs2(s1, 0, p1)+len(path)//2-1)

import sys
sys.setrecursionlimit(1<<30)
def dfs(x, dis):
    for y in Tree[x]:
        if parent[y] != x:
            parent[x] = y
            dis[y] = dis[x] + 1
            dfs(y, dis)
N, u, v = map(int,input().split())
Tree = [[] for _ in range(N+1)]
for i in range(N-1):
    A, B = map(int,input().split())
    Tree[A].append(B)
    Tree[B].append(A)
disT = [-1]*(N+1)
disA= [-1]*(N+1)
disT[u] = 0
disA[v] = 0
parent = [-1]*(N+1)
dfs(u, disT)
parent = [-1]*(N+1)
dfs(v, disA)
Ans = 0
for i in range(len(disA)):
    if disA[i] >= disT[i]:
        Ans = max(Ans, disA[i]-1)
print(Ans)

n,u,v=map(int,input().split())
u-=1;v-=1
ki=[[] for _ in range(n)]
for i in range(n-1):
  a,b=map(int,input().split())
  ki[a-1].append(b-1)
  ki[b-1].append(a-1)
ha=[]
for i in range(n):
  if len(ki[i])==1:
    ha.append(i)
ta=[0]*n
ao=[0]*n
from collections import deque
d=deque()
d.append(u)
visited=[False]*n
visited[u]=True
while d:
  g=d.popleft()
  for i in ki[g]:
    if visited[i]:
      continue
    d.append(i)
    visited[i]=True
    ta[i]=ta[g]+1
d=deque()
d.append(v)
visited=[False]*n
visited[v]=True
while d:
  g=d.popleft()
  for i in ki[g]:
    if visited[i]:
      continue
    d.append(i)
    visited[i]=True
    ao[i]=ao[g]+1
if u in ha and ki[u][0]==v:
  print(0)
  return
m=0
for i in ha:
  if ta[i]<ao[i]:
    m=max(m,ao[i])
print(m-1)

import sys
sys.setrecursionlimit(10**6)
 
N,u,v = map(int,input().split())
edges = [list(map(int,input().split())) for _ in range(N-1)]
E = [[] for _ in range(N+1)]
for a,b in edges:
    E[a].append(b)
    E[b].append(a)
 
depth = [[-1,-1] for _ in range(N+1)] 
depth[v][0] = 0
depth[u][1] = 0

def dfs1(E,v):
    vv = E[v]
    for vvv in vv:
        if depth[vvv][0] == -1:
            depth[vvv][0] = depth[v][0] + 1
            dfs1(E,vvv)
 
def dfs2(E,v):
    vv = E[v]
    for vvv in vv:
        if depth[vvv][1] == -1:
            depth[vvv][1] = depth[v][1] + 1
            dfs2(E,vvv)
dfs1(E,v)
dfs2(E,u)
depth = sorted(depth, reverse=True)
idx = 0
while True:
    if depth[idx][0] - depth[idx][1] >= 1:
        print(depth[idx][0]-1)
        return
    else:
        idx += 1

import sys
rd = sys.stdin.readline

sys.setrecursionlimit(10000000)

n,T,A = map(int,rd().split())
T,A = T-1,A-1

graph = [[] for _ in range(n)]
for i in range(n-1):
    a,b = map(int,rd().split())
    graph[a-1].append(b-1)
    graph[b-1].append(a-1)  # 有向グラフなら消す

def dfs(v,p=-1):
    deg = len(graph[v])
    if v == T:
        root.append(v)
        return True
    if deg == 1 and v != A:
        return False
    ok = False
    for i in range(deg):
        u = graph[v][i]
        if u == p:
            continue
        if dfs(u,v):
            root.append(v)
            ok = True
    return ok

def dfs2(v,p=-1):
    deg = len(graph[v])
    if deg == 1 and v != root[x]:
        return 0
    depth = 0
    for i in range(deg):
        u = graph[v][i]
        if u == p:
            continue
        d = dfs2(u,v)
        depth = max(depth,d+1)
    return depth

root = []
dfs(A)
x = len(root)//2 - 1

dp = [0]*n
ans = (len(root))//2 + dfs2(root[x],root[x+1])
if len(root)%2 == 0:
    ans -= 1
print(ans)

N, u, v = map(int, input().split())
u -= 1
v -= 1
tonari = [[] for _ in range(N)]
for _ in range(N-1):
  a, b = map(int, input().split())
  a -= 1
  b -= 1
  tonari[a].append(b)
  tonari[b].append(a)

d_from_u = [float('inf') for _ in range(N)]
d_from_v = [float('inf') for _ in range(N)]
d_from_u[u] = 0
d_from_v[v] = 0
#print(tonari)
now = [v] #追いかける方
d = 0
while now:
  d += 1
  nextvisit = []
  for n in now:
    for t in tonari[n]:
      if d_from_v[t] > d:
        d_from_v[t] = d
        nextvisit.append(t)
  now = nextvisit



now = [u] #逃げる方
d = 0
while now:
  d += 1
  nextvisit = []
  for n in now:
    for t in tonari[n]:
      if d_from_v[t] <= d: #追いかける方より遠い地点は排除
        continue
      elif d_from_u[t] > d:
        d_from_u[t] = d
        nextvisit.append(t)
  now = nextvisit


distance = []
for U, V in zip(d_from_u, d_from_v):
  if U < N+1: #infを排除
    distance.append((U, V))

if distance:
  ans = max(distance, key= lambda x: x[1])[1] - 1
else:
  ans = 0
print(ans)

import sys
sys.setrecursionlimit(10**7)
N,T,A = list(map(int,input().split())); INF = float("inf")
T-=1;A-=1 #0index
G = [[] for _ in range(N)]
for i in range(N-1):
  a,b = list(map(int,input().split()))
  a-=1;b-=1 #0index
  G[a].append(b);G[b].append(a)

disT = [INF for _ in range(N)]; disT[T] = 0
disA = [INF for _ in range(N)]; disA[A] = 0

def dfs(v, L):
  for u in G[v]:
    if L[u] != INF:
      continue
    L[u] = L[v]+1
    dfs(u,L)
dfs(T,disT)
dfs(A,disA)
#print(disT)
#print(disA)
ans = 0
for i in range(N):
  if disT[i] < disA[i]:
    ans = max(ans,disA[i]-1)
print(ans)


from collections import deque


def calc_dist(head, my_dict, dist_a):
    deq = deque()
    deq.append((head, 0))
    visited = set()
    visited.add(head)

    while deq:
        ptr, length = deq.pop()
        dist_a[ptr] = length
        for item in my_dict[ptr]:
            if item not in visited:
                visited.add(item)
                deq.append((item, length+1))


n, u, v = list(map(int, input().split()))
my_dict = dict()

for i in range(n-1):
    a, b = list(map(int, input().split()))
    if a in my_dict:
        my_dict[a].append(b)
    else:
        my_dict[a] = [b]
    if b in my_dict:
        my_dict[b].append(a)
    else:
        my_dict[b] = [a]

dist_a = dict()
dist_b = dict()

calc_dist(u, my_dict, dist_a)
calc_dist(v, my_dict, dist_b)
ret = 0

for i in range(1, n+1):
    if dist_a[i] > dist_b[i]:
        continue
    else:
        ret = max(ret, dist_b[i] - 1)

print(ret)


N, U, V = map(int, input().split())
edge = [[] for _ in range(N + 1)]
for _ in range(N - 1):
    x, y = map(int, input().split())
    edge[x].append(y)
    edge[y].append(x)


def dist(s):
    dist = [-1] * (N + 1)
    node = [s]
    dist[s] = 0
    while node:
        s = node.pop()
        for t in edge[s]:
            if dist[t] == -1:
                dist[t] = dist[s] + 1
                node.append(t)
    return dist


taka = dist(U)
aoki = dist(V)
can_go = taka[V]

ans = 0
for t, a in zip(taka[1:], aoki[1:]):
    if t <= a:
        ans = max(ans, a - 1)
print(ans)

N, u, v = map(int, input().split())
u -= 1
v -= 1
tonari = [[] for _ in range(N)]
for _ in range(N-1):
  a, b = map(int, input().split())
  a -= 1
  b -= 1
  tonari[a].append(b)
  tonari[b].append(a)

d_from_u = [float('inf') for _ in range(N)]
d_from_v = [float('inf') for _ in range(N)]
d_from_u[u] = 0
d_from_v[v] = 0
#print(tonari)

now = [u]
d = 0
while now:
  d += 1
  nextvisit = []
  for n in now:
    for t in tonari[n]:
      if d_from_u[t] > d:
        d_from_u[t] = d
        nextvisit.append(t)
  now = nextvisit

now = [v]
d = 0
while now:
  d += 1
  nextvisit = []
  for n in now:
    for t in tonari[n]:
      if d_from_v[t] > d:
        d_from_v[t] = d
        nextvisit.append(t)
  now = nextvisit

#print(d_from_u)
#print(d_from_v)

distance = []
for U, V in zip(d_from_u, d_from_v):
  distance.append((U, V))

distance.sort(key = lambda x: x[1])

#print(distance)
ans = 0
while distance:
  d = distance.pop()
  if d[0] < d[1]:
    ans = d[1] - 1
    break
print(ans)

import sys
sys.setrecursionlimit(1000000)

def dfs_a(N,v,adjlist,cur):
    nonlocal depth_a
    depth_a[v]=cur
    for node in adjlist[v]:
        if depth_a[node]<0:
            dfs_a(N,node,adjlist,cur+1)
def dfs_t(N,v,adjlist,cur):
    nonlocal depth_t
    depth_t[v]=cur
    for node in adjlist[v]:
        if depth_t[node]<0:
            dfs_t(N,node,adjlist,cur+1)
    
    
    

from collections import deque

N,u,v=list(map(int,input().split()))
adjlist=[[] for _ in range(N)]
for _ in range(N-1):
    a,b=list(map(int,input().split()))
    adjlist[a-1].append(b-1)
    adjlist[b-1].append(a-1)
depth_a=[-1]*N
depth_t=[-1]*N
dfs_a(N,v-1,adjlist,0)
dfs_t(N,u-1,adjlist,0)
M=0
for i in range(N):
    if depth_a[i]>depth_t[i]:
        curm=depth_a[i]-1
        if curm>M:
            M=curm
print(M)
        

 
        


import sys
sys.setrecursionlimit(10**7)


n,u,v=list(map(int, input().split()))
u-=1
v-=1

graph = [[] for _ in range(n+1)]
# for i in range(n-1):
#     a, b=map(int, input().split())
#     graph[a-1].append(b-1)
#     graph[b-1].append(a-1)

ab=[list(map(int, input().split())) for _ in range(n-1)]
for a,b in ab:
    graph[a-1].append(b-1)
    graph[b-1].append(a-1)

dist=[[-1, -1] for _ in range(n)]
dist[v][0]=0
dist[u][1]=0

def dfs1(graph,v):
    vv=graph[v]
    for vvv in vv:
        if dist[vvv][0]==-1:
            dist[vvv][0]=dist[v][0]+1
            dfs1(graph,vvv)

def dfs2(graph,v):
    vv=graph[v]
    for vvv in vv:
        if dist[vvv][1]==-1:
            dist[vvv][1]=dist[v][1]+1
            dfs2(graph,vvv)

dfs1(graph,v)
dfs2(graph,u)
dist.sort(reverse=True)

for i in range(10**6):
    if dist[i][0]-dist[i][1]>=1:
        print((dist[i][0]-1))
        break


import sys
sys.setrecursionlimit(10**6)

n,g,s = map(int,input().split())
G = [[] for _ in range(n)]
for i in range(n-1):
  a,b = map(int,input().split())
  G[a-1].append(b-1)
  G[b-1].append(a-1)
  
def dfs(V,v,d):
  if V[v] != -1:
    return
  V[v] = d
  for nv in G[v]:
    dfs(V,nv,d+1)
  
if len(G[g-1])==1 and G[g-1][0]==s-1:
  print(0)
else:
  vs = [-1]*n
  vg = [-1]*n
  dfs(vs,s-1,0)
  dfs(vg,g-1,0)
  
  ans = 0
  for i in range(n):
    if vs[i]>vg[i]:
      ans = max(ans,vs[i])
      
  print(ans-1)

from collections import defaultdict
import sys


sys.setrecursionlimit(10 ** 6)


def depth(adj, node, parent=None):
    ret = 0
    for child in adj[node]:
        if child == parent:
            continue
        d = depth(adj, child, node) + 1
        ret = max(ret, d)
    return ret


def main():
    N, U, V = list(map(int, input().split(' ')))
    U -= 1
    V -= 1
    adj = defaultdict(list)
    for _ in range(N - 1):
        A, B = list(map(lambda x: int(x) - 1, input().split(' ')))
        adj[A].append(B)
        adj[B].append(A)

    path = [-1] * N

    def find_path_on_tree(to_node, node, parent=None, d=0):
        if node == to_node:
            path[d] = node
            return True
        for child in adj[node]:
            if child == parent:
                continue
            if not find_path_on_tree(to_node, child, node, d + 1):
                continue
            path[d] = node
            return True
        return False

    find_path_on_tree(U, V)  # to U from V
    path = [node for node in path if node != -1]
    dist = len(path) - 1
    partial_depth = depth(adj, path[(dist // 2) + 1], path[dist // 2])
    print(partial_depth + (dist // 2))


def __starting_point():
    main()
__starting_point()

import os
import sys
import numpy as np

def solve(N, U, V, AB):
    G = [[0]*0 for _ in range(N+1)]
    for idx_ab in range(len(AB)):
        a, b = AB[idx_ab]
        G[a].append(b)
        G[b].append(a)
    P1 = np.zeros(N+1, dtype=np.int64)
    def dfs1(u):
        st = [u]
        while st:
            v = st.pop()
            p = P1[v]
            for u in G[v]:
                if p != u:
                    st.append(u)
                    P1[u] = v
    dfs1(U)
    path_u2v = [U]
    v = V
    while v != U:
        v = P1[v]
        path_u2v.append(v)
    path_u2v.reverse()
    l = len(path_u2v)
    half = (l-2) // 2
    c = path_u2v[half]
    ng = path_u2v[half+1]
    Depth = np.zeros(N+1, dtype=np.int64)
    def dfs2(c):
        st = [c]
        P = np.zeros(N+1, dtype=np.int64)
        while st:
            v = st.pop()
            p = P[v]
            d = Depth[v]
            for u in G[v]:
                if p != u and u != ng:
                    st.append(u)
                    P[u] = v
                    Depth[u] = d + 1
    dfs2(c)
    c_ = path_u2v[l-1-half]
    v = c_
    while v != c:
        Depth[v] = 0
        v = P1[v]
    d = l%2
    ans = max(Depth) + half + d
    return ans


# >>> numba compile >>>
numba_config = [
    [solve, "i8(i8,i8,i8,i8[:,:])"],
]
if sys.argv[-1] == "ONLINE_JUDGE":
    from numba import njit
    from numba.pycc import CC
    cc = CC("my_module")
    for func, signature in numba_config:
        vars()[func.__name__] = njit(signature)(func)
        cc.export(func.__name__, signature)(func)
    cc.compile()
    return
elif os.name == "posix":
    exec(f"from my_module import {','.join(func.__name__ for func, _ in numba_config)}")
else:
    from numba import njit
    for func, signature in numba_config:
        vars()[func.__name__] = njit(signature, cache=True)(func)
    print("compiled!", file=sys.stderr)
# <<< numba compile <<<


def main():
    N, u, v = map(int, input().split())
    if N==2:
        print(0)
        return
    AB = np.array(sys.stdin.read().split(), dtype=np.int64).reshape(N-1, 2)
    ans = solve(N, u, v, AB)
    print(ans)

main()


from collections import deque

def bfs(s):
    q = deque()
    q.append(s)
    dist = [-1] * (n + 1)
    dist[s] = 0
    while q:
        p = q.popleft()
        for i in G[p]:
            if dist[i] == -1:
                dist[i] = dist[p] + 1
                q.append(i)
    return dist

n, u, v = map(int, input().split())
G = [[] for _ in range(n + 1)]
for _ in range(n - 1):
    a, b = map(int, input().split())
    G[a].append(b)
    G[b].append(a)
dist1 = bfs(u)
dist2 = bfs(v)
ans = 0
for i in range(1, n + 1):
    if dist1[i] <= dist2[i]:
        ans = max(ans, dist2[i] - 1)
print(ans)

from collections import deque
n , u , v = map(int,input().split())
u -= 1
v -= 1
route = [[] for i in range(n)]
for i in range(n-1):
    a , b = map(lambda x:int(x)-1,input().split())
    route[a].append(b)
    route[b].append(a)

takyori = [-1 for i in range(n)]
takyori[u] = 0
d = deque()
d.append(u)
while d:
    now = d.popleft()
    for i in route[now]:
        if takyori[i] == -1:
            takyori[i] = takyori[now] + 1
            d.append(i)

aokyori = [-1 for i in range(n)]
aokyori[v] = 0
d = deque()
d.append(v)
while d:
    now = d.popleft()
    for i in route[now]:
        if aokyori[i] == -1:
            aokyori[i] = aokyori[now] + 1
            d.append(i)
ans = 0
for i in range(n):
    if takyori[i] <= aokyori[i]:
        ans = max(ans,aokyori[i]-1)
print(ans)

import sys
from collections import deque

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60
MOD = 1000000007


def main():
    N, p, q, *AB = list(map(int, read().split()))
    p -= 1
    q -= 1

    G = [[] for _ in range(N)]
    for a, b in zip(*[iter(AB)] * 2):
        G[a - 1].append(b - 1)
        G[b - 1].append(a - 1)

    if len(G[p]) == 1 and G[p][0] == q:
        print((0))
        return

    dist1 = [-1] * N
    dist1[p] = 0
    queue = deque([p])
    while queue:
        v = queue.popleft()
        for nv in G[v]:
            if dist1[nv] == -1:
                dist1[nv] = dist1[v] + 1
                queue.append(nv)

    dist2 = [-1] * N
    dist2[q] = 0
    queue = deque([q])
    while queue:
        v = queue.popleft()
        for nv in G[v]:
            if dist2[nv] == -1:
                dist2[nv] = dist2[v] + 1
                queue.append(nv)

    max_d = 0
    for d1, d2 in zip(dist1, dist2):
        if d1 < d2 and max_d < d2:
            max_d = d2

    print((max_d - 1))
    return


def __starting_point():
    main()

__starting_point()

N, u, v = map(int, input().split())
u -= 1
v -= 1
tonari = [[] for _ in range(N)]
for _ in range(N-1):
  a, b = map(int, input().split())
  a -= 1
  b -= 1
  tonari[a].append(b)
  tonari[b].append(a)

d_from_u = [float('inf') for _ in range(N)]
d_from_v = [float('inf') for _ in range(N)]
d_from_u[u] = 0
d_from_v[v] = 0
#print(tonari)
now = [v] #追いかける方
d = 0
while now:
  d += 1
  nextvisit = []
  for n in now:
    for t in tonari[n]:
      if d_from_v[t] > d:
        d_from_v[t] = d
        nextvisit.append(t)
  now = nextvisit



now = [u] #逃げる方
d = 0
while now:
  d += 1
  nextvisit = []
  for n in now:
    for t in tonari[n]:
      if d_from_v[t] <= d: #追いかける方より遠い地点は排除
        continue
      elif d_from_u[t] > d:
        d_from_u[t] = d
        nextvisit.append(t)
  now = nextvisit


distance = []
ans = 0
for U, V in zip(d_from_u, d_from_v):
  if U < N+1: #infを排除
    ans = max(ans, V-1)
print(ans)

import sys
f=lambda:map(int,sys.stdin.readline().split())
n,st,sa=f()
st-=1
sa-=1
g=[[] for _ in range(n)]
for _ in range(n-1):
  a,b=f()
  g[a-1].append(b-1)
  g[b-1].append(a-1)
def bfs(s):
  l=[-1]*n; l[s]=0; q=[s]
  while q:
    v=q.pop(); d=l[v]+1
    for c in g[v]:
      if l[c]<0: l[c]=d; q+=[c]
  return l
lt=bfs(st)
la=bfs(sa)
m=0
for i in range(n):
  if lt[i]<la[i]: m=max(m,la[i])
print(m-1)

N, u, v = (int(i) for i in input().split())
adj=[]
for i in range(N+1):adj.append([])
for i in range(N-1):
	a,b = list(map(int,input().split()))
	adj[a]+=[b]
	adj[b]+=[a]

def distances(sommet):
	d = [-1]*(N+1)
	File = [sommet]
	d[sommet] = 0
	while File:
		s = File.pop()
		for x in adj[s]:
			if d[x]==-1:
				d[x] = 1+d[s]
				File.append(x)
	return d


t = distances(u)
a = distances(v)

# ~ print(t)
# ~ print(a)

ans = -1
for i in range(1,N+1):
	if t[i]<a[i]:
		ans = max(ans, a[i]-1)
		
print(ans)


#!/usr/bin python3
# -*- coding: utf-8 -*-

# 双方向グラフで幅優先探索

from collections import deque

n, u, v = map(int, input().split())
g = [[] for _ in range(n)]
#隣接リストの作成
for i in range(n-1):
    a, b = map(int, input().split())
    a, b = a-1, b-1
    g[a].append(b)
    g[b].append(a)


# 幅優先探索
def bfs(x):
    q = deque()
    seen = [False]*n
    level = [-1]*n
    parent = [-1]*n
    q.append(x)
    seen[x] = True
    level[x] = 0
    parent[x] = 0
    while len(q)>0:
        cur = q.popleft()
        lvl = level[cur]
        for i in g[cur]:
            if seen[i]==False:
                seen[i] = True
                #階層
                level[i] = lvl + 1
                #親
                parent[i] = cur
                q.append(i)
    return level


ul = bfs(u-1)
vl = bfs(v-1)

ret = 0
for i, j in zip(ul, vl):
    if i < j:
        ret = max(ret, j-1)
print(ret)

from collections import deque

N,u,v=map(int,input().split())
G=[[] for _ in range(N)]
for _ in range(N-1):
  a,b = map(int,input().split())
  G[a-1].append(b-1)
  G[b-1].append(a-1)

def bfs(s):
    seen = [0]*N
    d = [0]*N
    todo = deque()
    seen[s]=1
    todo.append(s)
    while len(todo):
      a = todo.popleft()
      for b in G[a]:
        if seen[b] == 0:
          seen[b] = 1
          todo.append(b)
          d[b] += d[a] + 1
    return d
  
d1 = bfs(u-1)
d2 = bfs(v-1)

print(max([d2[i] for i in range(N) if d1[i]<=d2[i]])-1)

import sys
sys.setrecursionlimit(10**9)
f=lambda:map(int,sys.stdin.readline().split())
n,st,sa=f()
g=[set() for _ in range(n)]
for _ in range(n-1):
  a,b=f()
  g[a-1].add(b-1)
  g[b-1].add(a-1)
def dfs(l,v,p=-1,d=0):
  l[v]=d
  for c in g[v]:
    if c!=p: dfs(l,c,v,d+1)
lt=[0]*n
dfs(lt,st-1)
la=[0]*n
dfs(la,sa-1)
print(max(la[i] for i in range(n) if lt[i]<la[i])-1)

import numpy as np
import sys
read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
N, u, v = map(int, input().split())

E = [[] for _ in range(N+1)]
for _ in range(N-1):
	ta, tb = map(int, input().split())
	E[ta].append(tb)
	E[tb].append(ta)
E = np.array(E)

inf = 10**9
taka = np.full(N+1, inf, dtype = np.int64)
ao = np.full(N+1, inf, dtype = np.int64)
taka[u] = 0
ao[v] = 0
def solve(dist, start):
	q = [start]
	while q:
		cp = q.pop()
		for nep in E[cp]:
			if dist[nep] != inf:
				continue
			else:
				dist[nep] = dist[cp] + 1
				q.append(nep)

solve(taka, u)
solve(ao, v)
ind = taka < ao
ao_max = np.max(ao[ind])
print(ao_max - 1)

import sys
sys.setrecursionlimit(10**7)

n,u,v=list(map(int, input().split()))
u-=1
v-=1
ab=[list(map(int, input().split())) for _ in range(n-1)]
graph = [[] for _ in range(n+1)]
for a,b in ab:
    graph[a-1].append(b-1)
    graph[b-1].append(a-1)

dist=[[-1, -1] for _ in range(n)]
dist[v][0]=0
dist[u][1]=0

def dfs1(graph,v):
    vv=graph[v]
    for vvv in vv:
        if dist[vvv][0]==-1:
            dist[vvv][0]=dist[v][0]+1
            dfs1(graph,vvv)

def dfs2(graph,v):
    vv=graph[v]
    for vvv in vv:
        if dist[vvv][1]==-1:
            dist[vvv][1]=dist[v][1]+1
            dfs2(graph,vvv)

dfs1(graph,v)
dfs2(graph,u)
dist.sort(reverse=True)

for i in range(10**6):
    if dist[i][0]-dist[i][1]>=1:
        print((dist[i][0]-1))
        break



f=lambda:map(int,input().split())
n,st,sa=f()
g=[set() for _ in range(n)]
for _ in range(n-1):
  a,b=f()
  g[a-1].add(b-1)
  g[b-1].add(a-1)
def bfs(s):
  l=[-1]*n; l[s]=0; q=[s]
  while q:
    v=q.pop(); d=l[v]+1
    for c in g[v]:
      if l[c]<0: l[c]=d; q+=[c]
  return l
lt=bfs(st-1)
la=bfs(sa-1)
print(max(la[i] for i in range(n) if lt[i]<la[i])-1)

nii=lambda:map(int,input().split())
from collections import deque

n,u,v=nii()
u-=1
v-=1

tree=[[] for i in range(n)]
for i in range(n-1):
  a,b=nii()
  a-=1
  b-=1
  tree[a].append(b)
  tree[b].append(a)

def BFS(s):
  dist=[-1 for i in range(n)]
  dist[s]=0

  que=deque()
  que.append(s)

  while que:
    x=que.popleft()
    for i in tree[x]:
      if dist[i]==-1:
        que.append(i)
        dist[i]=dist[x]+1
  return dist

dist_t=BFS(u)
dist_a=BFS(v)

ans=0
for i in range(n):
  if dist_a[i]>dist_t[i]:
    ans=max(ans,dist_a[i]-1)

print(ans)

N,u,v=map(int,input().split())
from collections import deque
g=[[] for _ in range(N)]
for i in range(N-1):
  A,B=map(int,input().split())
  g[A-1].append(B-1)
  g[B-1].append(A-1)
def bfs(n):
  stack=deque([])
  depth=[-1]*N
  depth[n-1]=0
  stack.append(n-1)
  while stack:
    a=stack.popleft()
    for b in g[a]:
      if depth[b]==-1:
        depth[b]=depth[a]+1
        stack.append(b)
  return depth
U=bfs(u)
V=bfs(v)
List=[]
for i in range(N):
  if len(g[i])==1 and U[i]<V[i]:
    List.append(i)
ans=0

for i in List:
  ans=max(ans,V[i]-1)

print(ans)  

N,u,v=list(map(int, input().split()))
import sys
sys.setrecursionlimit(10**6)
T=[[] for _ in range(N)]
for i in range(N-1):
    a,b=list(map(int, input().split()))
    T[a-1].append(b-1)
    T[b-1].append(a-1)


def dfs(start):
    tolist=T[start]
    nowd=done[start]
    for next_n in tolist:
        if done[next_n]==-1:
            done[next_n]=nowd+1
            dfs(next_n)

done=[-1]*N
done[u-1]=0
dfs(u-1)
dtaka=done.copy()
done=[-1]*N
done[v-1]=0
dfs(v-1)
dao=done.copy()
ans=0
for i in range(N):
    t=dtaka[i]
    a=dao[i]
    if a>t:
        ans=max(ans, a)
print((ans-1))


import sys
sys.setrecursionlimit(10 ** 6)
N, u, v = map(int, input().split())
u -= 1
v -= 1
edges = [[] for _ in range(N)]
for _ in range(N - 1):
    a, b = map(int, input().split())
    a -= 1
    b -= 1
    edges[a].append(b)
    edges[b].append(a)

dist = [0] * N
def dfs(v, d=0, p=-1):
    nonlocal dist
    dist[v] = d
    for u in edges[v]:
        if u == p:
            continue
        dfs(u, d + 1, v)

def calc_dist(s):
    nonlocal dist
    dist = [0] * N
    dfs(s)
    return dist

distS = calc_dist(u)
distT = calc_dist(v)

mx = 0
for i in range(N):
    if distS[i] < distT[i]:
        mx = max(mx, distT[i])
ans = mx - 1
print(ans)

import sys
sys.setrecursionlimit(10**6)

n, u, v = list(map(int, input().split()))
# よりたかはしくんが早く辿り着ける葉のうち、青木君から最も遠いで
u -= 1
v -= 1

g = [[] for _ in range(n)]
for _ in range(n - 1):
    a, b = [int(x) - 1 for x in input().split()]
    g[a].append(b)
    g[b].append(a)

INF = 1 << 30
t = [INF] * n
a = [INF] * n

t[u] = 0
a[v] = 0


def dfs(v, p, d):
    for nv in g[v]:
        if nv == p:
            continue
        if d[nv] == INF:
            d[nv] = d[v] + 1
            dfs(nv, v, d)


dfs(u, -1, t)
dfs(v, -1, a)

ans = 0
for i in range(n):
    if t[i] < a[i]:
        ans = max(ans, a[i] - 1)
print(ans)