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

Source: codeforces | Difficulty: MEDIUM_HARD
Tags: Graph algorithms

Graph traversal


Solutions:
import sys
input = sys.stdin.buffer.readline
from collections import deque

def prog():
	(n, x) = map(int, input().split())
	adj = [[] for i in range(n + 1)]
	for i in range(n - 1):
		(u, v) = map(int, input().split())
		adj[u].append(v)
		adj[v].append(u)
	dist = [0] * (n + 1)
	parent = [0] * (n + 1)
	visited = [0] * (n + 1)
	dist2 = [0] * (n + 1)
	visited2 = [0] * (n + 1)
	s = deque([1])
	while s:
		curr = s[-1]
		if not visited[curr]:
			visited[curr] = 1
			for neighbor in adj[curr]:
				if not visited[neighbor]:
					parent[neighbor] = curr
					s.append(neighbor)
					dist[neighbor] = dist[curr] + 1
		else:
			s.pop()
	curr = x
	moved_up = 0
	while moved_up + 1 < dist[parent[curr]]:
		curr = parent[curr]
		moved_up += 1
	s = deque([curr])
	while s:
		curr = s[-1]
		if not visited2[curr]:
			visited2[curr] = 1
			for neighbor in adj[curr]:
				if parent[curr] != neighbor:
					s.append(neighbor)
					dist2[neighbor] = dist2[curr] + 1
		else:
			s.pop()
	best_loc = 0
	best_dist = -1
	for i in range(n + 1):
		if visited2[i] and dist2[i] > best_dist:
			best_dist = dist2[i]
			best_loc = i
	print(2 * dist[best_loc])
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
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'):
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')

	def I():
		return int(input())

	def In():
		return map(int, input().split())
else:

	def I():
		return int(stdin.readline())

	def In():
		return 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):
	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)
		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
for _ in range(1):
	main()


from collections import deque
from sys import stdin

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)
(n, x) = map(int, stdin.readline().split())
distance_Alice = [-1 for i in range(n)]
distance_Bob = [-1 for i in range(n)]
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)
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)


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()


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())
		graph[a - 1].append(b - 1)
		graph[b - 1].append(a - 1)
	q = deque([0])
	parent = [-1] * len(graph)
	visit = []
	while q:
		v = q.popleft()
		visit.append(v)
		for adj in graph[v]:
			if adj != parent[v]:
				parent[adj] = v
				q.append(adj)
	cur = X
	path = [X]
	while parent[X] != -1:
		X = parent[X]
		path += [X]
	P = len(path) - 1
	toleft = parent
	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
	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)
main()


import os
import sys
from io import BytesIO, IOBase
import string
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		import os
		self.os = os
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = self.os.read(self._fd, max(self.os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = self.os.read(self._fd, max(self.os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			self.os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')

def solve():
	(n, x) = [int(s) for s in input().split(' ')]
	G = {i: [] for i in range(n)}
	for e in range(n - 1):
		(a, b) = [int(x) for x in input().split(' ')]
		G[a - 1].append(b - 1)
		G[b - 1].append(a - 1)
	dp = [float('inf')] * n
	q = [(x - 1, 0)]
	visited = set()
	visited.add(x - 1)
	dp[x - 1] = 0
	while q:
		(v, p) = q.pop()
		for u in G[v]:
			if u not in visited:
				dp[u] = p + 1
				visited.add(u)
				q.append((u, p + 1))
	q = [(0, 0)]
	visited = set()
	visited.add(0)
	dp2 = [float('inf')] * n
	dp2[0] = 0
	while q:
		(v, p) = q.pop()
		for u in G[v]:
			if u not in visited:
				dp2[u] = p + 1
				visited.add(u)
				q.append((u, p + 1))
	m = 0
	for i in range(n):
		if dp2[i] > dp[i]:
			m = max(m, 2 * dp2[i])
	return m
print(solve())


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 sys import stdin

class Node:

	def __init__(self, value=None, prox=None):
		self.value = value
		self.prox = prox

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

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

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)
(n, x) = parser()
distance_Alice = [-1 for i in range(n)]
distance_Bob = [-1 for i in range(n)]
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)
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, 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 collections import deque
from sys import stdin
INF = 10 ** 6

def bfs(start):
	dist = [INF] * n
	dist[start] = 0
	q = deque([start])
	while q:
		top = q.pop()
		for ne in nei[top]:
			if dist[ne] == INF:
				q.append(ne)
				dist[ne] = dist[top] + 1
	return dist
(n, x) = map(int, stdin.readline().split())
x -= 1
nei = []
for _ in range(n):
	nei.append([])
lines = stdin.readlines()
for line in lines[:n - 1]:
	(a, b) = map(int, line.split())
	a -= 1
	b -= 1
	nei[a].append(b)
	nei[b].append(a)
dist1 = bfs(0)
dist2 = bfs(x)
best = 0
for (d1, d2) in zip(dist1, dist2):
	if d2 < d1:
		best = max(best, d1)
print(best * 2)


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)


import sys
from collections import deque
(n, x) = map(int, sys.stdin.buffer.readline().decode('utf-8').split())
x -= 1
adj = [[] for _ in range(n)]
for (u, v) in (map(int, line.decode('utf-8').split()) for line in sys.stdin.buffer):
	adj[u - 1].append(v - 1)
	adj[v - 1].append(u - 1)
bob = 1
color = [0] * n
color[0] = 1
color[x] = 2
dq = deque()
dq.extend(((x, 2, 1), (0, 1, 2)))
while dq:
	(v, c, t) = dq.popleft()
	if c == 2 and color[v] == 1:
		continue
	for dest in adj[v]:
		if color[dest] == c or (color[dest] == 1 and c == 2):
			continue
		if c == 1 and color[dest] == 2:
			bob -= 1
			if bob == 0:
				print(t)
				exit()
		if c == 2:
			bob += 1
		color[dest] = c
		dq.append((dest, c, t + 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) = map(int, stdin.readline().split())
x = x - 1
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)
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)


