Alice got tired of playing the tag game by the usual rules so she offered Bob a little modification to it. Now the game should be played on an undirected rooted tree of n vertices. Vertex 1 is the root of the tree.

Alice starts at vertex 1 and Bob starts at vertex x (x ≠ 1). The moves are made in turns, Bob goes first. In one move one can either stay at the current vertex or travel to the neighbouring one.

The game ends when Alice goes to the same vertex where Bob is standing. Alice wants to minimize the total number of moves and Bob wants to maximize it.

You should write a program which will determine how many moves will the game last.


-----Input-----

The first line contains two integer numbers n and x (2 ≤ n ≤ 2·10^5, 2 ≤ x ≤ n).

Each of the next n - 1 lines contains two integer numbers a and b (1 ≤ a, b ≤ n) — edges of the tree. It is guaranteed that the edges form a valid tree.


-----Output-----

Print the total number of moves Alice and Bob will make.


-----Examples-----
Input
4 3
1 2
2 3
2 4

Output
4

Input
5 2
1 2
2 3
3 4
2 5

Output
6



-----Note-----

In the first example the tree looks like this:

 [Image] 

The red vertex is Alice's starting position, the blue one is Bob's. Bob will make the game run the longest by standing at the vertex 3 during all the game. So here are the moves:

B: stay at vertex 3

A: go to vertex 2

B: stay at vertex 3

A: go to vertex 3

In the second example the tree looks like this:

 [Image] 

The moves in the optimal strategy are:

B: go to vertex 3

A: go to vertex 2

B: go to vertex 4

A: go to vertex 3

B: stay at vertex 4

A: go to vertex 4

Difficulty: interview

Starter code:

Solutions:
from collections import deque
from sys import stdin

n, x = map(int, stdin.readline().split())
leafs = set(range(n))
graph = [[] for i in range(n)]
count = [False for i in range(n)]
for i in range(n - 1):
    a, b = map(int, stdin.readline().split())
    graph[a - 1].append(b - 1)
    graph[b - 1].append(a - 1)
    if not count[a - 1]:
        count[a - 1] = True
    else:
        leafs.discard(a - 1)
    if not count[b - 1]:
        count[b - 1] = True
    else:
        leafs.discard(b - 1)
queue = deque()
way_a = [10 ** 6 for i in range(n)]
way_b = [10 ** 6 for i in range(n)]
used = [False for i in range(n)]
queue.append([0, 0])
while queue:
    j = queue.popleft()
    way_a[j[0]] = min(way_a[j[0]], j[1])
    for i in graph[j[0]]:
        if not used[i]:
            used[i] = True
            queue.append([i, j[1] + 1])
queue.append([x - 1, 0])
used = [False for i in range(n)]
while queue:
    j = queue.popleft()
    way_b[j[0]] = min(way_b[j[0]], j[1])
    for i in graph[j[0]]:
        if not used[i]:
            used[i] = True
            queue.append([i, j[1] + 1])
res = way_a[x - 1]
for i in leafs:
    if way_a[i] > way_b[i]:
        res = max(res, way_a[i])
print(res * 2)

from collections import deque
from sys import stdin

n, x = map(int, stdin.readline().split())
graph = [[] for i in range(n)]
for i in range(n - 1):
    a, b = map(int, stdin.readline().split())
    graph[a - 1].append(b - 1)
    graph[b - 1].append(a - 1)
queue = deque()
way_a = [10 ** 6 for i in range(n)]
way_b = [10 ** 6 for i in range(n)]
used = [False for i in range(n)]
queue.append([0, 0])
while queue:
    j = queue.popleft()
    way_a[j[0]] = min(way_a[j[0]], j[1])
    for i in graph[j[0]]:
        if not used[i]:
            used[i] = True
            queue.append([i, j[1] + 1])
