import java.io.*;
import java.io.PrintWriter;
import java.util.*;
import java.io.InputStream;
import java.io.DataInputStream;

public class Main
{ //static{ System.out.println(" hello ");}

  public static void main(String[] args) throws IOException
     {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        FastReader scn = new FastReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);

       int n=scn.ni();
       int a[]=new int[n];
       for (int i=0;i<n;i++)
       {
       a[i]=scn.ni();

        }
        Arrays.sort(a); int cnt=0;
        for (int i=1;i<n;i++)
        {
            int temp=a[i-1]+1;
            cnt+=temp-a[i];
            a[i]=temp;

        }
        out.println(cnt);
          out.close();
     }

   }
  class num //implements Comparator<num>
    {

        public int start;
        public int end;
        num()
        {

        }
        num(int x,int y)
        {
            this.start = x;
            this.end =y;
        }
        public int getstart()
        {
        return start;
        }
        public int getend()
        {
        return end;
        }
    /*    public boolean equals(Object n1)
        {num n=(num)n1;
        if (this.start==n.start && this.end==n.end) return true;
        else return false;

        }

         public int hashCode()
         {
        final int prime = 31;
        int result = 1;
        result = prime * result
                + start;
        result = prime * result+end;

        return result;
       }

       public int compare(num o1, num o2) {
              if (o1.getstart() > o2.getstart())
            return (o1.getstart()-o2.getstart());
            else if (o1.getstart() == o2.getstart())
            return (o1.getend()-o2.getend());
            else
            return (o1.getstart()-o2.getstart());

        }
      */

    }

class FastReader
{

    public InputStream stream;
    private byte[] buf = new byte[1024];
    private int curChar;
    private int numChars;
    private SpaceCharFilter filter;

    public FastReader(InputStream stream)
    {
        this.stream = stream;
    }

    public int read()
    {
        if (numChars == -1)
        {
            throw new InputMismatchException ();
        }
        if (curChar >= numChars)
        {
            curChar = 0;
            try
            {
                numChars = stream.read (buf);
            } catch (IOException e)
            {
                throw new InputMismatchException ();
            }
            if (numChars <= 0)
            {
                return -1;
            }
        }
        return buf[curChar++];
    }

 public char nc()
    {
        int c = read ();
        while (isSpaceChar (c))
            c = read ();
        return (char) c;
    }

    public int ni()
    {
        int c = read ();
        while (isSpaceChar (c))
            c = read ();
        int sgn = 1;
        if (c == '-')
        {
            sgn = -1;
            c = read ();
        }
        int res = 0;
        do
        {
            if (c < '0' || c > '9')
            {

            }
            res *= 10;
            res += c - '0';
            c = read ();
        } while (!isSpaceChar (c));
        return res * sgn;
    }

    public long nl()
    {
        int c = read ();
        while (isSpaceChar (c))
            c = read ();
        int sgn = 1;
        if (c == '-')
        {
            sgn = -1;
            c = read ();
        }
        long res = 0;
        do
        {
            if (c < '0' || c > '9')
            {
                throw new InputMismatchException ();
            }
            res *= 10;
            res += c - '0';
            c = read ();
        } while (!isSpaceChar (c));
        return res * sgn;
    }

    public String ns()
    {
        int c = read ();
        while (isSpaceChar (c))
            c = read ();
        StringBuilder res = new StringBuilder ();
        do
        {
            res.appendCodePoint (c);
            c = read ();
        } while (!isSpaceChar (c));
        return res.toString ();
    }

    public boolean isSpaceChar(int c)
    {
        if (filter != null)
        {
            return filter.isSpaceChar (c);
        }
        return isWhitespace (c);
    }

    public static boolean isWhitespace(int c)
    {
        return c==' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
    }

    public String next()
    {
        return ns ();
    }

    public interface SpaceCharFilter
    {
        public boolean isSpaceChar(int ch);
    }
}


import java.util.Arrays;
import java.util.Scanner;

public class B546 {
public static void main(String[] args) {
	Scanner sc = new Scanner(System.in);
	int n = sc.nextInt();
	int a[]=new int[n];
	for(int i =0;i<n;i++)
		a[i]=sc.nextInt();

	Arrays.sort(a);
	int count=0;
	int x=a[0];
	for(int i =1;i<n;i++)
	{
		if(a[i]<=a[i-1]){
			count+=a[i]+1-a[i-1];
			a[i]++;
		}
	}
	System.out.println(count);
}
}


import java.util.Arrays;
import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;

public class B546 {
public static void main(String[] args) {
	Scanner sc = new Scanner(System.in);
	int n = sc.nextInt();
	int a[]=new int[n];
	int count=0;
	Set<Integer> set = new HashSet<>();
	for(int i =0;i<n;i++){
		a[i]=sc.nextInt();

		while(set.contains(a[i])){

		a[i]++;
		count++;

		}
		set.add(a[i]);
	}

	System.out.println(count);

}
}


import java.util.Arrays;
import java.util.Scanner;

public class B546 {
public static void main(String[] args) {
	Scanner sc = new Scanner(System.in);
	int n = sc.nextInt();
	int a[]=new int[n];
	for(int i =0;i<n;i++)
		a[i]=sc.nextInt();

	Arrays.sort(a);
	int count=0;
	for(int i =0;i<n;i++)
	{
		if(a[i]<i+1){
			count+=i+1-a[i];
		}
	}
	System.out.println(count);
}
}


import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.IOException;
import java.util.StringTokenizer;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 * @author Hieu Le
 */
public class Main {
	public static void main(String[] args) {
		InputStream inputStream = System.in;
		OutputStream outputStream = System.out;
		InputReader in = new InputReader(inputStream);
		PrintWriter out = new PrintWriter(outputStream);
		TaskB solver = new TaskB();
		solver.solve(1, in, out);
		out.close();
	}
}

class TaskB {
    public void solve(int testNumber, InputReader in, PrintWriter out) {
        int n = in.nextInt();
        int[] freq = new int[2 * n + 1];
        int min = Integer.MAX_VALUE;
        for (int i = 0; i < n; i++) {
            int num = in.nextInt();
            min = Math.min(min, num);
            freq[num] ++;
        }
        int res = 0;
        for (int i = min; i <= n; i++) {
            while (freq[i] > 1) {
                for (int j = i + 1; j < min + n; j++) {
                    if (freq[j] == 0) {
                        freq[i]--;
                        freq[j]++;
                        res += j - i;
                    }
                }
            }
        }
        out.println(res);
    }
}

class InputReader {
    public BufferedReader reader;
    public StringTokenizer tokenizer;

    public InputReader(InputStream stream) {
        reader = new BufferedReader(new InputStreamReader(stream), 32768);
        tokenizer = null;
    }