import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')

def list2d(a, b, c):
	return [[c] * b for i in range(a)]

def list3d(a, b, c, d):
	return [[[d] * c for j in range(b)] for i in range(a)]

def list4d(a, b, c, d, e):
	return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]

def ceil(x, y=1):
	return int(-(-x // y))

def Yes():
	print('Yes')

def No():
	print('No')

def YES():
	print('YES')

def NO():
	print('NO')
INF = 10 ** 18
MOD = 10 ** 9 + 7
from types import GeneratorType

def bootstrap(func, stack=[]):

	def wrapped_function(*args, **kwargs):
		if stack:
			return func(*args, **kwargs)
		else:
			call = func(*args, **kwargs)
			while True:
				if type(call) is GeneratorType:
					stack.append(call)
					call = next(call)
				else:
					stack.pop()
					if not stack:
						break
					call = stack[-1].send(call)
			return call
	return wrapped_function
Ri = lambda : [int(x) for x in sys.stdin.readline().split()]
ri = lambda : sys.stdin.readline().strip()
(n, x) = Ri()
g = [[] for i in range(n)]
for i in range(n - 1):
	(a, b) = Ri()
	a -= 1
	b -= 1
	g[a].append(b)
	g[b].append(a)
areach = [0] * n
breach = [0] * n

@bootstrap
def dfs(cur, par, arr, lvl):
	arr[cur] = lvl
	for child in g[cur]:
		if child == par:
			continue
		yield dfs(child, cur, arr, lvl + 1)
	yield
dfs(0, -1, areach, 0)
dfs(x - 1, -1, breach, 0)
ans = -10 ** 18
for i in range(n):
	if breach[i] < areach[i]:
		ans = max(ans, 2 * areach[i])
print(ans)


import io, os
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
from collections import defaultdict, deque
(n, x) = list(map(int, input().split()))
edge = defaultdict(list)
child = defaultdict(list)
for i in range(n - 1):
	(u, v) = list(map(int, input().split()))
	edge[u].append(v)
	edge[v].append(u)

def bfsDistance(node):
	dist = [-1] * (n + 1)
	vis[node] = 1
	q = deque([node])
	dist[node] = 0
	temp = 0
	while q:
		cur = q.popleft()
		traverse.append(cur)
		for j in edge[cur]:
			if vis[j] == 0:
				q.append(j)
				vis[j] = 1
				dist[j] = dist[cur] + 1
	return dist

def dfsFindLeaf(node):
	vis[node] = 1
	stack = [node]
	while stack:
		cur = stack.pop()
		a = 0
		for j in edge[cur]:
			if vis[j] == 0:
				stack.append(j)
				if node == 1:
					child[cur].append(j)
				vis[j] = 1
				a += 1
		if a == 0:
			depth[cur] = 0
vis = [0] * (n + 1)
depth = [-1] * (n + 1)
dfsFindLeaf(1)
vis = [0] * (n + 1)
traverse = []
arr1 = bfsDistance(1)
vis = [0] * (n + 1)
arr2 = bfsDistance(x)
lis = []
l = 0
for i in range(1, n + 1):
	if i != 1 and i != x and (arr1[i] + arr2[i] == arr1[x]):
		lis.append(i)
		l += 1
traverse.reverse()
for i in range(n):
	if depth[traverse[i]] == -1:
		maxi = 0
		for j in child[traverse[i]]:
			maxi = max(depth[j] + 1, maxi)
		depth[traverse[i]] = maxi
ans = 2 * (arr1[x] + depth[x])
for i in range(l):
	if arr1[lis[i]] > arr2[lis[i]]:
		temp = 2 * (arr1[lis[i]] + depth[lis[i]])
		ans = max(ans, temp)
print(ans)


import sys, math, itertools
from collections import Counter, deque, defaultdict
from bisect import bisect_left, bisect_right
from heapq import heappop, heappush, heapify
mod = 10 ** 9 + 7
INF = float('inf')

def inp():
	return int(sys.stdin.readline())

def inpl():
	return list(map(int, sys.stdin.readline().split()))

def inpl_1():
	return list(map(lambda x: int(x) - 1, sys.stdin.readline().split()))

def inps():
	return sys.stdin.readline()

def inpsl(x):
	tmp = sys.stdin.readline()
	return list(tmp[:x])

def err(x):
	print(x)
	exit()
(n, X) = inpl()
X -= 1
g = [[] for _ in range(n)]
for _ in range(n - 1):
	(a, b) = inpl_1()
	g[a].append(b)
	g[b].append(a)
dist = [-1] * n
dist[0] = 0
q = deque([0])
pa = -1
while q:
	u = q.popleft()
	for v in g[u]:
		if dist[v] != -1:
			continue
		if v == X:
			pa = u
		dist[v] = dist[u] + 1
		q.append(v)
dist2 = [-1] * n
dist2[X] = 0
q = deque([X])
while q:
	u = q.popleft()
	for v in g[u]:
		if dist2[v] != -1 or dist2[u] + 1 >= dist[v]:
			continue
		dist2[v] = dist2[u] + 1
		q.append(v)
res = 0
for i in range(n):
	if dist2[i] >= 0:
		res = max(res, dist[i])
print(res * 2)