import sys

sys.setrecursionlimit(10**9)





def dfs4t(a, par):

    for child in edge[a]:

        if child == par:

            continue

        else:

            cost4t[child] = cost4t[a] + 1

            dfs4t(child, a)

    return



def dfs4a(a, par):

    for child in edge[a]:

        if child == par:

            continue

        else:

            cost4a[child] = cost4a[a] + 1

            dfs4a(child, a)

    return





N, u, v = list(map(int, input().split()))

edge = [[] for _ in range(N)]

cost4t = [10**5 for i in range(N)]

cost4a = [10**5 for i in range(N)]



for _ in range(N-1):

    a, b = list(map(int, input().split()))

    a -= 1

    b -= 1

    edge[a].append(b)

    edge[b].append(a)



u -= 1

v -= 1

cost4t[u] = 0

cost4a[v] = 0

dfs4t(u, 10**5)

dfs4a(v, 10**5)



ans = 0

for t, a, in zip(cost4t, cost4a):

    if a  >= t:

        ans = max(ans, t + a - (t + 1))

print(ans)
import sys

from collections import deque, defaultdict

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



def main():

    N, u, v = list(map(int, input().split()))

    u -= 1

    v -= 1

    

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



    to = [[] for _ in range(N)]

    for a, b in edges:

        a -= 1

        b -= 1

        to[a].append(b)

        to[b].append(a)

    

    dist  = {key:defaultdict(int) for key in [u, v]}



    def bfs(key):

        q = deque()

        q.append(key)

        while(len(q)):

            now = q.popleft()

            for nv in to[now]:

                if nv == key or dist[key][nv]:

                    continue

                else:

                    dist[key][nv] = dist[key][now] + 1

                    q.append(nv)

    

    for key in [u, v]:

        bfs(key)

    

    ans = 0

    for i in range(N):

        diff = dist[v][i] - dist[u][i]

        if diff > 0:

            ans = max(ans, dist[v][i]-1)



    print(ans)    



if __name__ == "__main__":

    main()
import sys

sys.setrecursionlimit(10**9)


def dfs4t(a, par):
    for child in edge[a]:
        if child == par:
            continue
        else:
            cost4t[child] = cost4t[a] + 1
            dfs4t(child, a)
    return


def dfs4a(a, par):
    for child in edge[a]:
        if child == par:
            continue
        else:
            cost4a[child] = cost4a[a] + 1
            dfs4a(child, a)
    return


N, u, v = list(map(int, input().split()))
edge = [[] for _ in range(N)]
cost4t = [10**5 for i in range(N)]
cost4a = [10**5 for i in range(N)]
for _ in range(N - 1):
    a, b = list(map(int, input().split()))
    a -= 1
    b -= 1
    edge[a].append(b)
    edge[b].append(a)
u -= 1
v -= 1
cost4t[u] = 0
cost4a[v] = 0
dfs4t(u, 10**5)
dfs4a(v, 10**5)
ans = 0
for (
    t,
    a,
) in zip(cost4t, cost4a):
    if a >= t:
        ans = max(ans, t + a - (t + 1))
print(ans)

import sys
from collections import deque, defaultdict


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


def main():
    N, u, v = list(map(int, input().split()))
    u -= 1
    v -= 1
    edges = [list(map(int, input().split())) for _ in range(N - 1)]
    to = [[] for _ in range(N)]
    for a, b in edges:
        a -= 1
        b -= 1
        to[a].append(b)
        to[b].append(a)
    dist = {key: defaultdict(int) for key in [u, v]}

    def bfs(key):
        q = deque()
        q.append(key)
        while len(q):
            now = q.popleft()
            for nv in to[now]:
                if nv == key or dist[key][nv]:
                    continue
                else:
                    dist[key][nv] = dist[key][now] + 1
                    q.append(nv)

    for key in [u, v]:
        bfs(key)
    ans = 0
    for i in range(N):
        diff = dist[v][i] - dist[u][i]
        if diff > 0:
            ans = max(ans, dist[v][i] - 1)
    print(ans)


if __name__ == "__main__":
    main()

- -sys.setrecursionlimit(10**9) +from collections import deque, defaultdict -def dfs4t(a, par): -    for child in edge[a]: -        if child == par: -            continue -        else: -            cost4t[child] = cost4t[a] + 1 -            dfs4t(child, a) -    return +def input(): +    return sys.stdin.readline().strip() -def dfs4a(a, par): -    for child in edge[a]: -        if child == par: -            continue -        else: -            cost4a[child] = cost4a[a] + 1 -            dfs4a(child, a) -    return +def main(): +    N, u, v = list(map(int, input().split())) +    u -= 1 +    v -= 1 +    edges = [list(map(int, input().split())) for _ in range(N - 1)] +    to = [[] for _ in range(N)] +    for a, b in edges: +        a -= 1 +        b -= 1 +        to[a].append(b) +        to[b].append(a) +    dist = {key: defaultdict(int) for key in [u, v]} + +    def bfs(key): +        q = deque() +        q.append(key) +        while len(q): +            now = q.popleft() +            for nv in to[now]: +                if nv == key or dist[key][nv]: +                    continue +                else: +                    dist[key][nv] = dist[key][now] + 1 +                    q.append(nv) + +    for key in [u, v]: +        bfs(key) +    ans = 0 +    for i in range(N): +        diff = dist[v][i] - dist[u][i] +        if diff > 0: +            ans = max(ans, dist[v][i] - 1) +    print(ans) -N, u, v = list(map(int, input().split())) -edge = [[] for _ in range(N)] -cost4t = [10**5 for i in range(N)] -cost4a = [10**5 for i in range(N)] -for _ in range(N - 1): -    a, b = list(map(int, input().split())) -    a -= 1 -    b -= 1 -    edge[a].append(b) -    edge[b].append(a) -u -= 1 -v -= 1 -cost4t[u] = 0 -cost4a[v] = 0 -dfs4t(u, 10**5) -dfs4a(v, 10**5) -ans = 0 -for ( -    t, -    a, -) in zip(cost4t, cost4a): -    if a >= t: -        ans = max(ans, t + a - (t + 1)) -print(ans) +if __name__ == "__main__": +    main()