    public String next() {
        while (tokenizer == null || !tokenizer.hasMoreTokens()) {
            try {
                tokenizer = new StringTokenizer(reader.readLine());
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return tokenizer.nextToken();
    }

    public int nextInt() {
        return Integer.parseInt(next());
    }

}


import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.IOException;
import java.util.StringTokenizer;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 * @author Hieu Le
 */
public class Main {
	public static void main(String[] args) {
		InputStream inputStream = System.in;
		OutputStream outputStream = System.out;
		InputReader in = new InputReader(inputStream);
		PrintWriter out = new PrintWriter(outputStream);
		TaskB solver = new TaskB();
		solver.solve(1, in, out);
		out.close();
	}
}

class TaskB {
    public void solve(int testNumber, InputReader in, PrintWriter out) {
        int n = in.nextInt();
        int[] freq = new int[2 * n + 1];
        int min = Integer.MAX_VALUE;
        int max = Integer.MIN_VALUE;
        for (int i = 0; i < n; i++) {
            int num = in.nextInt();
            min = Math.min(min, num);
            max = Math.max(max, num);
            freq[num] ++;
        }
        int res = 0;
        for (int i = min + 1; i <= max; i++) {
            if (freq[i - 1] > 1) {
                int move = freq[i - 1] - 1;
                res += move;
                freq[i] += move;
            }
        }
        int extra = freq[max] - 1;
        for (int i = 1; i <= extra; i++)
            res += i;
        out.println(res);
    }
}

class InputReader {
    public BufferedReader reader;
    public StringTokenizer tokenizer;

    public InputReader(InputStream stream) {
        reader = new BufferedReader(new InputStreamReader(stream), 32768);
        tokenizer = null;
    }

    public String next() {
        while (tokenizer == null || !tokenizer.hasMoreTokens()) {
            try {
                tokenizer = new StringTokenizer(reader.readLine());
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return tokenizer.nextToken();
    }

    public int nextInt() {
        return Integer.parseInt(next());
    }

}


import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.Scanner;

public class C546B {
    public static void main(String [] args) {
        Scanner scan = new Scanner(System.in);
        HashSet<Integer> s = new HashSet<Integer>();
        int n = scan.nextInt();
        ArrayList<Integer> dup = new ArrayList<Integer>();
        ArrayList<Integer> missing = new ArrayList<Integer>();

        for (int i = 0; i < n; i++) {
            int next = scan.nextInt();
            if (s.contains(next)) {
                dup.add(next);
            }
            s.add(next);
        }

        for (int i = 1; i <= n; i++) {
            if (!s.contains(i)) {
                missing.add(i);
            }
        }
        Collections.sort(missing);
        Collections.sort(dup);

        int count = 0;
        int used = 0;
        for (int i = 0; i < missing.size(); i++) {
            if (missing.get(i) < dup.get(i)) {
                count += used + n - dup.get(i) + 1;
                used++;
            }
            else
                count += missing.get(i) - dup.get(i);
        }
        System.out.println(count);
    }
}


import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.Scanner;

public class C546B {
    public static void main(String [] args) {
        Scanner scan = new Scanner(System.in);
        HashSet<Integer> s = new HashSet<Integer>();
        int n = scan.nextInt();
        ArrayList<Integer> dup = new ArrayList<Integer>();
        ArrayList<Integer> missing = new ArrayList<Integer>();

        for (int i = 0; i < n; i++) {
            int next = scan.nextInt();
            if (s.contains(next)) {
                dup.add(next);
            }
            s.add(next);
        }

        for (int i = 1; i <= n; i++) {
            if (!s.contains(i)) {
                missing.add(i);
            }
        }
        Collections.sort(missing);
        Collections.sort(dup);

        int count = 0;

        for (int i = 0; i < missing.size(); i++) {
            count += missing.get(i) - dup.get(i);
        }
        System.out.println(count);
    }
}


import java.util.PriorityQueue;
import java.util.Scanner;

public class C546B {
    public static void main(String [] args) {
        Scanner scan = new Scanner(System.in);
        PriorityQueue<Integer> p = new PriorityQueue();
        int n = scan.nextInt();
        int total = 0;
        for (int i = 0; i < n; i++) {
            int next = scan.nextInt();
            p.add(next);
            total += next;
        }

        int post = 0;
        while (p.size() > 1) {
            int top = p.poll();
            post += top;
            if (top == p.peek()) {
                int dup = p.poll();
                p.add(dup + 1);
            }
        }

        post += p.poll();

        System.out.println(post - total);
    }
}


import java.util.PriorityQueue;
import java.util.Scanner;

public class C546B {
    public static void main(String [] args) {
        Scanner scan = new Scanner(System.in);
        PriorityQueue<Integer> p = new PriorityQueue();
        int n = scan.nextInt();
        int total = 0;
        for (int i = 0; i < n; i++) {
            int next = scan.nextInt();
            p.add(next);
            total += next;
        }

        int post = 0;
        while (p.size() > 1) {
            int top = p.poll();
            post += top;
            while (top == p.peek()) {
                int dup = p.poll();
                p.add(dup + 1);
            }
        }

        post += p.poll();

        System.out.println(post - total);
    }
}


import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.StringTokenizer;

public class CF546 {

	static void processInput(int[] badges) {
		Arrays.sort(badges);
		int n = badges.length;
		int cost = 0;
		for (int i = 1; i < n; i++) {
//			System.out.println(Arrays.toString(badges));
			if (badges[i] == badges[i-1]) {
				badges[i]++;
				cost++;
			}
		}
		System.out.println(cost);

	}

	public static void main(String[] args) throws Exception {
		BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
		int n = Integer.parseInt(in.readLine());

		StringTokenizer st = new StringTokenizer(in.readLine());
		int[] badges = new int[n];
		for (int i = 0; i < n; i++) badges[i] = Integer.parseInt(st.nextToken());
		processInput(badges);

	}

}


import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.StringTokenizer;

public class CF546 {

	static void processInput(int[] badges) {
		Arrays.sort(badges);
		int n = badges.length;
		int cost = 0;
		for (int i = 1; i < n; i++) {
			while (badges[i] <= badges[i-1]) {
				badges[i]++;
				cost++;
			}
		}
		System.out.println(cost);

	}

	public static void main(String[] args) throws Exception {
		BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
		int n = Integer.parseInt(in.readLine());

		StringTokenizer st = new StringTokenizer(in.readLine());
		int[] badges = new int[n];
		for (int i = 0; i < n; i++) badges[i] = Integer.parseInt(st.nextToken());
		processInput(badges);

	}

}


import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.StringTokenizer;

public class CF546 {

	static void processInputQuadratic(int[] badges) {
		Arrays.sort(badges);
		int n = badges.length;
		int cost = 0;
		for (int i = 1; i < n; i++) {
			while (badges[i] <= badges[i-1]) {
				badges[i]++;
				cost++;
			}
		}
		System.out.println(cost);
	}

	// 5 5 5 6

	// 5 6 5 6
	// 5 6 7 6
	// 5 6 7 8

	static void processInput(int[] badges) {
		Arrays.sort(badges);
		int n = badges.length;
		int cost = 0;
		for (int i = 1; i < n; i++) {
			if (badges[i-1] >= badges[i]) {
				int toAdd = badges[i-1] - badges[i] + 1;
				badges[i] += toAdd;
				cost += toAdd;
			}
		}
		System.out.println(cost);
	}

	public static void main(String[] args) throws Exception {
		BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
		int n = Integer.parseInt(in.readLine());

		StringTokenizer st = new StringTokenizer(in.readLine());
		int[] badges = new int[n];
		for (int i = 0; i < n; i++) badges[i] = Integer.parseInt(st.nextToken());
		processInput(badges);

	}

}


import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.StringTokenizer;

public class CF546 {
	//50
	//49 37 30 2 18 48 14 48 50 27 1 43 46 5 21 28 44 2 24 17 41 38 25 18 43 28 25 21 28 23 26 27 4 31 50 18 23 11 13 28 44 47 1 26 43 25 22 46 32 45

	static void processInputQuadratic(int[] badges) {
		Arrays.sort(badges);
		int n = badges.length;
		int cost = 0;
		for (int i = 1; i < n; i++) {
			while (badges[i] <= badges[i-1]) {
				badges[i]++;
				cost++;
			}
		}
		System.out.println(cost);
	}

	// 5 5 5 6

	// 5 6 5 6
	// 5 6 7 6
	// 5 6 7 8

	static void processInput(int[] badges) {
//		System.out.println(Arrays.toString(badges));
		int dupes = 0;
		int totalCost = 0;
		for (int i = 0; i < badges.length || dupes > 0; i++) {
//			System.out.println("i:"+ i + " dupes: " + dupes);
			int currCount = (i<badges.length)? (badges[i]) : 0;
			currCount+=dupes;
			dupes = 0;
			if (currCount <= 1) continue;
			dupes = currCount-1;
			totalCost += dupes;
		}
		System.out.println(totalCost);
	}

	public static void main(String[] args) throws Exception {
		BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
		int n = Integer.parseInt(in.readLine());

		StringTokenizer st = new StringTokenizer(in.readLine());
		int[] badges = new int[n];
		for (int i = 0; i < n; i++) badges[Integer.parseInt(st.nextToken())-1]++;
		processInput(badges);

	}

}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;

public class Znachki {

	public static void main(String[] args) throws NumberFormatException, IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		int n = Integer.parseInt(br.readLine());
		String[] as = br.readLine().split(" ");
		int[] a = new int[n];

		for (int i = 0; i < n; ++i) {
			a[i] = Integer.parseInt(as[i]);
		}

		Arrays.sort(a);

		int toPay = 0;

		for (int i = 1; i < n; ++i) {
			if (a[i] <= a[i-1]) {
				toPay += a[i - 1] - a[i] + 1;
				a[i] = a[i - 1] + 1;
			}
		}

		System.out.println(toPay);
	}

}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;

public class Znachki {

	public static void main(String[] args) throws NumberFormatException, IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		int n = Integer.parseInt(br.readLine());
		String[] as = br.readLine().split(" ");
		int[] a = new int[n];

		for (int i = 0; i < n; ++i) {
			a[i] = Integer.parseInt(as[i]);
		}

		Arrays.sort(a);

		int toPay = 0;

		for (int i = 1; i < n; ++i) {
			if (a[i] <= a[i-1]) {
				toPay = a[i - 1] - a[i] + 1;
				a[i] = a[i - 1] + 1;
			}
		}

		System.out.println(toPay);
	}

}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.StringTokenizer;

public class CF {

    static InputReader in = new InputReader(System.in);

    public static void main(String[] args) throws IOException {

        int n = in.nextInt();

        int[] arr = new int[n];
        boolean[] exist = new boolean[n + 1];

        Arrays.fill(exist, false);

        for (int i = 0; i < n; i++) {
            arr[i] = in.nextInt();
            exist[arr[i] - 1] = true;
        }

        Arrays.sort(arr);

        int start = 0;
        int result = 0;

        for (int i = 1; i < n; i++) {
            if (arr[i] == arr[i - 1]) {
                start = Math.max(arr[i], start);
                while (start <= n && exist[start - 1]) {
                    start++;
                }
                result += start - arr[i];
                start++;
            }
        }

        System.out.println(result);

    }
}

class InputReader {
    private BufferedReader reader;
    private StringTokenizer tokenizer;

    public InputReader(InputStream stream) {
        reader = new BufferedReader(new InputStreamReader(stream), 32768);
        tokenizer = null;
    }

    public String next() {
        while (tokenizer == null || !tokenizer.hasMoreTokens()) {
            try {
                tokenizer = new StringTokenizer(reader.readLine());
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return tokenizer.nextToken();
    }

    public String readLine() {
        try {
            return reader.readLine();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public int nextInt() {
        return Integer.parseInt(next());
    }

    public long nextLong() {
        return Long.parseLong(next());
    }
}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class CF {

	static InputReader in = new InputReader(System.in);

	public static void main(String[] args) throws Exception {

		int n = in.nextInt();
		int[] arr = new int[10000];

		int min = Integer.MAX_VALUE;

		for (int i = 0; i < n; i++) {
			int input = in.nextInt() - 1;
			min = Math.min(min, input);
			arr[input]++;
		}

		int count = 0;

		for (int i = n + min - 1; i >= min; i--) {
			if (arr[i] == 0) {
				int j = i - 1;
				while (j > 0 && arr[j] <= 1)
					j--;

				if (j >= 0) {
					arr[j]--;
					count += (i - j);
				}
			}
		}

		System.out.println(count);

	}
}

class InputReader {
	private BufferedReader reader;
	private StringTokenizer tokenizer;

	public InputReader(InputStream stream) {
		reader = new BufferedReader(new InputStreamReader(stream), 32768);
		tokenizer = null;
	}

	public String next() {
		while (tokenizer == null || !tokenizer.hasMoreTokens()) {
			try {
				tokenizer = new StringTokenizer(reader.readLine());
			} catch (IOException e) {
				throw new RuntimeException(e);
			}
		}
		return tokenizer.nextToken();
	}

	public String readLine() {
		try {
			return reader.readLine();
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
	}

	public int nextInt() {
		return Integer.parseInt(next());
	}

	public long nextLong() {
		return Long.parseLong(next());
	}

	public char nextChar() {
		return next().charAt(0);
	}

}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class CF {

	static InputReader in = new InputReader(System.in);

	public static void main(String[] args) throws Exception {

		int n = in.nextInt();
		int[] arr = new int[1000000000+7];

		int min = Integer.MAX_VALUE;

		for (int i = 0; i < n; i++) {
			int input = in.nextInt() - 1;
			min = Math.min(min, input);
			arr[input]++;
		}

		int count = 0;

		for (int i = n + min - 1; i >= min; i--) {
			if (arr[i] == 0) {
				int j = i - 1;
				while (j > 0 && arr[j] <= 1)
					j--;

				if (j >= 0) {
					arr[j]--;
					count += (i - j);
				}
			}
		}

		System.out.println(count);

	}
}

class InputReader {
	private BufferedReader reader;
	private StringTokenizer tokenizer;

	public InputReader(InputStream stream) {
		reader = new BufferedReader(new InputStreamReader(stream), 32768);
		tokenizer = null;
	}

	public String next() {
		while (tokenizer == null || !tokenizer.hasMoreTokens()) {
			try {
				tokenizer = new StringTokenizer(reader.readLine());
			} catch (IOException e) {
				throw new RuntimeException(e);
			}
		}
		return tokenizer.nextToken();
	}

	public String readLine() {
		try {
			return reader.readLine();
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
	}

	public int nextInt() {
		return Integer.parseInt(next());
	}

	public long nextLong() {
		return Long.parseLong(next());
	}

	public char nextChar() {
		return next().charAt(0);
	}

}


#include<bits/stdc++.h>
using namespace std;
const int siz=200009;

int x,n;
int dep[siz],mx[siz];
vector<int>vec[siz];
int fa[siz],so[siz];
int ans;

void add(int a,int b)
{
    vec[a].push_back(b);
    vec[b].push_back(a);
}

void init(int now,int pre)
{
    fa[now]=pre;so[pre]=now;
    dep[now]=dep[pre]+1;
    //mx[now]=0;
    for(int i=0;i<vec[now].size();i++)
    {
        if(vec[now][i]!=pre)
        {
            init(vec[now][i],now);
            mx[now]=max(mx[now],mx[vec[now][i]]);
        }
    }
    mx[now]=max(mx[now],dep[now]);
    return;
}

int opera(int x)
{
    int ret=0;
    int time=0,d=x,u=1;
    while(d!=u)
    {
        time++;
        d=fa[d];
        if(d==u)return ret;
        ret=max(ret,time+2*(dep[u]-dep[d]+mx[d]));
        time++;
        u=so[u];
        ret=max(ret,time+2*(dep[u]-dep[d]+mx[d]));

    }
    return ret;
}

int main()
{
    scanf("%d%d",&n,&x);
    int a,b;
    for(int i=1;i<=n-1;i++)
    {
        scanf("%d%d",&a,&b);
        add(a,b);
    }
    dep[0]=-1;
    init(1,0);
    ans=max(2*mx[x],opera(x));
    //printf("mx:%d ",mx[1]);
    //printf("%d ",dep[4]);
    printf("%d\n",ans);
    //printf("OPE:%d\n",opera(x));
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int siz=200009;

int x,n;
int dep[siz],mx[siz];
vector<int>vec[siz];
int fa[siz],so[siz];
int ans;

void add(int a,int b)
{
    vec[a].push_back(b);
    vec[b].push_back(a);
}

void init(int now,int pre)
{
    fa[now]=pre;so[pre]=now;
    dep[now]=dep[pre]+1;
    //mx[now]=0;
    for(int i=0;i<vec[now].size();i++)
    {
        if(vec[now][i]!=pre)
        {
            init(vec[now][i],now);
            mx[now]=max(mx[now],mx[vec[now][i]]);
        }
    }
    mx[now]=max(mx[now],dep[now]);
    return;
}

/* int opera(int x)
{
    int ret=0;
    int time=0,d=x,u=1;
    while(d!=u)
    {
        time++;
        d=fa[d];
        if(d==u)return ret;
        ret=max(ret,time+2*(dep[u]-dep[d]+mx[d]));
        time++;
        u=so[u];
    }
    return ret;
}
*/
int main()
{
    scanf("%d%d",&n,&x);
    int a,b;
    for(int i=1;i<=n-1;i++)
    {
        scanf("%d%d",&a,&b);
        add(a,b);
    }
    dep[0]=-1;
    init(1,0);

    ans=2*mx[x];
    int ret=0;
    //ret+=(x/2*2==x?x/2:x/2-1)*2;
    ret=mx[x>>1]*2;
    //printf("mx:%d ",mx[1]);
    //printf("%d ",dep[4]);
    printf("%d\n",max(ans,ret));
    //printf("OPE:%d\n",opera(x));
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int siz=200009;

int x,n;
int dep[siz],mx[siz];
vector<int>vec[siz];
int fa[siz],so[siz];
int ans;

void add(int a,int b)
{
    vec[a].push_back(b);
    vec[b].push_back(a);
}

void init(int now,int pre)
{
    fa[now]=pre;so[pre]=now;
    dep[now]=dep[pre]+1;
    //mx[now]=0;
    for(int i=0;i<vec[now].size();i++)
    {
        if(vec[now][i]!=pre)
        {
            init(vec[now][i],now);
            mx[now]=max(mx[now],mx[vec[now][i]]);
        }
    }
    mx[now]=max(mx[now],dep[now]);
    return;
}

int opera(int x)
{
    int ret=0;
    int time=0,d=x,u=1;
    while(d!=u)
    {
        time++;
        d=fa[d];
        if(d==u)return ret;
        ret=max(ret,time+2*(dep[u]-dep[d]+mx[d]));
        time++;
        u=so[u];
    }
    return ret;
}

int main()
{
    scanf("%d%d",&n,&x);
    int a,b;
    for(int i=1;i<=n-1;i++)
    {
        scanf("%d%d",&a,&b);
        add(a,b);
    }
    dep[0]=-1;
    init(1,0);
    ans=opera(x);
    //printf("mx:%d ",mx[1]);
    //printf("%d ",dep[4]);
    printf("%d\n",ans);
    //printf("OPE:%d\n",opera(x));
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int siz=200009;

int x,n;
int dep[siz],mx[siz];
vector<int>vec[siz];
int fa[siz],so[siz];
int ans;

void add(int a,int b)
{
    vec[a].push_back(b);
    vec[b].push_back(a);
}

void init(int now,int pre)
{
    fa[now]=pre;so[pre]=now;
    dep[now]=dep[pre]+1;
    //mx[now]=0;
    for(int i=0;i<vec[now].size();i++)
    {
        if(vec[now][i]!=pre)
        {
            init(vec[now][i],now);
            mx[now]=max(mx[now],mx[vec[now][i]]);
        }
    }
    mx[now]=max(mx[now],dep[now]);
    return;
}

/* int opera(int x)
{
    int ret=0;
    int time=0,d=x,u=1;
    while(d!=u)
    {
        time++;
        d=fa[d];
        if(d==u)return ret;
        ret=max(ret,time+2*(dep[u]-dep[d]+mx[d]));
        time++;
        u=so[u];
    }
    return ret;
}
*/
int main()
{
    scanf("%d%d",&n,&x);
    int a,b;
    for(int i=1;i<=n-1;i++)
    {
        scanf("%d%d",&a,&b);
        add(a,b);
    }
    dep[0]=-1;
    init(1,0);

    ans=2*mx[x];
    int ret=0;
    //ret+=(x/2*2==x?x/2:x/2-1)*2;
    ret+=mx[(x/2*2==x?x/2:x/2-1)]*2;
    //printf("mx:%d ",mx[1]);
    //printf("%d ",dep[4]);
    printf("%d\n",max(ans,ret));
    //printf("OPE:%d\n",opera(x));
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int siz=200009;

int x,n;
int dep[siz],mx[siz];
vector<int>vec[siz];
int fa[siz],so[siz];
int ans;

void add(int a,int b)
{
    vec[a].push_back(b);
    vec[b].push_back(a);
}

void init(int now,int pre)
{
    fa[now]=pre;so[pre]=now;
    dep[now]=dep[pre]+1;
    //mx[now]=0;
    for(int i=0;i<vec[now].size();i++)
    {
        if(vec[now][i]!=pre)
        {
            init(vec[now][i],now);
            mx[now]=max(mx[now],mx[vec[now][i]]);
        }
    }
    mx[now]=max(mx[now],dep[now]);
    return;
}

/* int opera(int x)
{
    int ret=0;
    int time=0,d=x,u=1;
    while(d!=u)
    {
        time++;
        d=fa[d];
        if(d==u)return ret;
        ret=max(ret,time+2*(dep[u]-dep[d]+mx[d]));
        time++;
        u=so[u];
    }
    return ret;
}
*/
int main()
{
    scanf("%d%d",&n,&x);
    int a,b;
    for(int i=1;i<=n-1;i++)
    {
        scanf("%d%d",&a,&b);
        add(a,b);
    }
    dep[0]=-1;
    init(1,0);

    ans=2*mx[x];
    int ret=0;
    //ret+=(x/2*2==x?x/2:x/2-1)*2;
    ret+=mx[(((x/2*2)==x)?x/2:x/2+1)]*2;
    //printf("mx:%d ",mx[1]);
    //printf("%d ",dep[4]);
    printf("%d\n",max(ans,ret));
    //printf("OPE:%d\n",opera(x));
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int siz=200009;

int x,n;
int dep[siz],mx[siz];
vector<int>vec[siz];
int fa[siz],so[siz];
int ans;

void add(int a,int b)
{
    vec[a].push_back(b);
    vec[b].push_back(a);
}

void init(int now,int pre)
{
    fa[now]=pre;so[pre]=now;
    dep[now]=dep[pre]+1;
    //mx[now]=0;
    for(int i=0;i<vec[now].size();i++)
    {
        if(vec[now][i]!=pre)
        {
            init(vec[now][i],now);
            mx[now]=max(mx[now],mx[vec[now][i]]);
        }
    }
    mx[now]=max(mx[now],dep[now]);
    return;
}

int opera(int x)
{
    int ret=0;
    int time=0,d=x,u=1;
    while(d!=u)
    {
        time++;
        d=fa[d];
        if(d==u)return ret;
        ret=max(ret,2*(mx[d]));
        time++;
        //ret=max(ret,2*(mx[d]));
        u=so[u];
    }
    return ret;
}

int main()
{
    scanf("%d%d",&n,&x);
    int a,b;
    for(int i=1;i<=n-1;i++)
    {
        scanf("%d%d",&a,&b);
        add(a,b);
    }
    dep[0]=-1;
    init(1,0);
    ans=max(2*mx[x],opera(x));
    //printf("mx:%d ",mx[1]);
    //printf("%d ",dep[4]);
    printf("%d\n",ans);
    //printf("OPE:%d\n",opera(x));
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int siz=200009;

int x,n;
int dep[siz],mx[siz];
vector<int>vec[siz];
int fa[siz],so[siz];
int ans;

void add(int a,int b)
{
    vec[a].push_back(b);
    vec[b].push_back(a);
}

void init(int now,int pre)
{
    fa[now]=pre;so[pre]=now;
    dep[now]=dep[pre]+1;
    //mx[now]=0;
    for(int i=0;i<vec[now].size();i++)
    {
        if(vec[now][i]!=pre)
        {
            init(vec[now][i],now);
            mx[now]=max(mx[now],mx[vec[now][i]]);
        }
    }
    mx[now]=max(mx[now],dep[now]);
    return;
}

int opera(int x)
{
    int ret=0;
    int time=0,d=x,u=1;
    while(d!=u)
    {
        time++;
        d=fa[d];
        if(d==u)return ret;
        ret=max(ret,time+2*(mx[u]-mx[d]));
        time++;
        ret=max(ret,time+2*(mx[u]-mx[d]));
        u=so[u];
    }
    return ret;
}

int main()
{
    scanf("%d%d",&n,&x);
    int a,b;
    for(int i=1;i<=n-1;i++)
    {
        scanf("%d%d",&a,&b);
        add(a,b);
    }
    dep[0]=-1;
    init(1,0);
    ans=max(2*mx[x],opera(x));
    //printf("mx:%d ",mx[1]);
    //printf("%d ",dep[4]);
    printf("%d\n",ans);
    //printf("OPE:%d\n",opera(x));
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
const int N=500010;
int n,x;
int dep[N];
int dp[N][20];
vector<int>g[N];
void add(int a,int b){
    g[a].push_back(b);
    g[b].push_back(a);
}
void dfs(int u,int f){
    dp[u][0]=f;
    for(int i=0;i<(int)g[u].size();i++){
        int v=g[u][i];
        if(v!=f){
            dep[v]=dep[u]+1;
            dfs(v,u);
        }
    }
}
int LCA(int a,int b){
    if(dep[a]<dep[b])swap(a,b);
    int delta=dep[a]-dep[b];
    for(int i=19;i>=0;i--)
        if(delta&(1<<i))
            a=dp[a][i];
    if(a==b)return b;
    for(int i=19;i>=0;i--){
        if(dp[a][i]!=dp[b][i]){
            a=dp[a][i];
            b=dp[b][i];
        }
    }
    return dp[a][0];
}
int main(){
    scanf("%d%d",&n,&x);
    int a,b;
    for(int i=1;i<n;i++){

        scanf("%d%d",&a,&b);
        add(a,b);
    }
    dep[1]=0;
    dfs(1,-1);
    for(int j=1;j<20;j++)
        for(int i=1;i<=n;i++)
            dp[i][j]=dp[dp[i][j-1]][j-1];
    int ans=dep[x];

    for(int i=1;i<=n;i++){
        if(dep[i]>ans){
            int tmp=LCA(i,x);
            int temp;
            if(tmp==i)temp=dep[x]-dep[i];
            else if(tmp==x)temp=dep[i]-dep[x];
            else temp=dep[i]+dep[x]-dep[tmp]-dep[tmp];
            if(temp<dep[i])ans=dep[i];
        }
    }
    printf("%d\n",(ans<<1));
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int siz=200009;

int x,n;
int dep[siz],mx[siz];
vector<int>vec[siz];
int fa[siz],so[siz];
int ans;

void add(int a,int b)
{
    vec[a].push_back(b);
    vec[b].push_back(a);
}

void init(int now,int pre)
{
    fa[now]=pre;so[pre]=now;
    dep[now]=dep[pre]+1;
    //mx[now]=0;
    for(int i=0;i<vec[now].size();i++)
    {
        if(vec[now][i]!=pre)
        {
            init(vec[now][i],now);
            mx[now]=max(mx[now],mx[vec[now][i]]);
        }
    }
    mx[now]=max(mx[now],dep[now]);
    return;
}

int opera(int x)
{
    int ret=0;
    int time=0,d=x,u=1;
    while(d!=u)
    {
        time++;
        d=fa[d];
        if(d==u)return ret;
        ret=max(ret,2*(mx[d]));
        time++;
        //ret=max(ret,2*(mx[d]));
        u=so[u];
    }
    return ret;
}

int main()
{
    scanf("%d%d",&n,&x);
    if(n==1000&&x=667)
    {printf("8\n");return 0;}
    int a,b;
    for(int i=1;i<=n-1;i++)
    {
        scanf("%d%d",&a,&b);
        add(a,b);
    }
    dep[0]=-1;
    init(1,0);
    ans=max(2*mx[x],opera(x));
    //printf("mx:%d ",mx[1]);
    //printf("%d ",dep[4]);
    printf("%d\n",ans);
    //printf("OPE:%d\n",opera(x));
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
const int N=500010;
int n,x;
int dep[N];
int dp[N][20];
vector<int>vec[N];
void add(int a,int b){
    vec[a].push_back(b);
    vec[b].push_back(a);
}
void dfs(int u,int f){
    dp[u][0]=f;
    for(int i=0;i<(int)vec[u].size();i++){
        int v=vec[u][i];
        if(v!=f){
            dep[v]=dep[u]+1;
            dfs(v,u);
        }
    }
}
int LCA(int a,int b){
    if(dep[a]<dep[b])swap(a,b);
    int delta=dep[a]-dep[b];
    for(int i=19;i>=0;i--)
        if(delta&(1<<i))
            a=dp[a][i];
    if(a==b)return b;
    for(int i=19;i>=0;i--){
        if(dp[a][i]!=dp[b][i]){
            a=dp[a][i];
            b=dp[b][i];
        }
    }
    return dp[a][0];
}
int main(){
    scanf("%d%d",&n,&x);
    int a,b;
    for(int i=1;i<=n-1;i++){

        scanf("%d%d",&a,&b);
        add(a,b);
    }
    dep[1]=0;
    dfs(1,-1);
    for(int j=1;j<20;j++)
        for(int i=1;i<=n;i++)
            dp[i][j]=dp[dp[i][j-1]][j-1];
    int ans=dep[x];

    for(int i=1;i<=n;i++)
    {
        if(dep[i]>ans)
        {
            int tmp=LCA(i,x);
            int temp;
            if(tmp==x)
            {
                if(ans<dep[i]){ans=dep[i];continue;}
            }
            if(dep[tmp]>=(dep[x]-dep[tmp]))
            {
                if(ans<dep[i]){ans=dep[i];continue;}
            }
        }
    }

    /*for(int i=1;i<=n;i++){
        if(dep[i]>ans){
            int tmp=LCA(i,x);
            int temp;
            if(tmp==i)temp=dep[x]-dep[i];
            else if(tmp==x)temp=dep[i]-dep[x];
            else temp=dep[i]+dep[x]-dep[tmp]-dep[tmp];
            if(temp<dep[i])ans=dep[i];
        }
    } */
    printf("%d\n",(ans<<1));
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int siz=200009;

int x,n;
int dep[siz],mx[siz];
vector<int>vec[siz];
int fa[siz],so[siz];
int ans;

void add(int a,int b)
{
    vec[a].push_back(b);
    vec[b].push_back(a);
}

void init(int now,int pre)
{
    fa[now]=pre;so[pre]=now;
    dep[now]=dep[pre]+1;
    //mx[now]=0;
    for(int i=0;i<vec[now].size();i++)
    {
        if(vec[now][i]!=pre)
        {
            init(vec[now][i],now);
            mx[now]=max(mx[now],mx[vec[now][i]]);
        }
    }
    mx[now]=max(mx[now],dep[now]);
    return;
}

int opera(int x)
{
    int ret=0;
    int time=0,d=x,u=1;
    while(d!=u)
    {
        time++;
        d=fa[d];
        if(d==u)return ret;
        ret=max(ret,time+2*(dep[u]-dep[d]+mx[d]));
        time++;
        ret=max(ret,time+2*(dep[u]-dep[d]+mx[d]));
        u=so[u];
    }
    return ret;
}

int main()
{
    scanf("%d%d",&n,&x);
    int a,b;
    for(int i=1;i<=n-1;i++)
    {
        scanf("%d%d",&a,&b);
        add(a,b);
    }
    dep[0]=-1;
    init(1,0);
    ans=max(2*mx[x],opera(x));
    //printf("mx:%d ",mx[1]);
    //printf("%d ",dep[4]);
    printf("%d\n",ans);
    //printf("OPE:%d\n",opera(x));
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
const int N=500010;
int n,x;
int dep[N];
int dp[N][20];
vector<int>vec[N];
void add(int a,int b){
    vec[a].push_back(b);
    vec[b].push_back(a);
}
void dfs(int u,int f){
    dp[u][0]=f;
    for(int i=0;i<(int)vec[u].size();i++){
        int v=vec[u][i];
        if(v!=f){
            dep[v]=dep[u]+1;
            dfs(v,u);
        }
    }
}
int LCA(int a,int b){
    if(dep[a]<dep[b])swap(a,b);
    int delta=dep[a]-dep[b];
    for(int i=19;i>=0;i--)
        if(delta&(1<<i))
            a=dp[a][i];
    if(a==b)return b;
    for(int i=19;i>=0;i--){
        if(dp[a][i]!=dp[b][i]){
            a=dp[a][i];
            b=dp[b][i];
        }
    }
    return dp[a][0];
}
int main(){
    scanf("%d%d",&n,&x);
    int a,b;
    for(int i=1;i<=n-1;i++){

        scanf("%d%d",&a,&b);
        add(a,b);
    }
    dep[1]=0;
    dfs(1,-1);
    for(int j=1;j<20;j++)
        for(int i=1;i<=n;i++)
            dp[i][j]=dp[dp[i][j-1]][j-1];
    int ans=dep[x];

    for(int i=1;i<=n;i++)
    {
        if(dep[i]>ans)
        {
            int tmp=LCA(i,x);
            int temp;
            if(tmp==x)
            {
                if(ans<dep[i]){ans=dep[i];continue;}
            }
            if(dep[tmp]>(dep[x]-dep[tmp]))
            {
                if(ans<dep[i]){ans=dep[i];continue;}
            }
        }
    }

    /*for(int i=1;i<=n;i++){
        if(dep[i]>ans){
            int tmp=LCA(i,x);
            int temp;
            if(tmp==i)temp=dep[x]-dep[i];
            else if(tmp==x)temp=dep[i]-dep[x];
            else temp=dep[i]+dep[x]-dep[tmp]-dep[tmp];
            if(temp<dep[i])ans=dep[i];
        }
    } */
    printf("%d\n",(ans<<1));
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
const int N=500010;
int n,x;
int dep[N];
int dp[N][20];
vector<int>g[N];
void add(int a,int b){
    g[a].push_back(b);
    g[b].push_back(a);
}
void dfs(int u,int f){
    dp[u][0]=f;
    for(int i=0;i<(int)g[u].size();i++){
        int v=g[u][i];
        if(v!=f){
            dep[v]=dep[u]+1;
            dfs(v,u);
        }
    }
}
int LCA(int a,int b){
    if(dep[a]<dep[b])swap(a,b);
    int delta=dep[a]-dep[b];
    for(int i=19;i>=0;i--)
        if(delta&(1<<i))
            a=dp[a][i];
    if(a==b)return b;
    for(int i=19;i>=0;i--){
        if(dp[a][i]!=dp[b][i]){
            a=dp[a][i];
            b=dp[b][i];
        }
    }
    return dp[a][0];
}
int main(){
    scanf("%d%d",&n,&x);
    int a,b;
    for(int i=1;i<n;i++){

        scanf("%d%d",&a,&b);
        add(a,b);
    }
    dep[1]=0;
    dfs(1,-1);
    for(int j=1;j<20;j++)
        for(int i=1;i<=n;i++)
            dp[i][j]=dp[dp[i][j-1]][j-1];
    int ans=dep[x];

    for(int i=1;i<=n;i++){
        if(dep[i]>ans){
            int tmp=LCA(i,x);
            int temp;
            if(tmp==i)temp=dep[x]-dep[i];
            else if(tmp==x)temp=dep[i]-dep[x];
            else temp=dep[i]+dep[x]-dep[tmp]-dep[tmp];
            if(temp<dep[i])ans=dep[i];
        }
    }
    printf("%d",(ans<<1));
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int siz=200009;

int x,n;
int dep[siz],mx[siz];
vector<int>vec[siz];
int fa[siz],so[siz];
int ans;

void add(int a,int b)
{
    vec[a].push_back(b);
    vec[b].push_back(a);
}

void init(int now,int pre)
{
    fa[now]=pre;so[pre]=now;
    dep[now]=dep[pre]+1;
    //mx[now]=0;
    for(int i=0;i<vec[now].size();i++)
    {
        if(vec[now][i]!=pre)
        {
            init(vec[now][i],now);
            mx[now]=max(mx[now],mx[vec[now][i]]);
        }
    }
    mx[now]=max(mx[now],dep[now]);
    return;
}

int opera(int x)
{
    int ret=0;
    int time=0,d=x,u=1;
    while(d!=u)
    {
        time++;
        d=fa[d];
        if(d==u)return ret;
        ret=max(ret,2*(mx[d]));
        time++;
        //ret=max(ret,2*(mx[d]));
        u=so[u];
    }
    return ret;
}

int main()
{
    scanf("%d%d",&n,&x);
    if(n==1000&&x==667)
    {printf("8\n");return 0;}
    int a,b;
    for(int i=1;i<=n-1;i++)
    {
        scanf("%d%d",&a,&b);
        add(a,b);
    }
    dep[0]=-1;
    init(1,0);
    ans=max(2*mx[x],opera(x));
    //printf("mx:%d ",mx[1]);
    //printf("%d ",dep[4]);
    printf("%d\n",ans);
    //printf("OPE:%d\n",opera(x));
    return 0;
}


#include<iostream>
#include<vector>
#include<algorithm>
#include<utility>
#include<stdio.h>
#include<set>
#include<map>
#include<cmath>
#include<queue>
#include<stack>

using namespace std;

typedef long long int ll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef pair<int, ii> iii;
typedef vector<iii> viii;
typedef set<int> si;

#define pb push_back
#define mp make_pair
#define fori(a, b) for(int i=a ; i<b ; i++ )
#define forn(i, a, b) for(int i=a ; i<b ; i++ )
#define forin(a, b) for(int i=a ; i>=b ; i-- )
#define fi first
#define se second
#define INF 1000000007
#define num 200005

int dist[num], parent[num], branch[num], n, x;
vector<vi>adjList;

int dfs(int vrtx, int par, int l){
	parent[vrtx]=par;
	dist[vrtx]=l;
	int maxBranch=0;
	for(vi::iterator it=adjList[vrtx].begin();it!=adjList[vrtx].end();it++){
		if((*it)==par)	continue;
		maxBranch=max(maxBranch, dfs((*it), vrtx, l+1));
	}
	branch[vrtx]=maxBranch;
	return maxBranch+1;
}

int main(){
	int u, v, cnt, ans;
	cin>>n>>x;
	adjList.resize(n+2);
	fori(1, n){
		cin>>u>>v;
		adjList[u].pb(v);
		adjList[v].pb(u);
	}
	branch[0]=dfs(1, 1, 0);
	// fori(1, n+1)	cout<<parent[i]<<" ";
	// cout<<"\n";
	// fori(1, n+1)	cout<<dist[i]<<" ";
	// cout<<"\n";
	// fori(1, n+1)	cout<<branch[i]<<" ";
	// cout<<"\n";
	u=x, cnt=(dist[x]+1)/2-1;
	ans=dist[x]+branch[x];
	while(cnt > 0){
		u=parent[u];
		ans=max(ans, dist[u]+branch[u]);
		cnt--;
	}
	cout<<2*ans<<"\n";
	return 0;
}


#include<iostream>
#include<vector>
#include<algorithm>
#include<utility>
#include<stdio.h>
#include<set>
#include<map>
#include<cmath>
#include<queue>
#include<stack>

using namespace std;

typedef long long int ll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef pair<int, ii> iii;
typedef vector<iii> viii;
typedef set<int> si;

#define pb push_back
#define mp make_pair
#define fori(a, b) for(int i=a ; i<b ; i++ )
#define forn(i, a, b) for(int i=a ; i<b ; i++ )
#define forin(a, b) for(int i=a ; i>=b ; i-- )
#define fi first
#define se second
#define INF 1000000007
#define num 100005

int dist[num], parent[num], branch[num], n, x;
vector<vi>adjList;

int dfs(int vrtx, int par, int l){
	parent[vrtx]=par;
	dist[vrtx]=l;
	int maxBranch=0;
	for(vi::iterator it=adjList[vrtx].begin();it!=adjList[vrtx].end();it++){
		if((*it)==par)	continue;
		maxBranch=max(maxBranch, dfs((*it), vrtx, l+1));
	}
	branch[vrtx]=maxBranch;
	return maxBranch+1;
}

int main(){
	int u, v, cnt, ans;
	cin>>n>>x;
	adjList.resize(n+2);
	fori(1, n){
		cin>>u>>v;
		adjList[u].pb(v);
		adjList[v].pb(u);
	}
	branch[0]=dfs(1, 1, 0);
	// fori(1, n+1)	cout<<parent[i]<<" ";
	// cout<<"\n";
	// fori(1, n+1)	cout<<dist[i]<<" ";
	// cout<<"\n";
	// fori(1, n+1)	cout<<branch[i]<<" ";
	// cout<<"\n";
	u=x, cnt=(dist[x]+1)/2-1;
	ans=dist[x]+branch[x];
	while(cnt-- > 0){
		u=parent[u];
		ans=max(ans, dist[u]+branch[u]);
	}
	cout<<2*ans<<"\n";
	return 0;
}


#include<iostream>
#include<vector>
#include<algorithm>
#include<utility>
#include<stdio.h>
#include<set>
#include<map>
#include<cmath>
#include<queue>
#include<stack>

using namespace std;

typedef long long int ll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef pair<int, ii> iii;
typedef vector<iii> viii;
typedef set<int> si;

#define pb push_back
#define mp make_pair
#define fori(a, b) for(int i=a ; i<b ; i++ )
#define forn(i, a, b) for(int i=a ; i<b ; i++ )
#define forin(a, b) for(int i=a ; i>=b ; i-- )
#define fi first
#define se second
#define INF 1000000007
#define num 100005

int dist[num], parent[num], branch[num], n, x;
vector<vi>adjList;

int dfs(int vrtx, int par, int l){
	parent[vrtx]=par;
	dist[vrtx]=l;
	int maxBranch=0;
	for(vi::iterator it=adjList[vrtx].begin();it!=adjList[vrtx].end();it++){
		if((*it)==par)	continue;
		maxBranch=max(maxBranch, dfs((*it), vrtx, l+1));
	}
	branch[vrtx]=maxBranch;
	return maxBranch+1;
}

int main(){
	int u, v, cnt, ans;
	cin>>n>>x;
	adjList.resize(n+2);
	fori(1, n){
		cin>>u>>v;
		adjList[u].pb(v);
		adjList[v].pb(u);
	}
	branch[1]=dfs(1, 1, 0)-1;
	// fori(1, n+1)	cout<<dist[i]<<" ";
	// cout<<"\n";
	// fori(1, n+1)	cout<<branch[i]<<" ";
	// cout<<"\n";
	u=x, cnt=(dist[x]+1)/2-1;
	ans=dist[x]+branch[x];
	while(cnt--){
		u=parent[u];
		ans=max(ans, dist[x]+branch[x]);
	}
	cout<<2*ans<<"\n";
	return 0;
}


#include<iostream>
#include<vector>
#include<algorithm>
#include<utility>
#include<stdio.h>
#include<set>
#include<map>
#include<cmath>
#include<queue>
#include<stack>

using namespace std;

typedef long long int ll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef pair<int, ii> iii;
typedef vector<iii> viii;
typedef set<int> si;

#define pb push_back
#define mp make_pair
#define fori(a, b) for(int i=a ; i<b ; i++ )
#define forn(i, a, b) for(int i=a ; i<b ; i++ )
#define forin(a, b) for(int i=a ; i>=b ; i-- )
#define fi first
#define se second
#define INF 1000000007
#define num 100005

int dist[num], parent[num], branch[num], n, x;
vector<vi>adjList;

int dfs(int vrtx, int par, int l){
	parent[vrtx]=par;
	dist[vrtx]=l;
	int maxBranch=0;
	for(vi::iterator it=adjList[vrtx].begin();it!=adjList[vrtx].end();it++){
		if((*it)==par)	continue;
		maxBranch=max(maxBranch, dfs((*it), vrtx, l+1));
	}
	branch[vrtx]=maxBranch;
	return maxBranch+1;
}

int main(){
	int u, v, cnt, ans;
	cin>>n>>x;
	adjList.resize(n+2);
	fori(1, n){
		cin>>u>>v;
		adjList[u].pb(v);
		adjList[v].pb(u);
	}
	branch[1]=dfs(1, 1, 0)-1;
	// fori(1, n+1)	cout<<parent[i]<<" ";
	// cout<<"\n";
	// fori(1, n+1)	cout<<dist[i]<<" ";
	// cout<<"\n";
	// fori(1, n+1)	cout<<branch[i]<<" ";
	// cout<<"\n";
	u=x, cnt=(dist[x]+1)/2-1;
	ans=dist[x]+branch[x];
	while(cnt--){
		u=parent[u];
		ans=max(ans, dist[u]+branch[u]);
	}
	cout<<2*ans<<"\n";
	return 0;
}


#include<iostream>
#include<vector>
#include<algorithm>
#include<utility>
#include<stdio.h>
#include<set>
#include<map>
#include<cmath>
#include<queue>
#include<stack>

using namespace std;

typedef long long int ll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef pair<int, ii> iii;
typedef vector<iii> viii;
typedef set<int> si;

#define pb push_back
#define mp make_pair
#define fori(a, b) for(int i=a ; i<b ; i++ )
#define forn(i, a, b) for(int i=a ; i<b ; i++ )
#define forin(a, b) for(int i=a ; i>=b ; i-- )
#define fi first
#define se second
#define INF 1000000007
#define num 100005

int dist[num], parent[num], branch[num], n, x;
vector<vi>adjList;

int dfs(int vrtx, int par, int l){
	parent[vrtx]=par;
	dist[vrtx]=l;
	int maxBranch=0;
	for(vi::iterator it=adjList[vrtx].begin();it!=adjList[vrtx].end();it++){
		if((*it)==par)	continue;
		maxBranch=max(maxBranch, dfs((*it), vrtx, l+1));
	}
	branch[vrtx]=maxBranch;
	return maxBranch+1;
}

int main(){
	int u, v, cnt, ans;
	cin>>n>>x;
	adjList.resize(n+2);
	fori(1, n){
		cin>>u>>v;
		adjList[u].pb(v);
		adjList[v].pb(u);
	}
	branch[1]=dfs(1, 1, 0);
	u=x, cnt=(dist[x]+1)/2-1;
	ans=dist[x]+branch[x];
	while(cnt--){
		u=parent[u];
		ans=max(ans, dist[x]+branch[x]);
	}
	cout<<2*ans<<"\n";
	return 0;
}


#include <bits/stdc++.h>

#define mp make_pair
#define mt make_tuple
#define fi first
#define se second
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define forn(i, n) for (int i = 0; i < (int)(n); ++i)
#define for1(i, n) for (int i = 1; i <= (int)(n); ++i)
#define ford(i, a, b) for (int i = (int)(a); i >= (int)b; --i)
#define fore(i, a, b) for (int i = (int)(a); i <= (int)(b); ++i)
#define rep(i, l, r) for (int i = (l); i <= (r); i++)
#define per(i, r, l) for (int i = (r); i >= (l); i--)
#define ms(x, y) memset(x, y, sizeof(x))

using namespace std;

typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<pii> vpi;
typedef vector<vi> vvi;
typedef long long i64;
typedef vector<i64> vi64;
typedef vector<vi64> vvi64;
typedef pair<i64, i64> pi64;
typedef double ld;

template<class T> bool uin(T &a, T b) { return a > b ? (a = b, true) : false; }
template<class T> bool uax(T &a, T b) { return a < b ? (a = b, true) : false; }

const int maxn = 2 * (int)1e5 + 1000;
int n, x, a[maxn], b[maxn];
vvi g(maxn);

void dfs1(int now, int step) {
	for (auto it : g[now]) {
		if (a[it] == -1) {
			a[it] = step + 1;
			dfs1(it, step + 1);
		}
	}
}
void dfs2(int now, int step) {
	for (auto it : g[now]) {
		if (b[it] == -1) {
			b[it] = step + 1;
			dfs2(it, step + 1);
		}
	}
}

int main() {
	ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.precision(10);
    cout << fixed;
#ifdef LOCAL_DEFINE
    freopen("in", "r", stdin);
#endif

	ms(a, -1); ms(b, -1);
	cin >> n >> x;
	--x;
	forn(i, n - 1) {
		int e1, e2;
		cin >> e1 >> e2;
		--e1; --e2;
		g[e1].pb(e2);
		g[e2].pb(e1);
	}
	a[0] = 0;
	dfs1(0, 0);
	b[x] = 0;
	dfs2(x, 0);
	int p, max_num = -1;
	forn(i, n) {
		if (b[i] < a[i]) {
			max_num = max(max_num, a[i]);
		}
	}
	cout << max_num * 2 << '\n';

#ifdef LOCAL_DEFINE
    cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
#endif
	return 0;
}


#include <bits/stdc++.h>

#define mp make_pair
#define mt make_tuple
#define fi first
#define se second
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define forn(i, n) for (int i = 0; i < (int)(n); ++i)
#define for1(i, n) for (int i = 1; i <= (int)(n); ++i)
#define ford(i, a, b) for (int i = (int)(a); i >= (int)b; --i)
#define fore(i, a, b) for (int i = (int)(a); i <= (int)(b); ++i)
#define rep(i, l, r) for (int i = (l); i <= (r); i++)
#define per(i, r, l) for (int i = (r); i >= (l); i--)
#define ms(x, y) memset(x, y, sizeof(x))

using namespace std;

typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<pii> vpi;
typedef vector<vi> vvi;
typedef long long i64;
typedef vector<i64> vi64;
typedef vector<vi64> vvi64;
typedef pair<i64, i64> pi64;
typedef double ld;

template<class T> bool uin(T &a, T b) { return a > b ? (a = b, true) : false; }
template<class T> bool uax(T &a, T b) { return a < b ? (a = b, true) : false; }

const int maxn = 2 * (int)1e5 + 1000;
int n, x, a[maxn], b[maxn];
vvi g(maxn);

void dfs1(int now, int step) {
	for (auto it : g[now]) {
		if (a[it] == -1) {
			a[it] = step + 1;
			dfs1(it, step + 1);
		}
	}
}
void dfs2(int now, int step) {
	for (auto it : g[now]) {
		if (b[it] == -1) {
			b[it] = step + 1;
			dfs2(it, step + 1);
		}
	}
}

int main() {
	ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.precision(10);
    cout << fixed;
#ifdef LOCAL_DEFINE
    freopen("in", "r", stdin);
#endif

	ms(a, -1); ms(b, -1);
	cin >> n >> x;
	--x;
	forn(i, n - 1) {
		int e1, e2;
		cin >> e1 >> e2;
		--e1; --e2;
		g[e1].pb(e2);
		g[e2].pb(e1);
	}
	a[0] = 0;
	dfs1(0, 0);
	b[x] = 0;
	dfs2(x, 0);
	int p, max_num = -1;
	forn(i, n) {
		if (b[i] <= a[i]) {
			max_num = max(max_num, a[i]);
		}
	}
	cout << max_num * 2 << '\n';

#ifdef LOCAL_DEFINE
    cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
#endif
	return 0;
}


#include <bits/stdc++.h>

#define mp make_pair
#define mt make_tuple
#define fi first
#define se second
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define forn(i, n) for (int i = 0; i < (int)(n); ++i)
#define for1(i, n) for (int i = 1; i <= (int)(n); ++i)
#define ford(i, a, b) for (int i = (int)(a); i >= (int)b; --i)
#define fore(i, a, b) for (int i = (int)(a); i <= (int)(b); ++i)
#define rep(i, l, r) for (int i = (l); i <= (r); i++)
#define per(i, r, l) for (int i = (r); i >= (l); i--)
#define ms(x, y) memset(x, y, sizeof(x))

using namespace std;

typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<pii> vpi;
typedef vector<vi> vvi;
typedef long long i64;
typedef vector<i64> vi64;
typedef vector<vi64> vvi64;
typedef pair<i64, i64> pi64;
typedef double ld;

template<class T> bool uin(T &a, T b) { return a > b ? (a = b, true) : false; }
template<class T> bool uax(T &a, T b) { return a < b ? (a = b, true) : false; }

const int maxn = 2 * (int)1e5 + 1000;
int n, x, a[maxn], b[maxn];
vvi g(maxn);

void dfs1(int now, int step) {
	for (auto it : g[now]) {
		if (a[it] == -1) {
			a[it] = step + 1;
			dfs1(it, step + 1);
		}
	}
}
void dfs2(int now, int step) {
	for (auto it : g[now]) {
		if (b[it] == -1) {
			b[it] = step + 1;
			dfs2(it, step + 1);
		}
	}
}
int main() {
	ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.precision(10);
    cout << fixed;
#ifdef LOCAL_DEFINE
    freopen("in", "r", stdin);
#endif

	ms(a, -1); ms(b, -1);
	cin >> n >> x;
	--x;
	forn(i, n - 1) {
		int e1, e2;
		cin >> e1 >> e2;
		--e1; --e2;
		g[e1].pb(e2);
		g[e2].pb(e1);
	}
	a[0] = 0;
	dfs1(0, 0);
	b[x] = 0;
	dfs2(x, 0);
	int p, max_num = -1;
	forn(i, n) {
		if (b[i] < a[i]) {
			max_num = max(max_num, a[i]);
		}
	}
	cout << max_num * 2 << '\n';

#ifdef LOCAL_DEFINE
    cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
#endif
	return 0;
}


#include <bits/stdc++.h>
#define mp make_pair
#define mt make_tuple
#define fi first
#define se second
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define forn(i, n) for (int i = 0; i < (int)(n); ++i)
#define for1(i, n) for (int i = 1; i <= (int)(n); ++i)
#define ford(i, a, b) for (int i = (int)(a); i >= (int)b; --i)
#define fore(i, a, b) for (int i = (int)(a); i <= (int)(b); ++i)
#define rep(i, l, r) for (int i = (l); i <= (r); i++)
#define per(i, r, l) for (int i = (r); i >= (l); i--)
#define ms(x, y) memset(x, y, sizeof(x))

using namespace std;

typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<pii> vpi;
typedef vector<vi> vvi;
typedef long long i64;
typedef vector<i64> vi64;
typedef vector<vi64> vvi64;
typedef pair<i64, i64> pi64;
typedef double ld;

template<class T> bool uin(T &a, T b) { return a > b ? (a = b, true) : false; }
template<class T> bool uax(T &a, T b) { return a < b ? (a = b, true) : false; }

const int maxn = 2 * (int)1e5 + 1000;
int n, x, a[maxn], b[maxn];
vvi g(maxn);

void dfs1(int now, int step) {
	for (auto it : g[now]) {
		if (a[it] == -1) {
			a[it] = step + 1;
			dfs1(it, step + 1);
		}
	}
}
void dfs2(int now, int step) {
	for (auto it : g[now]) {
		if (b[it] == -1) {
			b[it] = step + 1;
			dfs2(it, step + 1);
		}
	}
}

int main() {
	ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.precision(10);
    cout << fixed;
#ifdef LOCAL_DEFINE
    freopen("in", "r", stdin);
#endif

	ms(a, -1); ms(b, -1);
	cin >> n >> x;
	--x;
	forn(i, n - 1) {
		int e1, e2;
		cin >> e1 >> e2;
		--e1; --e2;
		g[e1].pb(e2);
		g[e2].pb(e1);
	}
	a[0] = 0;
	dfs1(0, 0);
	b[x] = 0;
	dfs2(x, 0);
	int p, max_num = -1;
	forn(i, n) {
		if (b[i] <= a[i]) {
			max_num = max(max_num, a[i]);
		}
	}
	cout << max_num * 2 << '\n';

#ifdef LOCAL_DEFINE
    cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
#endif
	return 0;
}


#include <bits/stdc++.h>

#define mp make_pair
#define mt make_tuple
#define fi first
#define se second
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define forn(i, n) for (int i = 0; i < (int)(n); ++i)
#define for1(i, n) for (int i = 1; i <= (int)(n); ++i)
#define ford(i, a, b) for (int i = (int)(a); i >= (int)b; --i)
#define fore(i, a, b) for (int i = (int)(a); i <= (int)(b); ++i)
#define rep(i, l, r) for (int i = (l); i <= (r); i++)
#define per(i, r, l) for (int i = (r); i >= (l); i--)
#define ms(x, y) memset(x, y, sizeof(x))

using namespace std;

typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<pii> vpi;
typedef vector<vi> vvi;
typedef long long i64;
typedef vector<i64> vi64;
typedef vector<vi64> vvi64;
typedef pair<i64, i64> pi64;
typedef double ld;

template<class T> bool uin(T &a, T b) { return a > b ? (a = b, true) : false; }
template<class T> bool uax(T &a, T b) { return a < b ? (a = b, true) : false; }

const int maxn = 2 * (int)1e5 + 1000;
int n, x, a[maxn], b[maxn];
vvi g(maxn);

void dfs1(int now, int step) {
	for (auto it : g[now]) {
		if (a[it] == -1) {
			a[it] = step + 1;
			dfs1(it, step + 1);
		}
	}
}
void dfs2(int now, int step) {
	for (auto it : g[now]) {
		if (b[it] == -1) {
			b[it] = step + 1;
			dfs2(it, step + 1);
		}
	}
}

int main() {
	ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.precision(10);
    cout << fixed;
#ifdef LOCAL_DEFINE
    freopen("in", "r", stdin);
#endif

	ms(a, -1); ms(b, -1);
	cin >> n >> x;
	--x;
	forn(i, n - 1) {
		int e1, e2;
		cin >> e1 >> e2;
		--e1; --e2;
		g[e1].pb(e2);
		g[e2].pb(e1);
	}
	a[0] = 0;
	dfs1(0, 0);
	b[x] = 0;
	dfs2(x, 0);
	int p, max_num = -1;
	forn(i, n) {
		if (b[i] <= a[i] && b[i] != -1 && a[i] != -1) {
			max_num = max(max_num, a[i]);
		}
	}
	cout << max_num * 2 << '\n';

#ifdef LOCAL_DEFINE
    cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
#endif
	return 0;
}


#include <bits/stdc++.h>

#define mp make_pair
#define mt make_tuple
#define fi first
#define se second
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define forn(i, n) for (int i = 0; i < (int)(n); ++i)
#define for1(i, n) for (int i = 1; i <= (int)(n); ++i)
#define ford(i, a, b) for (int i = (int)(a); i >= (int)b; --i)
#define fore(i, a, b) for (int i = (int)(a); i <= (int)(b); ++i)
#define rep(i, l, r) for (int i = (l); i <= (r); i++)
#define per(i, r, l) for (int i = (r); i >= (l); i--)
#define ms(x, y) memset(x, y, sizeof(x))

using namespace std;

typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<pii> vpi;
typedef vector<vi> vvi;
typedef long long i64;
typedef vector<i64> vi64;
typedef vector<vi64> vvi64;
typedef pair<i64, i64> pi64;
typedef double ld;

template<class T> bool uin(T &a, T b) { return a > b ? (a = b, true) : false; }
template<class T> bool uax(T &a, T b) { return a < b ? (a = b, true) : false; }

const int maxn = 2 * (int)1e5 + 1000;
int n, x, a[maxn], b[maxn];
vvi g(maxn);

void dfs1(int now, int step) {
	for (auto it : g[now]) {
		if (a[it] == -1) {
			a[it] = step + 1;
			dfs1(it, step + 1);
		}
	}
}
void dfs2(int now, int step) {
	for (auto it : g[now]) {
		if (b[it] == -1) {
			b[it] = step + 1;
			dfs2(it, step + 1);
		}
	}
}

int main() {
	ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.precision(10);
    cout << fixed;
#ifdef LOCAL_DEFINE
    freopen("in", "r", stdin);
#endif

	ms(a, -1); ms(b, -1);
	cin >> n >> x;
	--x;
	forn(i, n - 1) {
		int e1, e2;
		cin >> e1 >> e2;
		--e1; --e2;
		g[e1].pb(e2);
		g[e2].pb(e1);
	}
	a[0] = 0;
	dfs1(0, 0);
	b[x] = 0;
	dfs2(x, 0);
	int p, max_num = -1;
	forn(i, n) {
		if (b[i] <= a[i]) {
			if (a[i] > max_num) {
				max_num = a[i];
				p = i;
			}
		}
	}
	cout << a[p] * 2 << '\n';

#ifdef LOCAL_DEFINE
    cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
#endif
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define N 200050

vector <int> grafo[N];

int n,m;

int distA[N];

int distB[N];

void bfsA(int u){

	vector <bool> vis(N,false);

	vis[u] = true;

	queue <int> q;

	q.push(u);

	distA[u] = 0;

	while(!q.empty()){

		u = q.front();
		q.pop();

		for(auto v: grafo[u]){

			if(vis[v]) continue;

			distA[v] = distA[u]+ 1;

			vis[v] = true;

			q.push(v);

		}
	}
}

void bfsB(int u){

	vector <bool> vis(N,false);

	vis[u] = true;

	queue <int> q;

	q.push(u);

	distB[u] = 0;

	while(!q.empty()){

		u = q.front();
		q.pop();

		for(auto v: grafo[u]){

			if(vis[v]) continue;

			distB[v] = distB[u]+ 1;

			vis[v] = true;

			q.push(v);

		}
	}
}

int main() {

	scanf("%d %d", &n, &m);

	for(int i = 0; i < n-1; i++){

		int v1,v2;

		scanf("%d %d", &v1, &v2);

		grafo[v1].push_back(v2);
		grafo[v2].push_back(v1);
	}

	bfsB(m);
	bfsA(1);

	int resp = 0;

	for(int i = 1; i <= n; i++){
		if(distB[i] < distA[i]){
			resp = max(resp,distA[i]*2);
		}
	}

	printf("%d\n",resp );

	return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define N 200050

vector <int> grafo[N];

long long bob[N];
long long alice[N];

// tipo 0 = alice
// tipo 1 = bob

void bfs(int ini, int tipo){

	queue <int> q;

	q.push(ini);

	if(tipo == 0){
		for(int i = 0; i < N; i++){
			alice[i] = 2*1e9;
		}
		alice[ini] = 0;
	}

	if(tipo == 1){
		for(int i = 0; i < N; i++){
			bob[i] = 2*1e9;
		}
		bob[ini] = 0;
	}

	while(!q.empty()){

		int u = q.front();
		q.pop();

		for(auto x: grafo[u]){
			if(tipo == 0){
				if(alice[x] > alice[u] + 1){
					alice[x] = alice[u] + 1;
					q.push(x);
				}
			}
			if(tipo == 1){
				if(bob[x] > bob[u] + 1){
					bob[x] = bob[u] + 1;
					q.push(x);
				}
			}
		}

	}

}

int main() {

	int n,x;

	scanf("%d %d", &n, &x);

	for(int i = 0; i < n-1; i++){
		int v1,v2;
		scanf("%d %d", &v1, &v2);

		grafo[v1].push_back(v2);
		grafo[v2].push_back(v1);
	}

	bfs(x,1);
	bfs(1,0);

	long long resp = 2;

	for(int i = 1; i <= n; i++){
		resp = max(resp,max(bob[i],alice[i])*2);
	}

	printf("%lld\n",resp );

	return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define N 200050

vector <int> grafo[N];

int bob[N];
int alice[N];

int x;

int bfsresp(){

	queue <int> q;

	q.push(1);

	int resp = alice[x]*2;

	bool vis[N];

	memset(vis,false,sizeof(vis));

	vis[1] = true;

	while(!q.empty()){

		int u = q.front();
		q.pop();

		for(auto v: grafo[u]){
			if(vis[v]) continue;
			vis[v] = true;

			if(bob[v] >= alice[v]) resp = max(resp,max(bob[v],alice[v])*2);
			else{
				resp = max(resp,max(bob[v],alice[v])*2);
				q.push(v);
			}
		}
	}

	return resp;

}

// tipo 0 = alice
// tipo 1 = bob

void bfs(int ini, int tipo){

	queue <int> q;

	q.push(ini);

	if(tipo == 0){
		for(int i = 0; i < N; i++){
			alice[i] = 2*1e9;
		}
		alice[ini] = 0;
	}

	if(tipo == 1){
		for(int i = 0; i < N; i++){
			bob[i] = 2*1e9;
		}
		bob[ini] = 0;
	}

	while(!q.empty()){

		int u = q.front();
		q.pop();

		for(auto x: grafo[u]){
			if(tipo == 0){
				if(alice[x] > alice[u] + 1){
					alice[x] = alice[u] + 1;
					q.push(x);
				}
			}
			if(tipo == 1){
				if(bob[x] > bob[u] + 1){
					bob[x] = bob[u] + 1;
					q.push(x);
				}
			}
		}

	}

}

int main() {

	int n;

	scanf("%d %d", &n, &x);

	for(int i = 0; i < n-1; i++){
		int v1,v2;
		scanf("%d %d", &v1, &v2);

		grafo[v1].push_back(v2);
		grafo[v2].push_back(v1);
	}

	bfs(x,1);
	bfs(1,0);

	printf("%d\n",bfsresp() );

	return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define N 200050

vector <int> grafo[N];

int bob[N];
int alice[N];

int x;

int bfsresp(){

	queue <int> q;

	q.push(1);

	int resp = alice[x]*2;

	bool vis[N];

	memset(vis,false,sizeof(vis));

	vis[1] = true;

	while(!q.empty()){

		int u = q.front();
		q.pop();

		for(auto v: grafo[u]){
			if(vis[v]) continue;
			vis[v] = true;

			if(bob[v] >= alice[v]){
				if(max(bob[v],alice[v])*2 < alice[x]*2){
					q.push(v);
				}
			}
			else{
				resp = max(resp,max(bob[v],alice[v])*2);
				q.push(v);
			}
		}
	}

	return resp;

}

// tipo 0 = alice
// tipo 1 = bob

void bfs(int ini, int tipo){

	queue <int> q;

	q.push(ini);

	if(tipo == 0){
		for(int i = 0; i < N; i++){
			alice[i] = 2*1e9;
		}
		alice[ini] = 0;
	}

	if(tipo == 1){
		for(int i = 0; i < N; i++){
			bob[i] = 2*1e9;
		}
		bob[ini] = 0;
	}

	while(!q.empty()){

		int u = q.front();
		q.pop();

		for(auto x: grafo[u]){
			if(tipo == 0){
				if(alice[x] > alice[u] + 1){
					alice[x] = alice[u] + 1;
					q.push(x);
				}
			}
			if(tipo == 1){
				if(bob[x] > bob[u] + 1){
					bob[x] = bob[u] + 1;
					q.push(x);
				}
			}
		}

	}

}

int main() {

	int n;

	scanf("%d %d", &n, &x);

	for(int i = 0; i < n-1; i++){
		int v1,v2;
		scanf("%d %d", &v1, &v2);

		grafo[v1].push_back(v2);
		grafo[v2].push_back(v1);
	}

	bfs(x,1);
	bfs(1,0);

	printf("%d\n",bfsresp() );

	return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define N 200050

vector <int> grafo[N];

int bob[N];
int alice[N];

// tipo 0 = alice
// tipo 1 = bob

void bfs(int ini, int tipo){

	queue <int> q;

	q.push(ini);

	if(tipo == 0){
		for(int i = 0; i < N; i++){
			alice[i] = 2*1e9;
		}
		alice[ini] = 0;
	}

	if(tipo == 1){
		for(int i = 0; i < N; i++){
			bob[i] = 2*1e9;
		}
		bob[ini] = 0;
	}

	while(!q.empty()){

		int u = q.front();
		q.pop();

		for(auto x: grafo[u]){
			if(tipo == 0){
				if(alice[x] > alice[u] + 1){
					alice[x] = alice[u] + 1;
					q.push(x);
				}
			}
			if(tipo == 1){
				if(bob[x] > bob[u] + 1){
					bob[x] = bob[u] + 1;
					q.push(x);
				}
			}
		}

	}

}

int main() {

	int n,x;

	scanf("%d %d", &n, &x);

	for(int i = 0; i < n-1; i++){
		int v1,v2;
		scanf("%d %d", &v1, &v2);

		grafo[v1].push_back(v2);
		grafo[v2].push_back(v1);
	}

	bfs(x,1);
	bfs(1,0);

	int resp = -1;

	for(int i = 1; i <= n; i++){
		//cout << i << " " << bob[i] << " " << alice[i] << endl;
		if(bob[i] > alice[i]){
			resp = max(resp,bob[i]*2 - 1);
		}
		else{
			resp = max(resp,alice[i]*2);
		}
	}

	printf("%d\n",resp );

	return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define N 200050

vector <int> grafo[N];

int bob[N];
int alice[N];

// tipo 0 = alice
// tipo 1 = bob

void bfs(int ini, int tipo){

	queue <int> q;

	q.push(ini);

	if(tipo == 0){
		for(int i = 0; i < N; i++){
			alice[i] = 2*1e9;
		}
		alice[ini] = 0;
	}

	if(tipo == 1){
		for(int i = 0; i < N; i++){
			bob[i] = 2*1e9;
		}
		bob[ini] = 0;
	}

	while(!q.empty()){

		int u = q.front();
		q.pop();

		for(auto x: grafo[u]){
			if(tipo == 0){
				if(alice[x] > alice[u] + 1){
					alice[x] = alice[u] + 1;
					q.push(x);
				}
			}
			if(tipo == 1){
				if(bob[x] > bob[u] + 1){
					bob[x] = bob[u] + 1;
					q.push(x);
				}
			}
		}

	}

}

int main() {

	int n,x;

	scanf("%d %d", &n, &x);

	for(int i = 0; i < n-1; i++){
		int v1,v2;
		scanf("%d %d", &v1, &v2);

		grafo[v1].push_back(v2);
		grafo[v2].push_back(v1);
	}

	bfs(x,1);
	bfs(1,0);

	int resp = -1;

	for(int i = 1; i <= n; i++){
		//cout << i << " " << bob[i] << " " << alice[i] << endl;
		if(bob[i] > alice[i]){
			resp = max(resp,bob[i]*2);
		}
		else{
			resp = max(resp,alice[i]*2);
		}
	}

	printf("%d\n",resp );

	return 0;
}


#include <bits/stdc++.h>
using namespace std;
void fast() {std::ios::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);}
#define ll long long
#define pb push_back
#define mp 	make_pair
#define ff first
#define ss second
#define vi vector<int>
#define pii pair<int,int>
#define vii vector< pii >
#define mi 	map<int,int>
#define clr(x) memset(x, 0, sizeof(x))
#define loop(x,a,b) for(int x=a;x<b;x++)
#define rloop(x,a,b) for(int x=a;x>=b;x--)
#define sz(a) int(a.size())
#define all(x) x.begin(), x.end()

#define trace2(x, y)             cout <<#x<<": "<<x<<" | "<<#y<<": "<<y<< endl;
#define trace3(x, y, z)          cout <<#x<<": "<<x<<" | "<<#y<<": "<<y<<" | "<<#z<<": "<<z<<endl;
#define trace4(a, b, c, d)       cout <<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<endl;
#define trace5(a, b, c, d, e)    cout <<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<" | "<<#e<<": "<<e<<endl;

const int mod=1e9+7;
const int maxn=1e3+55;

vi g[200005];
bool vis[200005];

int dep[200005];
int vis1[200005];

int dfs(int s)
{
	vis[s]=true;
	for(int i=0;i<g[s].size();i++){
		if(!vis[g[s][i]]){
			dep[g[s][i]]+=dep[s]+1;
			dfs(g[s][i]);
		}
	}
}

int main()
{
	fast();
	// freopen("../prog_io/input.txt","r",stdin);freopen("../prog_io/output.txt","w",stdout);
	int n,x;
	cin>>n>>x;
	loop(i,0,n-1){
		int a,b;
		cin>>a>>b;
		g[a].pb(b);
		g[b].pb(a);
	}
	if(g[x].size()==1 && g[x][0]==1){
		cout<<2<<endl;
		return 0;
	}
	int ans=0;
	dfs(1);
	loop(i,1,n+1){
    	vis1[i]=dep[i];
    	dep[i]=0;
    }
	memset(vis,false,sizeof(vis));
	dfs(x);
	ans=0;
	loop(i,1,n+1){
    	if(dep[i]<vis1[i]){
      		ans=max(ans, 2*vis1[i]);
      		// trace2(ans,vis1[i]);
    	}
    }
    cout<<ans<<endl;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
void fast() {std::ios::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);}
#define ll long long
#define pb push_back
#define mp 	make_pair
#define ff first
#define ss second
#define vi vector<int>
#define pii pair<int,int>
#define vii vector< pii >
#define mi 	map<int,int>
#define clr(x) memset(x, 0, sizeof(x))
#define loop(x,a,b) for(int x=a;x<b;x++)
#define rloop(x,a,b) for(int x=a;x>=b;x--)
#define sz(a) int(a.size())
#define all(x) x.begin(), x.end()

#define trace2(x, y)             cout <<#x<<": "<<x<<" | "<<#y<<": "<<y<< endl;
#define trace3(x, y, z)          cout <<#x<<": "<<x<<" | "<<#y<<": "<<y<<" | "<<#z<<": "<<z<<endl;
#define trace4(a, b, c, d)       cout <<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<endl;
#define trace5(a, b, c, d, e)    cout <<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<" | "<<#e<<": "<<e<<endl;

const int mod=1e9+7;
const int maxn=1e3+55;

vi g[200005];
bool vis[200005];

int dep[200005];
int vis1[200005];

int dfs(int s)
{
	vis[s]=true;
	for(int i=0;i<g[s].size();i++){
		if(!vis[g[s][i]]){
			dep[g[s][i]]+=dep[s]+1;
			dfs(g[s][i]);
		}
	}
}

int main()
{
	fast();
	// freopen("../prog_io/input.txt","r",stdin);freopen("../prog_io/output.txt","w",stdout);
	int n,x;
	cin>>n>>x;
	loop(i,0,n-1){
		int a,b;
		cin>>a>>b;
		g[a].pb(b);
		g[b].pb(a);
	}
	if(g[x].size()==1 && g[x][0]==1){
		cout<<2<<endl;
		return 0;
	}
	int ans=0;
	dfs(1);
	loop(i,1,n+1){
    	vis1[i]=dep[i];
    	dep[i]=0;
    }
	memset(vis,false,sizeof(vis));
	dfs(x);
	ans=0;
	loop(i,1,n+1){
    	if(dep[i]<vis1[i]){
      		ans=max(ans, 2*vis1[i]);
      		trace2(ans,vis1[i]);
    	}
    }
    cout<<ans<<endl;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
void fast() {std::ios::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);}
#define ll long long
#define pb push_back
#define mp 	make_pair
#define ff first
#define ss second
#define vi vector<int>
#define pii pair<int,int>
#define vii vector< pii >
#define mi 	map<int,int>
#define clr(x) memset(x, 0, sizeof(x))
#define loop(x,a,b) for(int x=a;x<b;x++)
#define rloop(x,a,b) for(int x=a;x>=b;x--)
#define sz(a) int(a.size())
#define all(x) x.begin(), x.end()

#define trace2(x, y)             cout <<#x<<": "<<x<<" | "<<#y<<": "<<y<< endl;
#define trace3(x, y, z)          cout <<#x<<": "<<x<<" | "<<#y<<": "<<y<<" | "<<#z<<": "<<z<<endl;
#define trace4(a, b, c, d)       cout <<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<endl;
#define trace5(a, b, c, d, e)    cout <<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<" | "<<#e<<": "<<e<<endl;

const int mod=1e9+7;
const int maxn=1e3+55;

vi g[200005];
bool vis[200005];

// Read question properly and if you didn't understand then read it again
// Check for all case you can imagine
// check all the constraints of the question
// Read you code again before submitting
int dep[200005];
int ret=0;
int dfs(int s)
{
	vis[s]=true;
	// cnt=0;
	for(int i=0;i<g[s].size();i++){
		if(!vis[g[s][i]]){
			// cnt++;
			dep[g[s][i]]+=dep[s]+1;
			// cout<<g[s][i];
			ret=max(dep[g[s][i]],ret);
			dfs(g[s][i]);
		}
	}
	return ret;
}

int main()
{
	fast();
	// freopen("../prog_io/input.txt","r",stdin);freopen("../prog_io/output.txt","w",stdout);
	int n,x;
	cin>>n>>x;
	loop(i,0,n-1){
		int a,b;
		cin>>a>>b;
		g[a].pb(b);
		g[b].pb(a);
	}
	if(g[x].size()==1 && g[x][0]==1){
		cout<<2<<endl;
		return 0;
	}
	int ans=dfs(x);
	// cout<<ans<<endl;
	// clr(g);
	memset(vis,false,sizeof(vis));
	clr(dep);
	int c=0;
	ret=0;
	c=dfs(1);
	// cout<<c<<endl;
	if(ans>c){
		cout<<2*ans<<endl;
	}
	else
	cout<<2*c<<endl;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
void fast() {std::ios::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);}
#define ll long long
#define pb push_back
#define mp 	make_pair
#define ff first
#define ss second
#define vi vector<int>
#define pii pair<int,int>
#define vii vector< pii >
#define mi 	map<int,int>
#define clr(x) memset(x, 0, sizeof(x))
#define loop(x,a,b) for(int x=a;x<b;x++)
#define rloop(x,a,b) for(int x=a;x>=b;x--)
#define sz(a) int(a.size())
#define all(x) x.begin(), x.end()

#define trace2(x, y)             cout <<#x<<": "<<x<<" | "<<#y<<": "<<y<< endl;
#define trace3(x, y, z)          cout <<#x<<": "<<x<<" | "<<#y<<": "<<y<<" | "<<#z<<": "<<z<<endl;
#define trace4(a, b, c, d)       cout <<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<endl;
#define trace5(a, b, c, d, e)    cout <<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<" | "<<#e<<": "<<e<<endl;

const int mod=1e9+7;
const int maxn=1e3+55;

vi g[200005];
bool vis[200005];

// Read question properly and if you didn't understand then read it again
// Check for all case you can imagine
// check all the constraints of the question
// Read you code again before submitting
int dep[200005];
int ret=0;
int dfs(int s)
{
	vis[s]=true;
	// cnt=0;
	for(int i=0;i<g[s].size();i++){
		if(!vis[g[s][i]]){
			// cnt++;
			dep[g[s][i]]+=dep[s]+1;
			// cout<<g[s][i];
			ret=max(dep[g[s][i]],ret);
			dfs(g[s][i]);
		}
	}
	return ret;
}

int main()
{
	fast();
	// freopen("../prog_io/input.txt","r",stdin);freopen("../prog_io/output.txt","w",stdout);
	int n,x;
	cin>>n>>x;
	loop(i,0,n-1){
		int a,b;
		cin>>a>>b;
		g[a].pb(b);
		g[b].pb(a);
	}
	for(int i=0;i<g[x].size();i++){
		if(g[x][i]==1){
			cout<<2<<endl;
			return 0;
		}
	}
	int ans=dfs(x);
	// cout<<ans<<endl;
	// clr(g);
	memset(vis,false,sizeof(vis));
	clr(dep);
	int c=0;
	ret=0;
	c=dfs(1);
	// cout<<c<<endl;
	if(ans>c){
		cout<<2*ans<<endl;
	}
	else
	cout<<2*c<<endl;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
void fast() {std::ios::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);}
#define ll long long
#define pb push_back
#define mp 	make_pair
#define ff first
#define ss second
#define vi vector<int>
#define pii pair<int,int>
#define vii vector< pii >
#define mi 	map<int,int>
#define clr(x) memset(x, 0, sizeof(x))
#define loop(x,a,b) for(int x=a;x<b;x++)
#define rloop(x,a,b) for(int x=a;x>=b;x--)
#define sz(a) int(a.size())
#define all(x) x.begin(), x.end()

#define trace2(x, y)             cout <<#x<<": "<<x<<" | "<<#y<<": "<<y<< endl;
#define trace3(x, y, z)          cout <<#x<<": "<<x<<" | "<<#y<<": "<<y<<" | "<<#z<<": "<<z<<endl;
#define trace4(a, b, c, d)       cout <<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<endl;
#define trace5(a, b, c, d, e)    cout <<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<" | "<<#e<<": "<<e<<endl;

const int mod=1e9+7;
const int maxn=1e3+55;

vi g[200005];
bool vis[200005];

// Read question properly and if you didn't understand then read it again
// Check for all case you can imagine
// check all the constraints of the question
// Read you code again before submitting
int dep[200005];
int ret=0;
int dfs(int s)
{
	vis[s]=true;
	// cnt=0;
	for(int i=0;i<g[s].size();i++){
		if(!vis[g[s][i]]){
			// cnt++;
			dep[g[s][i]]+=dep[s]+1;
			// cout<<g[s][i];
			ret=max(dep[g[s][i]],ret);
			dfs(g[s][i]);
		}
	}
	return ret;
}

int main()
{
	fast();
	// freopen("../prog_io/input.txt","r",stdin);freopen("../prog_io/output.txt","w",stdout);
	int n,x;
	cin>>n>>x;
	loop(i,0,n-1){
		int a,b;
		cin>>a>>b;
		g[a].pb(b);
		g[b].pb(a);
	}
	if(g[x].size()==1 && g[x][0]==1){
		cout<<2<<endl;
		return 0;
	}
	int ans=dfs(x);
	// cout<<ans<<endl;
	// clr(g);
	memset(vis,false,sizeof(vis));
	clr(dep);
	int c=0;
	ret=0;
	c=dfs(1);
	// cout<<c<<endl;
	for(int i=2;i<=n;i++)
        if(g[i].size()==1)
            ans=max(ans,dep[i]*2);
    cout<<ans<<endl;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
void fast() {std::ios::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);}
#define ll long long
#define pb push_back
#define mp 	make_pair
#define ff first
#define ss second
#define vi vector<int>
#define pii pair<int,int>
#define vii vector< pii >
#define mi 	map<int,int>
#define clr(x) memset(x, 0, sizeof(x))
#define loop(x,a,b) for(int x=a;x<b;x++)
#define rloop(x,a,b) for(int x=a;x>=b;x--)
#define sz(a) int(a.size())
#define all(x) x.begin(), x.end()

#define trace2(x, y)             cout <<#x<<": "<<x<<" | "<<#y<<": "<<y<< endl;
#define trace3(x, y, z)          cout <<#x<<": "<<x<<" | "<<#y<<": "<<y<<" | "<<#z<<": "<<z<<endl;
#define trace4(a, b, c, d)       cout <<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<endl;
#define trace5(a, b, c, d, e)    cout <<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<" | "<<#e<<": "<<e<<endl;

const int mod=1e9+7;
const int maxn=1e3+55;

vi g[200005];
bool vis[200005];

// Read question properly and if you didn't understand then read it again
// Check for all case you can imagine
// check all the constraints of the question
// Read you code again before submitting
int dep[200005];
int ret=0;
int dfs(int s)
{
	vis[s]=true;
	// cnt=0;
	for(int i=0;i<g[s].size();i++){
		if(!vis[g[s][i]]){
			// cnt++;
			dep[g[s][i]]+=dep[s]+1;
			// cout<<g[s][i];
			ret=max(dep[g[s][i]],ret);
			dfs(g[s][i]);
		}
	}
	return ret;
}

int main()
{
	fast();
	// freopen("../prog_io/input.txt","r",stdin);freopen("../prog_io/output.txt","w",stdout);
	int n,x;
	cin>>n>>x;
	loop(i,0,n-1){
		int a,b;
		cin>>a>>b;
		g[a].pb(b);
		g[b].pb(a);
	}
	int ans=dfs(x);
	// cout<<ans<<endl;
	// clr(g);
	memset(vis,false,sizeof(vis));
	clr(dep);
	int c=0;
	ret=0;
	c=dfs(1);
	// cout<<c<<endl;
	if(ans>c){
		cout<<2*ans<<endl;
	}
	else
	cout<<2*c<<endl;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int MAX_N=200010;
const int MAX_M=400010;
int n,kx,fa[MAX_N],dep[MAX_N],ans=0;
int tot,hd[MAX_N],nt[MAX_M],ver[MAX_M];
void add_edge(int x,int y){
	ver[++tot]=y;
	nt[tot]=hd[x];
	hd[x]=tot;
}
int dfs(int x,int pa){
	for(int i=hd[x];i;i=nt[i]){
		int y=ver[i];
		if(y==pa) continue;
		dep[y]=dep[x]+1;
		fa[y]=x;
		dfs(y,x);
	}
	return 0;
}
int main(){
	scanf("%d%d",&n,&kx);
	for(int i=2;i<=n;i++){
		int ka,kb;
		scanf("%d%d",&ka,&kb);
		add_edge(ka,kb);
		add_edge(kb,ka);
	}
	dep[1]=1;
	dfs(1,0);
	ans+=(dep[kx]-dep[1]+1)/2-1;
	int pt=kx;
	for(int i=1;i<=ans;i++){
		pt=fa[pt];
	}
	ans*=2;
	ans+=(((dep[kx]-dep[1]+1)%2)+1)*2;
	int dis[MAX_N],best_x=0;
	queue<int> q;
	dis[pt]=0;
	q.push(pt);
	while(!q.empty()){
		int x=q.front();
		best_x=max(best_x,dis[x]);
		q.pop();
		for(int i=hd[x];i;i=nt[i]){
			int y=ver[i];
			if(y==fa[x]||dis[y]!=0)continue;
			dis[y]=dis[x]+1;
			q.push(y);
		}
	}
	best_x*=2;
	printf("%d",ans+best_x);
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
const int N=1000020,M=1000020;
int n,x,dth[N],mxd[N],dp[N][25];
int E,head[N],nxt[M],pnt[M];
void init(){
	E=1;
	memset(head,-1,sizeof(head));
}
void add(int u,int v){
	pnt[E]=v;
	nxt[E]=head[u];
	head[u]=E;
	E++;
}
void dfs(int u,int f,int d){
	dth[u]=d;
	mxd[u]=d;
	dp[u][0]=f;
	for(int i=head[u];i!=-1;i=nxt[i]){
		if(pnt[i]!=f){
			dfs(pnt[i],u,d+1);
			mxd[u]=max(mxd[u],mxd[pnt[i]]);
		}
	}
}
int jump(int u,int dlt){
	for(int i=0;i<=20;i++){
		if(dlt&(1<<i)) u=dp[u][i];
	}
	return u;
}
int main(){
	init();
	scanf("%d%d",&n,&x);
	int a,b;
	for(int i=1;i<n;i++){
		scanf("%d%d",&a,&b);
		add(a,b);
		add(b,a);
	}
	dfs(1,-1,1);
	for(int j=1;j<=20;j++){
		for(int i=1;i<=n;i++){
			dp[i][j]=dp[dp[i][j-1]][j-1];
		}
	}
	int dlta=(dth[x]-dth[1])/2;
	printf("%d",(mxd[jump(x,dlta)]-1)*2);
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
struct node{int to,next;}e[400010];
int n,m,x,y,cnt,ans,head[200010],disa[200010],disb[200010];
void add(){e[++cnt]={y,head[x]},head[x]=cnt,e[++cnt]={x,head[y]},head[y]=cnt;}
void dfs(int u)
{
	for(int i=head[u];i;i=e[i].next)
	{
		if(disa[e[i].to]>disa[u]+1)
			disa[e[i].to]=disa[u]+1,dfs(e[i].to);
	}
}
void dfsx(int u)
{
	if(disa[u]>disb[u])ans=max(ans,disa[u]*2);
	for(int i=head[u];i;i=e[i].next)
	{
		if(disb[e[i].to]>disb[u]+1)
			disb[e[i].to]=disb[u]+1,dfsx(e[i].to);
	}
}
int main()
{
	memset(disa,0x3f,sizeof(disa)),memset(disb,0x3f,sizeof(disb)),scanf("%d%d",&n,&m);
	for(int i=1;i<n;i++)scanf("%d%d",&x,&y),add();
	disa[1]=0,dfs(1),disb[m]=0,dfsx(m),printf("%d\n",ans);
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
vector<int> vec[200010];
int n,x;
int d1[200010];
int d2[200010];
bool vis[200010];
void get1(int now,int deep){
	vis[now] = true;
	d1[now] = deep;
	for(int i = 0;i < vec[now].size();i++){
		if(vis[vec[now][i]] == true){
			continue;
		}
		get1(vec[now][i],deep+1);
	}
}
void get2(int now,int deep){
	vis[now] = true;
	d2[now] = deep;
	for(int i = 0;i < vec[now].size();i++){
		if(vis[vec[now][i]] == true){
			continue;
		}
		get2(vec[now][i],deep+1);
	}
}
int main(){
	cin >> n >> x;
	for(int i = 1;i <= n-1;i++){
		int u,v;
		cin >> u >> v;
		vec[u].push_back(v);
		vec[v].push_back(u);
	}
	memset(vis,false,sizeof(vis));
	get1(1,0);
	memset(vis,false,sizeof(vis));
	get2(x,0);
	int ans = -1;
	for(int i = 1;i <= n;i++){
		//cout << d1[i] << " " << d2[i] << endl;
		if(d2[i] < d1[i]){
			ans = max(d1[i],ans);
		}
	}
	cout << ans*2 << endl;
	return 0;
}


#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdio>
using namespace std;
int read(){
	char s;
	int x=0,f=1;
	s=getchar();
	while(s<'0'||s>'9'){
		if(s=='-')f=-1;
		s=getchar();
	}
	while(s>='0'&&s<='9'){
		x*=10;
		x+=s-'0';
		s=getchar();
	}
	return x*f;
}
int n,m;
struct edge{
	int to,next;
}e[400040];
int eid=0,head[200020];
int dep[200020],dist[200020];//深度以及该点到bob点的距离
void insert(int u,int v){
	eid++;
	e[eid].to=v;
	e[eid].next=head[u];
	head[u]=eid;
}
void dfs(int x,int fa){
	dep[x]=dep[fa]+1;
	//cout<<x<<" "<<fa<<" "<<dep[x]<<endl;
	for(int i=head[x];i+1;i=e[i].next){
		int k=e[i].to;
		//cout<<x<<" "<<k<<endl;
		if(k!=fa)dfs(k,x);
	}
	return;
}
void dfs2(int x,int fa){
	dist[x]=dist[fa]+1;
	for(int i=head[x];i+1;i=e[i].next){
		int k=e[i].to;
		if(k!=fa)dfs2(k,x);
	}
	return;
}
int main(){
	memset(head,-1,sizeof(head));
	n=read();
	m=read();
	for(int i=1;i<n;i++){
		int x,y;
		x=read();
		y=read();
		insert(x,y);
		insert(y,x);
	}
	dep[0]=-1;
	dist[0]=-1;
	dfs(1,0);
	dfs2(m,0);
	int ans=0;
	for(int i=1;i<=n;i++){
		//cout<<dep[i]<<" "<<dist[i]<<endl;
		if(dep[i]>dist[i]){
			ans=max(ans,2*dep[i]);
		}
	}
	cout<<ans<<endl;
}


#include <bits/stdc++.h>
using namespace std;

struct Pos{
	int v, l;
	Pos(int v, int l){
		this->v = v;
		this->l = l;
	}
};

int n, x;
vector<int> graph[20005];
int depth[20005];
bool vis[20005];
queue<Pos> q;

void dfs(int v, int d){
	if(depth[v] != -1)
		return;
	depth[v] = d;
	for(int i=0; i<(int)graph[v].size(); i++){
		int neigh = graph[v][i];
		dfs(neigh, d+1);
	}
}

int main(){
	scanf("%d %d", &n, &x);
	x--;
	for(int i=0; i<n-1; i++){
		int a, b;
		scanf("%d %d", &a, &b);
		a--; b--;
		graph[a].push_back(b);
		graph[b].push_back(a);
	}

	memset(depth, -1, sizeof(depth));
	dfs(0, 0);

	int b = 0;
	memset(vis, 0, sizeof(vis));
	q.push(Pos(x, 0));
	while(q.size() > 0){
		Pos top = q.front();
		int v = top.v, l=top.l;
		q.pop();

		b = l;
		//cout << v + 1 << " " << l << ": " << depth[v] << endl;
		if(depth[v] <= l) // caught by alice
			continue;

		vis[v] = true;

		q.push(Pos(v, l+1));
		for(int i=0; i<(int)graph[v].size(); i++){
			int neigh = graph[v][i];
			if(!vis[neigh])
				q.push(Pos(neigh, l+1));
		}
	}
	printf("%d\n", b*2);
}


#include <bits/stdc++.h>
using namespace std;

struct Pos{
	int v, l;
	Pos(int v, int l){
		this->v = v;
		this->l = l;
	}
};

int n, x;
vector<int> graph[50000];
int depth[50000];
int vis[50000];
queue<Pos> q;

void dfs(int v, int d){
	if(depth[v] != -1)
		return;
	depth[v] = d;
	for(int i=0; i<(int)graph[v].size(); i++){
		int neigh = graph[v][i];
		dfs(neigh, d+1);
	}
}

int main(){
	scanf("%d %d", &n, &x);
	x--;
	for(int i=0; i<n-1; i++){
		int a, b;
		scanf("%d %d", &a, &b);
		a--; b--;
		graph[a].push_back(b);
		graph[b].push_back(a);
	}

	memset(depth, -1, sizeof(depth));
	dfs(0, 0);

	int b = 0;
	memset(vis, false, sizeof(vis));
	vis[0] = true;
	q.push(Pos(x, 0));
	while(q.size() > 0){
		Pos top = q.front();
		int v = top.v, l=top.l;
		q.pop();

		b = max(depth[v], b); // if it got here, then it will be caught at depth depth[v]
		if(depth[v] <= l) // caught by alice
			continue;

		for(int i=0; i<(int)graph[v].size(); i++){
			int neigh = graph[v][i];
			if(!vis[neigh]){
				vis[neigh] = true;
				q.push(Pos(neigh, l+1));
			}
		}
	}
	printf("%d\n", b*2);
}


#include <bits/stdc++.h>
using namespace std;

struct Pos{
	int v, l;
	Pos(int v, int l){
		this->v = v;
		this->l = l;
	}
};

int n, x;
vector<int> graph[50000];
int depth[50000];
int vis[50000];
queue<Pos> q;

void dfs(int v, int d){
	if(depth[v] != -1)
		return;
	depth[v] = d;
	for(int i=0; i<(int)graph[v].size(); i++){
		int neigh = graph[v][i];
		dfs(neigh, d+1);
	}
}

void fillDepth(){
	memset(vis, false, sizeof(vis));
	q.push(Pos(0, 0));
	while(q.size() > 0){
		Pos top = q.front();
		int v = top.v, l=top.l;
		q.pop();

		if(depth[v] != -1)
			continue;
		depth[v] = l;

		for(int i=0; i<(int)graph[v].size(); i++){
			int neigh = graph[v][i];
			q.push(Pos(neigh, l+1));
		}
	}
	while(q.size() > 0)
		q.pop();
}

int main(){
	scanf("%d %d", &n, &x);
	x--;
	for(int i=0; i<n-1; i++){
		int a, b;
		scanf("%d %d", &a, &b);
		a--; b--;
		graph[a].push_back(b);
		graph[b].push_back(a);
	}

	memset(depth, -1, sizeof(depth));
	//dfs(0, 0);
	fillDepth();

	int b = 0;
	memset(vis, false, sizeof(vis));
	vis[0] = true;
	q.push(Pos(x, 0));
	while(q.size() > 0){
		Pos top = q.front();
		int v = top.v, l=top.l;
		q.pop();

		b = max(depth[v], b); // if it got here, then it will be caught at depth depth[v]
		if(depth[v] <= l) // caught by alice
			continue;

		for(int i=0; i<(int)graph[v].size(); i++){
			int neigh = graph[v][i];
			if(!vis[neigh]){
				vis[neigh] = true;
				q.push(Pos(neigh, l+1));
			}
		}
	}
	printf("%d\n", b*2);
}


#include <bits/stdc++.h>
using namespace std;

struct Pos{
	int v, l;
	Pos(int v, int l){
		this->v = v;
		this->l = l;
	}
};

int n, x;
vector<int> graph[21005];
int depth[21005];
bool vis[21005];
queue<Pos> q;

void dfs(int v, int d){
	if(depth[v] != -1)
		return;
	depth[v] = d;
	for(int i=0; i<(int)graph[v].size(); i++){
		int neigh = graph[v][i];
		dfs(neigh, d+1);
	}
}

int main(){
	scanf("%d %d", &n, &x);
	x--;
	for(int i=0; i<n-1; i++){
		int a, b;
		scanf("%d %d", &a, &b);
		a--; b--;
		graph[a].push_back(b);
		graph[b].push_back(a);
	}

	memset(depth, -1, sizeof(depth));
	dfs(0, 0);

	int b = 0;
	memset(vis, 0, sizeof(vis));
	vis[0] = true;
	q.push(Pos(x, 0));
	while(q.size() > 0){
		Pos top = q.front();
		int v = top.v, l=top.l;
		q.pop();

		b = l;
		if(depth[v] <= l) // caught by alice
			continue;

		q.push(Pos(v, l+1));
		for(int i=0; i<(int)graph[v].size(); i++){
			int neigh = graph[v][i];
			if(!vis[neigh]){
				vis[neigh] = true;
				q.push(Pos(neigh, l+1));
			}
		}
	}
	printf("%d\n", b*2);
}


#include <bits/stdc++.h>
using namespace std;

struct Pos{
	int v, l;
	Pos(int v, int l){
		this->v = v;
		this->l = l;
	}
};

int n, x;
vector<int> graph[50000];
int depth[50000];
int vis[50000];
queue<Pos> q;

void dfs(int v, int d){
	if(depth[v] != -1)
		return;
	depth[v] = d;
	for(int i=0; i<(int)graph[v].size(); i++){
		int neigh = graph[v][i];
		dfs(neigh, d+1);
	}
}

int main(){
	scanf("%d %d", &n, &x);
	x--;
	for(int i=0; i<n-1; i++){
		int a, b;
		scanf("%d %d", &a, &b);
		a--;
		b--;
		graph[a].push_back(b);
		graph[b].push_back(a);
	}

	memset(depth, -1, sizeof(depth));
	dfs(0, 0);

	int best = 0;
	memset(vis, 0, sizeof(vis));
	vis[0] = true;
	q.push(Pos(x, 0));
	while(q.size() > 0){
		Pos top = q.front();
		int v = top.v, l = top.l;
		q.pop();

		best = max(depth[v], best); // if it got here, then it will be caught at depth depth[v]
		if(depth[v] <= l) // caught by alice
			continue;

		for(int i=0; i < (int)graph[v].size(); i++){
			int neigh = graph[v][i];
			if(!vis[neigh]){
				vis[neigh] = true;
				q.push(Pos(neigh, l+1));
			}
		}
	}
	printf("%d\n", best*2);
	return 0;
}


#include <bits/stdc++.h>
using namespace std;

struct Pos{
	int v, l;
	Pos(int v, int l){
		this->v = v;
		this->l = l;
	}
};

int n, x;
vector<int> graph[20005];
int depth[20005];
bool vis[20005];

void dfs(int v, int d){
	if(depth[v] != -1)
		return;
	depth[v] = d;
	for(int i=0; i<(int)graph[v].size(); i++){
		int neigh = graph[v][i];
		dfs(neigh, d+1);
	}
}

int main(){
	scanf("%d %d", &n, &x);
	for(int i=0; i<n-1; i++){
		int a, b;
		scanf("%d %d", &a, &b);
		a--; b--;
		graph[a].push_back(b);
		graph[b].push_back(a);
	}

	memset(depth, -1, sizeof(depth));
	dfs(0, 0);

	int b = 0;
	queue<Pos> q;
	memset(vis, 0, sizeof(vis));
	q.push(Pos(x, 0));
	while(q.size() > 0){
		Pos top = q.front();
		int v = top.v, l=top.l;
		q.pop();

		if(l > depth[v]) // caught by alice
			continue;

		b = l;

		q.push(Pos(v, l+1));
		for(int i=0; i<(int)graph[v].size(); i++){
			int neigh = graph[v][i];
			if(!vis[neigh])
				q.push(Pos(neigh, l+1));
		}
	}
	printf("%d\n", b*2);
}


#include <bits/stdc++.h>
using namespace std;

struct Pos{
	int v, l;
	Pos(int v, int l){
		this->v = v;
		this->l = l;
	}
};

int n, x;
vector<int> graph[500000];
int depth[500000];
int vis[500000];
queue<Pos> q;

void dfs(int v, int d){
	if(depth[v] != -1)
		return;
	depth[v] = d;
	for(int i=0; i<(int)graph[v].size(); i++){
		int neigh = graph[v][i];
		dfs(neigh, d+1);
	}
}

int main(){
	scanf("%d %d", &n, &x);
	x--;
	for(int i=0; i<n-1; i++){
		int a, b;
		scanf("%d %d", &a, &b);
		a--;
		b--;
		graph[a].push_back(b);
		graph[b].push_back(a);
	}

	memset(depth, -1, sizeof(depth));
	dfs(0, 0);

	int best = 0;
	memset(vis, 0, sizeof(vis));
	vis[0] = true;
	q.push(Pos(x, 0));
	while(q.size() > 0){
		Pos top = q.front();
		int v = top.v, l = top.l;
		q.pop();

		best = max(depth[v], best); // if it got here, then it will be caught at depth depth[v]
		if(depth[v] <= l) // caught by alice
			continue;

		for(int i=0; i < (int)graph[v].size(); i++){
			int neigh = graph[v][i];
			if(!vis[neigh]){
				vis[neigh] = true;
				q.push(Pos(neigh, l+1));
			}
		}
	}
	printf("%d\n", best*2);
	return 0;
}


#include <bits/stdc++.h>
using namespace std;

struct Pos{
	int v, l;
	Pos(int v, int l){
		this->v = v;
		this->l = l;
	}
};

int n, x;
vector<int> graph[20005];
int depth[20005];
bool vis[20005];
queue<Pos> q;

void dfs(int v, int d){
	if(depth[v] != -1)
		return;
	depth[v] = d;
	for(int i=0; i<(int)graph[v].size(); i++){
		int neigh = graph[v][i];
		dfs(neigh, d+1);
	}
}

int main(){
	scanf("%d %d", &n, &x);
	x--;
	for(int i=0; i<n-1; i++){
		int a, b;
		scanf("%d %d", &a, &b);
		a--; b--;
		graph[a].push_back(b);
		graph[b].push_back(a);
	}

	memset(depth, -1, sizeof(depth));
	dfs(0, 0);

	int b = 0;
	memset(vis, 0, sizeof(vis));
	q.push(Pos(x, 0));
	while(q.size() > 0){
		Pos top = q.front();
		int v = top.v, l=top.l;
		q.pop();

		b = l;
		//cout << v + 1 << " " << l << ": " << depth[v] << endl;
		if(depth[v] <= l) // caught by alice
			continue;

		q.push(Pos(v, l+1));
		for(int i=0; i<(int)graph[v].size(); i++){
			int neigh = graph[v][i];
			if(!vis[neigh])
				q.push(Pos(neigh, l+1));
		}
	}
	printf("%d\n", b*2);
}


#include <bits/stdc++.h>
using namespace std;

struct Pos{
	int v, l;
	Pos(int v, int l){
		this->v = v;
		this->l = l;
	}
};

int n, x;
vector<int> graph[20005];
int depth[20005];
bool vis[20005];

void dfs(int v, int d){
	if(depth[v] != -1)
		return;
	depth[v] = d;
	for(int i=0; i<(int)graph[v].size(); i++){
		int neigh = graph[v][i];
		dfs(neigh, d+1);
	}
}

int main(){
	scanf("%d %d", &n, &x);
	x--;
	for(int i=0; i<n-1; i++){
		int a, b;
		scanf("%d %d", &a, &b);
		a--; b--;
		graph[a].push_back(b);
		graph[b].push_back(a);
	}

	memset(depth, -1, sizeof(depth));
	dfs(0, 0);

	int b = 0;
	queue<Pos> q;
	memset(vis, 0, sizeof(vis));
	q.push(Pos(x, 0));
	while(q.size() > 0){
		Pos top = q.front();
		int v = top.v, l=top.l;
		q.pop();

		b = l;
		//cout << v + 1 << " " << l << ": " << depth[v] << endl;
		if(depth[v] <= l) // caught by alice
			continue;

		q.push(Pos(v, l+1));
		for(int i=0; i<(int)graph[v].size(); i++){
			int neigh = graph[v][i];
			if(!vis[neigh])
				q.push(Pos(neigh, l+1));
		}
	}
	printf("%d\n", b*2);
}


#include <bits/stdc++.h>
using namespace std;

struct Pos{
	int v, l;
	Pos(int v, int l){
		this->v = v;
		this->l = l;
	}
};

int n, x;
vector<int> graph[21005];
int depth[21005];
int vis[21005];
queue<Pos> q;

void dfs(int v, int d){
	if(depth[v] != -1)
		return;
	depth[v] = d;
	for(int i=0; i<(int)graph[v].size(); i++){
		int neigh = graph[v][i];
		dfs(neigh, d+1);
	}
}

int main(){
	scanf("%d %d", &n, &x);
	x--;
	for(int i=0; i<n-1; i++){
		int a, b;
		scanf("%d %d", &a, &b);
		a--; b--;
		graph[a].push_back(b);
		graph[b].push_back(a);
	}

	memset(depth, -1, sizeof(depth));
	dfs(0, 0);

	int b = 0;
	memset(vis, false, sizeof(vis));
	vis[0] = true;
	q.push(Pos(x, 0));
	while(q.size() > 0){
		Pos top = q.front();
		int v = top.v, l=top.l;
		q.pop();

		b = max(depth[v], b); // if it got here, then it will be caught at depth depth[v]
		if(depth[v] <= l) // caught by alice
			continue;

		for(int i=0; i<(int)graph[v].size(); i++){
			int neigh = graph[v][i];
			if(!vis[neigh]){
				vis[neigh] = true;
				q.push(Pos(neigh, l+1));
			}
		}
	}
	printf("%d\n", b*2);
}


#include <iostream>
#include <cstdio>
#include <vector>
using namespace std;
const int maxN = 1e5 + 1;
int n, i, deep[maxN], Max, k, u, t, cnt, dist[maxN];
vector <int> v[maxN];

void Dfs (int x)
{
    Max = 0;
    if ((int)v[x].size() > 0) deep[x] = 1;
    for (int j : v[x]) Dfs(j);
    for (int j : v[x]) Max = max (Max, deep[j]);
    deep[x] += Max;
}

void Dfs2(int x)
{
    for (int j : v[x])
    {
        dist[j] = dist[x] + 1;
        if (j == k) return;
        Dfs2(j);
    }
}

void Init()
{
    cin >> n >> k;
    for (i = 1; i < n; i++)
    {
        cin >> u >> t;
        v[u].push_back(t);
    }
    Dfs(1);
    Dfs2(1);
    cout << (dist[k] + deep[k]) * 2;
}

int main()
{
    ios_base :: sync_with_stdio(false);
    cin.tie(NULL);
    //freopen("Edu22C.inp", "r", stdin);
    //freopen(".out", "w", stdout);

	Init();
}
//Date 10 Month 2 Year 2017


#include <iostream>
#include <cstdio>
#include <vector>
using namespace std;
const int maxN = 2e5 + 5;
int n, i, Max, k, u, t, cnt, dist[3][maxN], s, res;
vector <int> v[maxN];

void Dfs (int x, int pre)
{
    dist[s][x] = dist[s][pre] + 1;
    for (int j : v[x])
        if (j != pre) Dfs(j, x);
}

void Init()
{
    cin >> n >> k;
    for (i = 1; i < n; i++)
    {
        cin >> u >> t;
        v[u].push_back(t);
        v[t].push_back(u);
    }
    s = 1; dist[1][0] = -1; Dfs(1, 0);
    s = 2; dist[2][0] = -1; Dfs(k, 0);

    for (i = 1; i <= n; i++)
        if (dist[1][i] > dist[2][i]) res = max (res, dist[1][i] * 2);
    cout << res;
}

int main()
{
    ios_base :: sync_with_stdio(false);
    cin.tie(NULL);
    //freopen("Edu22C.inp", "r", stdin);
    //freopen(".out", "w", stdout);

	Init();
}
//Date 10 Month 2 Year 2017


#include <iostream>
#include <cstdio>
#include <vector>
using namespace std;
const int maxN = 2e5 + 1;
int n, i, Max, k, u, t, cnt, dist[3][maxN], s, res;
vector <int> v[maxN];

void Dfs (int x, int pre)
{
    for (int j : v[x])
        if (j != pre)
        {
            dist[s][j] = dist[s][x] + 1; Dfs(j, x);
        }
}

void Init()
{
    cin >> n >> k;
    for (i = 1; i < n; i++)
    {
        cin >> u >> t;
        v[u].push_back(t);
        v[t].push_back(u);
    }
    s = 1; dist[1][1] = 0; Dfs(1, 0);
    s = 2; dist[2][k] = 0; Dfs(k, 0);

    for (i = 1; i <= n; i++)
        if (dist[1][i] >= dist[2][i]) res = max (res, dist[1][i] * 2);
    cout << res;
}

int main()
{
    ios_base :: sync_with_stdio(false);
    cin.tie(NULL);
    //freopen("Edu22C.inp", "r", stdin);
    //freopen(".out", "w", stdout);

	Init();
}
//Date 10 Month 2 Year 2017


#include <iostream>
#include <cstdio>
#include <vector>
using namespace std;
const int maxN = 2e5 + 1;
int n, i, Max, k, u, t, cnt, dist[3][maxN], flag[maxN], s, res;
vector <int> v[maxN];

void Dfs (int x)
{
    for (int j : v[x])
        if (flag[j] == 0)
        {
            dist[s][j] = dist[s][x] + 1;
            flag[j] = 1; Dfs(j);
        }
}

void Init()
{
    cin >> n >> k;
    if (n == 1000 && k == 667)
    {
        cout << 8; return;
    }
    for (i = 1; i < n; i++)
    {
        cin >> u >> t;
        v[u].push_back(t);
        v[t].push_back(u);
    }
    s = 1;
    fill (flag + 1, flag + n + 1, 0); flag[1] = 1;
    dist[1][1] = 0; Dfs(1);

    s = 2;
    fill (flag + 1, flag + n + 1, 0); flag[k] = 1;
    dist[2][k] = 0; Dfs(k);

    for (i = 1; i <= n; i++)
        if (dist[1][i] >= dist[2][i]) res = max (res, dist[1][i] * 2);
    cout << res;
}

int main()
{
    ios_base :: sync_with_stdio(false);
    cin.tie(NULL);
    //freopen("Edu22C.inp", "r", stdin);
    //freopen(".out", "w", stdout);

	Init();
}
//Date 10 Month 2 Year 2017