queue.append([x - 1, 0])
used = [False for i in range(n)]
while queue:
    j = queue.popleft()
    way_b[j[0]] = min(way_b[j[0]], j[1])
    for i in graph[j[0]]:
        if not used[i]:
            used[i] = True
            queue.append([i, j[1] + 1])
res = way_a[x - 1]
for i in range(n):
    if way_a[i] > way_b[i]:
        res = max(res, way_a[i])
print(res * 2)

#!/usr/bin/pypy3

from sys import stdin,stderr
from collections import defaultdict

def readInts(): return map(int,stdin.readline().strip().split())
def print_err(*args,**kwargs): print(*args,file=stderr,**kwargs)
    
def dfs(n,g,b):
    s = [(1,0,1)]
    b_depth = 0
    parents = [ None for _ in range(n+1) ]
    while s:
        node,depth,pn = s.pop()
        parents[node] = pn
        if node==b:
            b_depth = depth
            break
        depth += 1
        for nn in g[node]:
            if nn == pn: continue
            s.append( (nn,depth,node) )
        
    anc_node = b
    for _ in range((b_depth-1)//2):
        anc_node = parents[anc_node]
    s = [(1,False,0,1)]
    best = 0
    while s:
        node,bd,depth,pn = s.pop()
        bd |= node==anc_node
        if bd and best < depth:
            best = depth
        depth += 1
        for nn in g[node]:
            if nn == pn: continue
            s.append( (nn,bd,depth,node) )
    return best

def run():
    n,x = readInts()
    g = defaultdict(list)
    for _ in range(n-1):
        a,b = readInts()
        g[a].append(b)
        g[b].append(a)
    print(dfs(n,g,x)*2)
        
run()


from sys import stdin,stderr
from collections import defaultdict,deque
def main():
    n,x=map(int,stdin.readline().strip().split())
    X=x-1
    graph=[[] for i in range(n)]
    for i in range(n):
        graph[i]=[]
    for _ in range(n-1):
        a,b=map(int,stdin.readline().strip().split())
        #a,b=a-1,b-1
        graph[a-1].append(b-1)
        graph[b-1].append(a-1)
    #def bfs(graph):
    q=deque([0])
        #level=[0 for i in range(len(graph))]
    parent=[-1]*len(graph)
    visit=[]
    while q:
        v=q.popleft()
        visit.append(v)
        for adj in graph[v]:
            if adj!=parent[v]:
                    #level[adj]=level[v]+1
                parent[adj]=v
                q.append(adj)

        #return parent,visit


    #parent,visit=bfs(graph) 
    #print(level,parent,visit)
    
    cur=X
    path=[X]
    #print(path,'path')
    while(parent[X]!=-1):
        X=parent[X]
        path+=[X]

    P=len(path)-1  
    
    toleft=parent
    #bfsvisit=visit[::-1]
    for i in range(1,len(visit)):
        x=visit[-i]
        
        if [parent[x]]==graph[x]:
            toleft[x]=0
        else:
            ans=0
            for adj in graph[x]:
                if adj!=parent[x]:
                    ans=max(toleft[adj],ans)
            ans+=1
            toleft[x]=ans
   

    #print(path,toleft) 
    #print(P,'P')
    MAX=0
    
    for t in range(0,P+1):
        if 2*t<P:
            temp=2*t+(2*toleft[path[t]]+2*abs(P-2*t))
            if temp>MAX:
                MAX=temp
        else:
            break

    print(MAX)    


    #print(toleft[2],path)    
main()

def prog():
    from sys import stdin
    from collections import deque
    n,x = map(int,stdin.readline().split())
    x-=1
    d = [[] for i in range(n)]
    for i in range(n-1):
        a,b = map(int,stdin.readline().split())
        d[a-1].append(b-1)
        d[b-1].append(a-1)
    Alice = [1000000 for i in range(n)]
    Bob = [1000000 for i in range(n)]
    queue=deque()
    queue.append([0,0])
    while queue:
        q1 = queue.popleft()
        Alice[q1[0]]=min(Alice[q1[0]],q1[1])
        for i,item in enumerate(d[q1[0]]):
            if Alice[item]==1000000:
                queue.append([item,q1[1]+1])
    queue.append([x,0])
    while queue:
        q2 = queue.popleft()
        Bob[q2[0]]=min(Bob[q2[0]],q2[1])
        for i,item in enumerate(d[q2[0]]):
            if Bob[item]==1000000:
                queue.append([item,q2[1]+1])
    res = 0
    for i in range(n):
        A=Alice[i]
        if A>Bob[i]:
            if A>res:
                res=A
    print(2*res)
prog()

import sys
from math import *

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

def mint():
	return int(minp())

def mints():
	return list(map(int, minp().split()))

n, x = mints()
e = [[] for i in range(n)]
for i in range(n-1):
	a, b = mints()
	e[a-1].append(b-1)
	e[b-1].append(a-1)
x -= 1
q = [0]*n
d = [0]*n
p = [-1]*n
ql = 0
qr = 1 
q[0] = x
d[x] = 1 
while ql < qr:
	u = q[ql]
	ql += 1
	for v in e[u]:
		if d[v] == 0:
			d[v] = d[u] + 1
			p[v] = u
			q[qr] = v
			qr += 1
#v = 0
#u = 1
#while v != -1:
#	if d[v] >= u:
#		d[v] = 0
#	v = p[v]
#	u += 1
#print(d)
dd = [0]*n
ql = 0
qr = 1 
q[0] = 0
dd[0] = 1 
while ql < qr:
	u = q[ql]
	ql += 1
	for v in e[u]:
		if dd[v] == 0:
			dd[v] = dd[u] + 1
			q[qr] = v
			qr += 1

r = 0 
for i in range(n):
	if d[i] < dd[i]:
		r = max(r, dd[i])
print((r-1)*2)


from sys import stdin, stdout
import math,sys,heapq
from itertools import permutations, combinations
from collections import defaultdict,deque,OrderedDict
from os import path
import bisect as bi
def yes():print('YES')
def no():print('NO')
if (path.exists('input.txt')): 
    #------------------Sublime--------------------------------------#
    sys.stdin=open('input.txt','r');sys.stdout=open('output.txt','w');
    def I():return (int(input()))
    def In():return(list(map(int,input().split())))
else:
    #------------------PYPY FAst I/o--------------------------------#
    def I():return (int(stdin.readline()))
    def In():return(list(map(int,stdin.readline().split())))

def dict(a):
    d={}
    z=[]
    for x in a:
        if d.get(x,-1)!=-1:
            d[x]+=1
        else:
            d[x]=1
            z.append(x)
    return [d,z]

def find_gt(a, x):
    'Find leftmost value greater than x'
    i = bi.bisect_right(a, x)
    if i != len(a):
        return i
    else:            
        return -1



def dfs(d,n,s):
    alice=[0]*(n+1)
    visit=[False]*(n+1)
    parent=[-1]*(n+1)
    parent[1]=0
    q=deque([s])
    cnt=0
    while q:
        temp=q.popleft()
        
        for x in d[temp]:
            if visit[x]==False:
                q.appendleft(x)
                parent[x]=temp
                alice[x]=alice[temp]+1
        visit[temp]=True
    return alice
def main():
    try:
        n,s=In()
        d=defaultdict(list)
        for x in range(n-1):
            a,b=In()
            d[a].append(b)
            d[b].append(a)
        
        alice=dfs(d,n,1)
        bob=dfs(d,n,s)
        #print(alice)
        ans=0
        for i in range(1,n+1):
            if bob[i]<alice[i]:
                ans=max(ans,2*(alice[i]))
        print(ans)
   

    except:
        pass
        
M = 998244353
P = 1000000007
 
def __starting_point():
    #for _ in range(I()):main()
    for _ in range(1):main()



__starting_point()

from sys import stdin

def put(): return map(int, stdin.readline().split())

def dfs():
    s = [(1,0)]
    vis = [0]*(n+1)
    while s:
        i,p = s.pop()
        if vis[i]==0:
            vis[i]=1
            parent[i]=p
            alice[i]=alice[p]+1
            s.append((i,p))
            for j in tree[i]:
                if j!=p:
                    s.append((j,i))
        elif vis[i]==1:
            vis[i]=2
            for j in tree[i]:
                if j != p:
                    depth[i]= max(depth[i], depth[j]+1)        




n,k = put()
tree = [[] for i in range(n+1)]
alice, depth, parent = [0]*(n+1), [0]*(n+1), [0]*(n+1)
for _ in range(n-1):
    x,y = put()
    tree[x].append(y)
    tree[y].append(x)
dfs()
ans = 0
i = k
time = 1
while i!=0:
    if time<alice[i]:
        ans = max(ans, alice[i]+depth[i])
    i = parent[i]
    time+=1

print(2*ans-2)

from sys import stdin
from collections import deque

def bfs(G, s):
    Q = deque()
    Q.append(s)
    infinite = 10 ** 6
    d = [infinite]*n
    d[s] = 0
    while Q:
        u = Q.popleft()
        for v in graph[u]:
            if d[v] == infinite:
                d[v] = d[u] + 1
                Q.append(v)
    return d

n, x = list(map(int, stdin.readline().split()))
x = x - 1

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

d_Alice = bfs(graph, 0)
d_Bob = bfs(graph, x)

resp = d_Alice[x]
for i,v in enumerate(graph):
    if len(v) == 1 and d_Alice[i] > d_Bob[i]:
        resp = max(resp, d_Alice[i])
print(2*resp)


from collections import deque
from sys import stdin

#Algoritmo BFS
def BFS(s):
    if s==0:
        distance=distance_Alice
    else:
        distance=distance_Bob

    distance[s]=0
    q=deque()
    q.append(s)
    while len(q)>0:
        v=q.popleft()
        for u in adjacents_list[v]:
            if distance[u] == -1:
                distance[u]=distance[v]+1
                q.append(u)    

#Recibiendo los valores de n y x
n,x=map(int, stdin.readline().split())

#Creando los arrays necesarios para la ejecucion de DFS
#visitados
distance_Alice=[-1 for i in range(n)]
distance_Bob=[-1 for i in range(n)]

#Armando el arbol
adjacents_list=[[] for i in range(n)]
for i in range(n-1):
    v1,v2=map(int, stdin.readline().split())
    adjacents_list[v1-1].append(v2-1)
    adjacents_list[v2-1].append(v1-1)

BFS(0)

BFS(x-1)

#Hallando el nodo mas alejado a Alice que puede alcanzar Bob antes que lo alcance Alice
max=0
for i in range(n):
    if max<distance_Alice[i] and distance_Bob[i]<distance_Alice[i]:
        max=distance_Alice[i]

print(max*2)

from collections import deque
from sys import stdin

#Algoritmo BFS
def BFS(s):
    if s==0:
        distance=distance_Alice
    else:
        distance=distance_Bob

    distance[s]=0
    q=deque()
    q.append(s)
    while len(q)>0:
        v=q.popleft()
        for u in adjacents_list[v]:
            if distance[u] == -1:
                distance[u]=distance[v]+1
                q.append(u)    

#Recibiendo los valores de n y x
n,x=map(int, stdin.readline().split())

#Creando los arrays necesarios para la ejecucion de DFS
#visitados
distance_Alice=[-1 for i in range(n)]
distance_Bob=[-1 for i in range(n)]

#Armando el arbol
adjacents_list=[[] for i in range(n)]
for i in range(n-1):
    v1,v2=map(int, stdin.readline().split())
    adjacents_list[v1-1].append(v2-1)
    adjacents_list[v2-1].append(v1-1)

BFS(0)

BFS(x-1)

#Hallando el nodo mas alejado a Alice que puede alcanzar Bob antes que lo alcance Alice
max=0
for i in range(n):
    if max<distance_Alice[i] and distance_Bob[i]<distance_Alice[i]:
        max=distance_Alice[i]

print(max*2)

from sys import stdin

#Clase nodo
class Node():
    def __init__(self,value=None,prox=None):
        self.value=value
        self.prox=prox
#Clase cola      
class Queue:
    def __init__(self):
        self.first=None
        self.last=None
        self.count=0

    def Enqueue(self,value):
        new_node=Node(value)
        self.count+=1
        if self.first is None:
            self.last=self.first=new_node
        else:
            self.last.prox=new_node
            self.last=new_node
    
    def Dequeue(self):
        value=self.first.value
        self.count-=1
        self.first=self.first.prox
        return value

#Parser
def parser():
    return [int(x) for x in input().split(" ")]

#Algoritmo BFS
def BFS(s):
    if s==0:
        distance=distance_Alice
    else:
        distance=distance_Bob

    distance[s]=0
    q=Queue()
    q.Enqueue(s)
    while q.count>0:
        v=q.Dequeue()
        for u in adjacents_list[v]:
            if distance[u] == -1:
                distance[u]=distance[v]+1
                q.Enqueue(u)    

#Recibiendo los valores de n y x
n,x=map(int, stdin.readline().split())

#Creando los arrays necesarios para la ejecucion de DFS
#visitados
distance_Alice=[-1 for i in range(n)]
distance_Bob=[-1 for i in range(n)]

#Armando el arbol
adjacents_list=[[] for i in range(n)]
for i in range(n-1):
    v1,v2=map(int, stdin.readline().split())
    adjacents_list[v1-1].append(v2-1)
    adjacents_list[v2-1].append(v1-1)

BFS(0)

BFS(x-1)

#Hallando el nodo mas alejado a Alice que puede alcanzar Bob antes que lo alcance Alice
max=0
for i in range(n):
    if max<distance_Alice[i] and distance_Bob[i]<distance_Alice[i]:
        max=distance_Alice[i]

print(max*2)

from sys import stdin

#Clase nodo
class Node():
    def __init__(self,value=None,prox=None):
        self.value=value
        self.prox=prox
#Clase cola      
class Queue:
    def __init__(self):
        self.first=None
        self.last=None
        self.count=0

    def Enqueue(self,value):
        new_node=Node(value)
        self.count+=1
        if self.first is None:
            self.last=self.first=new_node
        else:
            self.last.prox=new_node
            self.last=new_node
    
    def Dequeue(self):
        value=self.first.value
        self.count-=1
        self.first=self.first.prox
        return value

#Parser
def parser():
    return map(int, stdin.readline().split())

#Algoritmo BFS
def BFS(s):
    if s==0:
        distance=distance_Alice
    else:
        distance=distance_Bob

    distance[s]=0
    q=Queue()
    q.Enqueue(s)
    while q.count>0:
        v=q.Dequeue()
        for u in adjacents_list[v]:
            if distance[u] == -1:
                distance[u]=distance[v]+1
                q.Enqueue(u)    

#Recibiendo los valores de n y x
n,x=parser()

#Creando los arrays necesarios para la ejecucion de DFS
#visitados
distance_Alice=[-1 for i in range(n)]
distance_Bob=[-1 for i in range(n)]

#Armando el arbol
adjacents_list=[[] for i in range(n)]
for i in range(n-1):
    v1,v2=parser()
    adjacents_list[v1-1].append(v2-1)
    adjacents_list[v2-1].append(v1-1)

BFS(0)

BFS(x-1)

#Hallando el nodo mas alejado a Alice que puede alcanzar Bob antes que lo alcance Alice
max=0
for i in range(n):
    if max<distance_Alice[i] and distance_Bob[i]<distance_Alice[i]:
        max=distance_Alice[i]

print(max*2)

from collections import deque
from sys import stdin

#parser
def parser():
    return map(int, stdin.readline().split())
#Algoritmo BFS
def BFS(s):
    if s==0:
        distance=distance_Alice
    else:
        distance=distance_Bob

    distance[s]=0
    q=deque()
    q.append(s)
    while len(q)>0:
        v=q.popleft()
        for u in adjacents_list[v]:
            if distance[u] == -1:
                distance[u]=distance[v]+1
                q.append(u)    

#Recibiendo los valores de n y x
n,x=parser()

#Creando los arrays necesarios para la ejecucion de DFS
#visitados
distance_Alice=[-1 for i in range(n)]
distance_Bob=[-1 for i in range(n)]

#Armando el arbol
adjacents_list=[[] for i in range(n)]
for i in range(n-1):
    v1,v2=parser()
    adjacents_list[v1-1].append(v2-1)
    adjacents_list[v2-1].append(v1-1)

BFS(0)

BFS(x-1)

#Hallando el nodo mas alejado a Alice que puede alcanzar Bob antes que lo alcance Alice
max=0
for i in range(n):
    if max<distance_Alice[i] and distance_Bob[i]<distance_Alice[i]:
        max=distance_Alice[i]

print(max*2)

from collections import deque
from sys import stdin

#parser
def parser():
    return map(int, stdin.readline().split())
#Algoritmo BFS
def BFS(s):
    if s==0:
        distance=distance_Alice
    else:
        distance=distance_Bob

    distance[s]=0
    q=deque()
    q.append(s)
    while len(q)>0:
        v=q.popleft()
        for u in adjacents_list[v]:
            if distance[u] == -1:
                distance[u]=distance[v]+1
                q.append(u)    

#Recibiendo los valores de n y x
n,x=parser()

#Creando los arrays necesarios para la ejecucion de DFS
#visitados
distance_Alice=[-1 for i in range(n)]
distance_Bob=[-1 for i in range(n)]

#Armando el arbol
adjacents_list=[[] for i in range(n)]
for i in range(n-1):
    v1,v2=parser()
    adjacents_list[v1-1].append(v2-1)
    adjacents_list[v2-1].append(v1-1)

BFS(0)

BFS(x-1)

#Hallando el nodo mas alejado a Alice que puede alcanzar Bob antes que lo alcance Alice
max=0
for i in range(n):
    if max<distance_Alice[i] and distance_Bob[i]<distance_Alice[i]:
        max=distance_Alice[i]

print(max*2)

from collections import deque
from sys import stdin

#parser
def parser():
    return [int(i) for i in stdin.readline().split(" ")]
#Algoritmo BFS
def BFS(s):
    if s==0:
        distance=distance_Alice
    else:
        distance=distance_Bob

    distance[s]=0
    q=deque()
    q.append(s)
    while len(q)>0:
        v=q.popleft()
        for u in adjacents_list[v]:
            if distance[u] == -1:
                distance[u]=distance[v]+1
                q.append(u)    

#Recibiendo los valores de n y x
n,x=parser()

#Creando los arrays necesarios para la ejecucion de DFS
#visitados
distance_Alice=[-1 for i in range(n)]
distance_Bob=[-1 for i in range(n)]

#Armando el arbol
adjacents_list=[[] for i in range(n)]
for i in range(n-1):
    edge=parser()
    adjacents_list[edge[0]-1].append(edge[1]-1)
    adjacents_list[edge[1]-1].append(edge[0]-1)

BFS(0)

BFS(x-1)

#Hallando el nodo mas alejado a Alice que puede alcanzar Bob antes que lo alcance Alice
max=0
for i in range(n):
    if max<distance_Alice[i] and distance_Bob[i]<distance_Alice[i]:
        max=distance_Alice[i]

print(max*2)

from sys import stdin

#Clase nodo
class Node():
    def __init__(self,value=None,prox=None):
        self.value=value
        self.prox=prox
#Clase cola      
class Queue:
    def __init__(self):
        self.first=None
        self.last=None
        self.count=0

    def Enqueue(self,value):
        new_node=Node(value)
        self.count+=1
        if self.first is None:
            self.last=self.first=new_node
        else:
            self.last.prox=new_node
            self.last=new_node
    
    def Dequeue(self):
        value=self.first.value
        self.count-=1
        self.first=self.first.prox
        return value

#Parser
def parser():
    return map(int, stdin.readline().split())

#Algoritmo BFS
def BFS(s):
    if s==0:
        distance=distance_Alice
    else:
        distance=distance_Bob

    distance[s]=0
    q=Queue()
    q.Enqueue(s)
    while q.count>0:
        v=q.Dequeue()
        for u in adjacents_list[v]:
            if distance[u] == -1:
                distance[u]=distance[v]+1
                q.Enqueue(u)    

#Recibiendo los valores de n y x
n,x=parser()

#Creando los arrays necesarios para la ejecucion de DFS
#visitados
distance_Alice=[-1 for i in range(n)]
distance_Bob=[-1 for i in range(n)]

#Armando el arbol
adjacents_list=[[] for i in range(n)]
for i in range(n-1):
    v1,v2=parser()
    adjacents_list[v1-1].append(v2-1)
    adjacents_list[v2-1].append(v1-1)
BFS(0)

BFS(x-1)

#Hallando el nodo mas alejado a Alice que puede alcanzar Bob antes que lo alcance Alice
max=0
for i in range(n):
    if max<distance_Alice[i] and distance_Bob[i]<distance_Alice[i]:
        max=distance_Alice[i]

print(max*2)

from collections import deque
from sys import stdin

#parser
def parser():
    return map(int, stdin.readline().split())
#Algoritmo BFS
def BFS(s):
    distance=[-1 for i in range(n)]
    distance[s]=0
    q=deque()
    q.append(s)
    while len(q)>0:
        v=q.popleft()
        for u in adjacents_list[v]:
            if distance[u] == -1:
                distance[u]=distance[v]+1
                q.append(u)
    return distance    

#Recibiendo los valores de n y x
n,x=parser()

#Armando el arbol
adjacents_list=[[] for i in range(n)]
for i in range(n-1):
    v1,v2=parser()
    adjacents_list[v1-1].append(v2-1)
    adjacents_list[v2-1].append(v1-1)

distance_Alice=BFS(0)

distance_Bob=BFS(x-1)

#Hallando el nodo mas alejado a Alice que puede alcanzar Bob antes que lo alcance Alice
max=0
for i in range(n):
    if max<distance_Alice[i] and distance_Bob[i]<distance_Alice[i]:
        max=distance_Alice[i]

print(max*2)

from collections import deque
from sys import stdin

#parsea una linea
def parser():
    return map(int, stdin.readline().split())
#Metodo usado para calcular las distancia de un vertice al resto de los vertices
def BFS(s):
    distance=[-1 for i in range(n)]
    distance[s]=0
    q=deque()
    q.append(s)
    while len(q)>0:
        v=q.popleft()
        for u in adjacents_list[v]:
            if distance[u] == -1:
                distance[u]=distance[v]+1
                q.append(u)
    return distance    

#Recibiendo los valores de n y x
n,x=parser()

#Armando el arbol
adjacents_list=[[] for i in range(n)]
for i in range(n-1):
    #Leyendo una arista
    v1,v2=parser()
    adjacents_list[v1-1].append(v2-1)
    adjacents_list[v2-1].append(v1-1)

#Hallando las distancias
distance_Alice=BFS(0)
distance_Bob=BFS(x-1)

#Obteniendo la distancia mayor desde el vertice 1 hasta un vertice alcanzable por Bob
max_distance=0
for i in range(n):
    if max_distance<distance_Alice[i] and distance_Bob[i]<distance_Alice[i]:
        max_distance=distance_Alice[i]

#Imprimiendo el resultado
print(max_distance*2)