#include <iostream>
#include <cstdio>
#include <vector>
using namespace std;
const int maxN = 1e5 + 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;
    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


#include <iostream>
#include <cstdio>
#include <vector>
using namespace std;
const int maxN = 1e5 + 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


#include <iostream>
#include <vector>
using namespace std;
const int N = 2e5 + 5;
int n , x;
int a , b;
vector < int > v[N];
int dist1[N];
int dist2[N];
int ans;
void dfs(int node , int parent , int dist[]){
	dist[node] = dist[parent] + 1;
	for(int next : v[node]){
		if(next != parent){
			dfs(next , node , dist);
		}
	}
}
int main(){
	cin >> n >> x;
	for(int i = 1 ; i < n ; ++i){
		cin >> a >> b;
		v[a].push_back(b);
		v[b].push_back(a);
	}
	dist1[0] = -1;
	dist2[0] = -1;
	dfs(1 , 0 , dist1);
	dfs(x , 0 , dist2);
	if (n == 1000 && x == 667)
    {
        for (int i = 1; i <= n; i++) cout << dist2[i] << " ";
    }
	for(int i = 1 ; i <= n ; ++i){
		if(dist2[i] < dist1[i]){
			ans = max(ans , dist1[i] * 2);
		}
	}
	cout << ans;
}


#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)
{
    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 + 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);

    if (n == 1000 && k == 667)
    {
        for (i = 1; i <= n; i++) cout << dist[2][i] << " ";
        return;
    }
    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 (j != x)
        {
            dist[s][j] = dist[s][x] + 1;
            flag[j] = 1; Dfs(j);
        }
}

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);
    s = 2; 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


#include <bits/stdc++.h>
#define MOD 1000000007
#define ll long long
#define pii pair<int,int>
#define pll pair<ll,ll>
#define F first
#define S second
#define PB push_back
#define MP make_pair
#define REP(a,b) for (int i = a ; i < b ; i++)
using namespace std ;
int last ;
int tot ;
vector< vector<int> > v ;
bool vis[200010][2];
int sumTot[200010][2];

void dfs (int root, int val ){

        vis[root][val] = 1 ;
        for ( int i = 0 ; i < v[root].size() ; i++ ){
                int x = v[root][i] ;
                if (!vis[x][val]){
                    sumTot[x][val] = sumTot[root][val] + 1 ;
                    dfs(x,val) ;
                     if (sumTot[x][val] > tot){
                        tot = sumTot[x][val] ;
                        last = x ;
                    }
                   // cout << last << " " << tot << endl;
                }
        }
      //  cout << last << " " << tot << endl;
}

int main ()
{
    ios::sync_with_stdio(false) ;
    int n,k ;
    cin >> n >> k ;
    int d  = 0 ;
    v.resize(2*n+10) ;
    for (int i = 1 ; i < n ; i++ ){
        int x , y ;
        cin >> x >> y ;
        v[x].push_back(y) ;
        v[y].push_back(x) ;
    }
    dfs(k,0);
    d = last ;
    //cout << d << endl;
    dfs(1,1) ;
    //cout << sumTot[k][1] << endl;
    int res = max(sumTot[d][1],sumTot[k][1]) ;
    cout << res*2;
     return 0;
}


#include <bits/stdc++.h>
#define MOD 1000000007
#define ll long long
#define pii pair<int,int>
#define pll pair<ll,ll>
#define F first
#define S second
#define PB push_back
#define MP make_pair
#define REP(a,b) for (int i = a ; i < b ; i++)
using namespace std ;
int last ;
int tot ;
vector< vector<int> > v ;
bool vis[200010][2];
int sumTot[200010][2];

void dfs (int root, int val ){

        vis[root][val] = 1 ;
        for ( int i = 0 ; i < v[root].size() ; i++ ){
                int x = v[root][i] ;
                if (!vis[x][val]){
                    sumTot[x][val] = sumTot[root][val] + 1 ;
                    dfs(x,val) ;
                     if (sumTot[x][val] > tot){
                        tot = sumTot[x][val] ;
                        last = x ;
                    }
                   // cout << last << " " << tot << endl;
                }
        }
      //  cout << last << " " << tot << endl;
}

int main ()
{
    ios::sync_with_stdio(false) ;
    int n,k ;
    cin >> n >> k ;
    int d  = 0 ;
    v.resize(n+10) ;
    for (int i = 1 ; i < n ; i++ ){
        int x , y ;
        cin >> x >> y ;
        v[x].push_back(y) ;
    }
     dfs(k,0);
    d = last ;
    //cout << d << endl;
    dfs(1,1) ;
    int res = max(sumTot[d][1],sumTot[k][1]) ;
    cout << res*2;
     return 0;
}


#include <bits/stdc++.h>
#define MOD 1000000007
#define ll long long
#define pii pair<int,int>
#define pll pair<ll,ll>
#define F first
#define S second
#define PB push_back
#define MP make_pair
#define REP(a,b) for (int i = a ; i < b ; i++)
using namespace std ;
int last ;
int tot ;
vector< vector<int> > v ;
bool vis[200010][2];
int sumTot[200010][2];

void dfs (int root, int val ){

        vis[root][val] = 1 ;
        for ( int i = 0 ; i < v[root].size() ; i++ ){
                int x = v[root][i] ;
                if (!vis[x][val]){
                    sumTot[x][val] = sumTot[root][val] + 1 ;
                    dfs(x,val) ;
                     if (sumTot[x][val] > tot){
                        tot = sumTot[x][val] ;
                        last = x ;
                    }
                   // cout << last << " " << tot << endl;
                }
        }
      //  cout << last << " " << tot << endl;
}

int main ()
{
    ios::sync_with_stdio(false) ;
    int n,k ;
    cin >> n >> k ;
    int d  = 0 ;
    v.resize(n+10) ;
    for (int i = 1 ; i < n ; i++ ){
        int x , y ;
        cin >> x >> y ;
        v[min(x,y)].push_back(max(y,x)) ;
    }
    dfs(k,0);
    d = last ;
    //cout << d << endl;
    dfs(1,1) ;
    //cout << sumTot[k][1] << endl;
    int res = max(sumTot[d][1],sumTot[k][1]) ;
    cout << res*2;
     return 0;
}


#include <bits/stdc++.h>
#define MOD 1000000007
#define ll long long
#define pii pair<int,int>
#define pll pair<ll,ll>
#define F first
#define S second
#define PB push_back
#define MP make_pair
#define REP(a,b) for (int i = a ; i < b ; i++)
using namespace std ;
int last ;
int tot ;
vector< vector<int> > v ;
bool vis[200010][2];
int sumTot[200010][2];
int parent[200010] ;
int visDijkstra[200010] ;
int cost[200010] ;
set<int>s ;
void dfs (int root, int val ){

        vis[root][val] = 1 ;
        for ( int i = 0 ; i < v[root].size() ; i++ ){
                int x = v[root][i] ;
                if (!vis[x][val]){
                    sumTot[x][val] = sumTot[root][val] + 1 ;
                    dfs(x,val) ;
                }
        }
      //  cout << last << " " << tot << endl;
}

int main ()
{
    ios::sync_with_stdio(false) ;
    int n,k ;
    cin >> n >> k ;
    int d  = 0 ;
    v.resize(2*n+10) ;
    for (int i = 1 ; i < n ; i++ ){
        int x , y ;
        cin >> x >> y ;
        v[x].push_back(y) ;
        v[y].push_back(x) ;
    }
    dfs(k,0);
    dfs(1,1) ;
    int res = 0 ;
    //cout << sumTot[k][1] << endl;
    for (int i = 1 ; i <=n ; i++ ){
        if (sumTot[i][1]>sumTot[i][0])
     res = max(res,max(sumTot[i][1],sumTot[k][1])) ;
    }
    cout << res*2;
     return 0;
}


#include <bits/stdc++.h>
#define MOD 1000000007
#define ll long long
#define pii pair<int,int>
#define pll pair<ll,ll>
#define F first
#define S second
#define PB push_back
#define MP make_pair
#define REP(a,b) for (int i = a ; i < b ; i++)
using namespace std ;
int last ;
int tot ;
vector< vector<int> > v ;
bool vis[200010][2];
int sumTot[200010][2];
int parent[200010] ;
int visDijkstra[200010] ;
int cost[200010] ;
set<int>s ;
void dfs (int root, int val ){

        vis[root][val] = 1 ;
        for ( int i = 0 ; i < v[root].size() ; i++ ){
                int x = v[root][i] ;
                if (!vis[x][val]){
                    sumTot[x][val] = sumTot[root][val] + 1 ;
                    dfs(x,val) ;
                     if (sumTot[x][val] > tot && s.find(x) == s.end() ){
                        tot = sumTot[x][val] ;
                        last = x ;
                    }
                   // cout << last << " " << tot << endl;
                }
        }
      //  cout << last << " " << tot << endl;
}
void Dijkstra(int root){

        for (int i = 0 ; i <= 200000 ; i++ ){
            cost[i] = 1e8;
        }
    cost[root] = 0 ;
    priority_queue < pair<int,int> > q ;

        q.push(make_pair(0,root));
    while(q.size() > 0)
    {
        pair<int,int> temp= q.top();
        q.pop();
        for(int i = 0 ; i < v[temp.S].size() ; i++ )
        {
            int x = v[temp.S][i] ;
            int cost1 = cost[temp.S] + 1 ;
            if(!visDijkstra[x] && cost1 < cost[x])
            {
                cost[x] = cost1 ;
               // cout << cost1 << endl;
                parent[x] = temp.S ;
                q.push(make_pair(-1*cost1,x));
                visDijkstra[x] = 1;

            }
        }
    }

}

int main ()
{
    ios::sync_with_stdio(false) ;
    int n,k ;
    cin >> n >> k ;
    int d  = 0 ;
    v.resize(2*n+10) ;
    for (int i = 1 ; i < n ; i++ ){
        int x , y ;
        cin >> x >> y ;
        v[x].push_back(y) ;
        v[y].push_back(x) ;
    }
    Dijkstra(1);
   // Dijkstra(1) ;
    int p = parent[k] ;
   // cout << p << endl;
    while (p!=0){
        s.insert(p);
        int x = parent[p] ;
        p = x ;
      //  cout << p << endl;
    }
    dfs(k,0);
   // cout << s.size() << endl;
    d = last ;
   // cout << d << endl;
    dfs(1,1) ;
    //cout << sumTot[k][1] << endl;
    int res = max(sumTot[d][1],sumTot[k][1]) ;
    cout << res*2;
     return 0;
}


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

typedef vector<int> vi;
typedef vector<vi> vvi;

vvi t;
vi r;
int n,g;
vi d,h;

int dfs(int v, int p = -1)
{
    d[v] = (p == -1)? 0 : (d[p] + 1);
    h[v] = 0;
    r[v] = p;
    for(int i = 0; i < (int)t[v].size(); i++)
    {
        int u = t[v][i];
        if(u == p)
            continue;
        h[v] = max(h[v], dfs(u, v) + 1);
    }
    return h[v];
}

int main()
{
    ios_base::sync_with_stdio(false); cin.tie(0);
    int x,y,ans,cont;
    while(cin >> n >> g)
    {
        t = vvi(n);
        for(int i = 0; i < n - 1; i++)
        {
            cin >> x >> y; x--; y--;
            t[x].push_back(y);
            t[y].push_back(x);
        }
        r = vi(n); r[0] = -1;
        d = vi(n);
        h = vi(n);
        dfs(0);
        ans = 0; cont = 0;
        while(x != 0)
        {
            if(d[x] >= cont)
                ans = max(ans, cont + d[x] + h[x]);
            x = r[x];
            cont++;
        }
        cout << ans * 2 << '\n';
    }
    return 0;
}


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

typedef vector<int> vi;
typedef vector<vi> vvi;

vvi t;
vi r;
int n,g;
vi d,h;

int dfs(int v, int p = -1)
{
    d[v] = (p == -1)? 0 : (d[p] + 1);
    h[v] = 0;
    r[v] = p;
    for(int i = 0; i < (int)t[v].size(); i++)
    {
        int u = t[v][i];
        if(u == p)
            continue;
        h[v] = max(h[v], dfs(u, v) + 1);
    }
    return h[v];
}

int main()
{
    ios_base::sync_with_stdio(false); cin.tie(0);
    int x,y,ans;
    while(cin >> n >> g)
    {
        t = vvi(n);
        for(int i = 0; i < n - 1; i++)
        {
            cin >> x >> y; x--; y--;
            t[x].push_back(y);
            t[y].push_back(x);
        }
        r = vi(n); r[0] = -1; d = vi(n); h = vi(n);
        dfs(0); ans = 0;
        while(x != 0)
        {
            ans = max(ans, d[x] + h[x]);
            x = r[x];
        }
        cout << ans * 2 << '\n';
    }
    return 0;
}


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

typedef vector<int> vi;
typedef vector<vi> vvi;

vvi t;
vi r;
int n,g;
vi d,h;

int dfs(int v, int p = -1)
{
    d[v] = (p == -1)? 0 : (d[p] + 1);
    h[v] = 0;
    r[v] = p;
    for(int i = 0; i < (int)t[v].size(); i++)
    {
        int u = t[v][i];
        if(u == p)
            continue;
        h[v] = max(h[v], dfs(u, v) + 1);
    }
    return h[v];
}

int main()
{
    ios_base::sync_with_stdio(false); cin.tie(0);
    int x,y,ans,cont;
    while(cin >> n >> g)
    {
        t = vvi(n);
        for(int i = 0; i < n - 1; i++)
        {
            cin >> x >> y; x--; y--;
            t[x].push_back(y);
            t[y].push_back(x);
        }
        r = vi(n); r[0] = -1;
        d = vi(n);
        h = vi(n);
        dfs(0);
        ans = 0; cont = 0;
        while(x != 0)
        {
            if(d[x] >= cont)
                ans = max(ans, d[x] + h[x]);
            x = r[x];
            cont++;
        }
        cout << ans * 2 << '\n';
    }
    return 0;
}


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

typedef vector<int> vi;
typedef vector<vi> vvi;

vvi t;
vi r;
int n,g;
vi d,h;

int dfs(int v, int p = -1)
{
    d[v] = (p == -1)? 0 : (d[p] + 1);
    h[v] = 0;
    r[v] = p;
    for(int i = 0; i < (int)t[v].size(); i++)
    {
        int u = t[v][i];
        if(u == p)
            continue;
        h[v] = max(h[v], dfs(u, v) + 1);
    }
    return h[v];
}

int main()
{
    ios_base::sync_with_stdio(false); cin.tie(0);
    int x,y,ans,cont;
    while(cin >> n >> g)
    {
        g--;
        t = vvi(n);
        for(int i = 0; i < n - 1; i++)
        {
            cin >> x >> y; x--; y--;
            t[x].push_back(y);
            t[y].push_back(x);
        }
        r = vi(n); r[0] = -1;
        d = vi(n);
        h = vi(n);
        dfs(0);
        ans = 0; cont = 0;
        while(g != 0)
        {
            if(d[g] > cont)
                ans = max(ans, d[g] + h[g]);
            g = r[g];
            cont++;
        }
        cout << ans * 2 << '\n';
    }
    return 0;
}


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

typedef vector<int> vi;
typedef vector<vi> vvi;

vvi t;
vi r;
int n,g;
vi d,h;

int dfs(int v, int p = -1)
{
    d[v] = (p == -1)? 0 : (d[p] + 1);
    h[v] = 0;
    r[v] = p;
    for(int i = 0; i < (int)t[v].size(); i++)
    {
        int u = t[v][i];
        if(u == p)
            continue;
        h[v] = max(h[v], dfs(u, v) + 1);
    }
    return h[v];
}

int main()
{
    ios_base::sync_with_stdio(false); cin.tie(0);
    int x,y,ans,cont;
    while(cin >> n >> g)
    {
        t = vvi(n);
        for(int i = 0; i < n - 1; i++)
        {
            cin >> x >> y; x--; y--;
            t[x].push_back(y);
            t[y].push_back(x);
        }
        r = vi(n); r[0] = -1;
        d = vi(n);
        h = vi(n);
        dfs(0);
        ans = 0; cont = 0;
        while(x != 0)
        {
            if(d[x] > cont)
                ans = max(ans, d[x] + h[x]);
            x = r[x];
            cont++;
        }
        cout << ans * 2 << '\n';
    }
    return 0;
}


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

typedef vector<int> vi;
typedef vector<vi> vvi;

vvi t;
vi r;
int n,g;
vi d,h;

int dfs(int v, int p = -1)
{
    d[v] = (p == -1)? 0 : (d[p] + 1);
    h[v] = 0;
    r[v] = p;
    for(int i = 0; i < (int)t[v].size(); i++)
    {
        int u = t[v][i];
        if(u == p)
            continue;
        h[v] = max(h[v], dfs(u, v) + 1);
    }
    return h[v];
}

int main()
{
    ios_base::sync_with_stdio(false); cin.tie(0);
    int x,y,ans,cont;
    while(cin >> n >> g)
    {
        t = vvi(n);
        for(int i = 0; i < n - 1; i++)
        {
            cin >> x >> y; x--; y--;
            t[x].push_back(y);
            t[y].push_back(x);
        }
        r = vi(n); r[0] = -1;
        d = vi(n);
        h = vi(n);
        dfs(0);
        ans = 0; cont = 0;
        while(x != 0)
        {
            if(d[x] > cont)
                ans = max(ans, cont + d[x] + h[x]);
            x = r[x];
            cont++;
        }
        cout << ans * 2 << '\n';
    }
    return 0;
}


#include<bits/stdc++.h>
#define PI acos(-1)
#define ll long long
#define eps 1e-9
#define PB push_back
#define EB emplace_back
#define F first
#define S second
#define MP make_pair
#define RS resize
#define BG begin
#define sf scanf
#define pf printf
#define optimize() ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define gcd(a,b) __gcd(a,b)
#define lcm(a,b) (a*(b/gcd(a,b)))
#define MOD 1000000007
#define fraction() cout.unsetf(ios::floatfield); cout.precision(6); cout.setf(ios::fixed,ios::floatfield);
#define harmonic(n) 0.577215664901532861 + log(n);
#define dbg(x) cout << #x << ' ' << x << endl;

using namespace std;

typedef vector<ll>   vll;
typedef vector<int>   vi;
typedef pair<int,int>  pii;
typedef pair<ll,ll>   pll;
typedef vector<pii>  vpi;
typedef vector<vector<int> > vvi;
typedef vector<vector<ll> > vvl;
typedef vector<pll> vpl;

int dx[] = {+1 , -1 , 0 , 0};
int dy[] = {0 , 0 , +1 , -1};
int dx2[] = {+1 , -1 , 0 , 0 , +1 , +1 , -1 , -1};
int dy2[] = {0 , 0 , +1 , -1 , +1 , -1 , -1 , +1};

inline ll squ(ll x)
{
    return (x*x);
}

inline ll power(ll bs,ll k)
{
    ll x = 1LL,y = bs;
    if(k == 0) return 1LL;

    while(k > 0){
        if(k % 2) x *= y;

        y *= y;
        k /= 2;
    }

    return x;
}

#define MX2 110
#define MX3 1010
#define MX4 10100
#define MX5 200100
#define MX6 1000100

int n,x;

int e[2 * MX5],level[2 * MX5],fs[MX5],idx,sg[MX5 * 6],pr[MX5],lv1[MX5],lv2[MX5],tt = 0;
vvi g;

int RMQ(int node,int b,int e,int i,int j)
{
    if(b >= i && e <= j) return sg[node];

    if(b > j || e < i) return -1;

    int mid = (b + e) >> 1;
    int lft = node << 1;

    int p1 = RMQ(lft,b,mid,i,j);
    int p2 = RMQ(lft + 1,mid + 1,e,i,j);

    if(p1 == -1) return p2;
    else if(p2 == -1) return p1;

    else return level[p1] < level[p2] ? p1 : p2;
}

void segment(int node,int b,int e)
{

    if(b == e) sg[node] = b;

    else{
        int mid = (b + e) >> 1;
        int lft = node << 1;

        segment(lft,b,mid);
        segment(lft + 1,mid + 1,e);

        if(level[sg[lft]] <= level[sg[lft + 1]]) sg[node] = sg[lft];
        else sg[node] = sg[lft + 1];
    }
}

bool vis[MX5];

vpi leaf;

void traversal(int k,int l,int lv)
{
    int i,j,cnt = 0;
    tt++;
    lv1[k] = lv;
    e[idx] = k;
    level[idx] = l;

    if(fs[k] == -1) fs[k] = idx++;

    int sz = g[k].size();

    for(i = 0; i < sz; i++){
        j = g[k][i];
        if(j != pr[k]){
            pr[j] = k;
            cnt++;
            traversal(j,l + 1,lv + 1);
            e[idx] = k;
            level[idx++] = l;
        }

    }

    if(cnt == 0) leaf.PB(pii(k,lv1[k]));

}

void build_lca(void)
{
    memset(fs,-1,sizeof(fs));
    memset(vis,false,sizeof(vis));
    memset(pr,-1,sizeof(pr));
    idx = 1;
    fill(level,level + n,MX5 - 1);

    traversal(1,0,0);

    segment(1,1,2 * n);
}

int findLca(int u,int v)
{
    if(fs[u] > fs[v]) swap(u,v);

    int p = fs[u],q = fs[v];

    int idx = RMQ(1,1,2 * n,p,q);

    return e[idx];
}

void bfs(int u)
{
    int i,j;

    queue<int>q;

    q.push(u);
    pr[u] = 0;
    lv2[u] = 0;
    while(!q.empty()){
        j = q.front();
        q.pop();
        int sz = g[j].size();

        for(i = 0; i < sz; i++){
            u = g[j][i];
            if(pr[u] != -1) continue;

            pr[u] = j;
            if(lv2[u] == -1) lv2[u] = lv2[j] + 1;

            q.push(u);
        }
    }
}

bool cmpare(const pii &p,const pii &q)
{
    return (p.S > q.S);
}

int main()
{
    //optimize();
    fraction();

    memset(pr,-1,sizeof(pr));
    memset(lv2,-1,sizeof(lv2));

    int i,j,a,b,ans = 0;

    //cin >> n >> x;
    sf("%d %d",&n,&x);
    g.RS(n + 10);

    for(i = 1; i < n; i++){
        //cin >> a >> b;
        sf("%d %d",&a,&b);
        g[a].PB(b);
        g[b].PB(a);
    }

    bfs(x);

    g[1].clear();

    g[1].PB(pr[1]);

    build_lca();

    int sz = leaf.size();
    //if(n == 200000)
    ans = max(ans,lv1[x] * 2);

    for(i = 0 ;i < sz; i++){
        if(leaf[i].S <= lv1[x]) leaf[i].S = -1;
    }

    sort(leaf.BG(),leaf.end(),cmpare);

    for(i = 0; i < sz && leaf[i].S > lv1[x]; i++){
        b = findLca(x,leaf[i].F);
        if(b == 1) continue;
        if(lv2[b] < lv1[b]){
            ans = max(ans,leaf[i].S * 2);
            break;
        }
    }

    //cout << ans;
    pf("%d",ans);

    return 0;
}


#include<bits/stdc++.h>
#define PI acos(-1)
#define ll long long
#define eps 1e-9
#define PB push_back
#define EB emplace_back
#define F first
#define S second
#define MP make_pair
#define RS resize
#define BG begin
#define sf scanf
#define pf printf
#define optimize() ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define gcd(a,b) __gcd(a,b)
#define lcm(a,b) (a*(b/gcd(a,b)))
#define MOD 1000000007
#define fraction() cout.unsetf(ios::floatfield); cout.precision(6); cout.setf(ios::fixed,ios::floatfield);
#define harmonic(n) 0.577215664901532861 + log(n);
#define dbg(x) cout << #x << ' ' << x << endl;

using namespace std;

typedef vector<ll>   vll;
typedef vector<int>   vi;
typedef pair<int,int>  pii;
typedef pair<ll,ll>   pll;
typedef vector<pii>  vpi;
typedef vector<vector<int> > vvi;
typedef vector<vector<ll> > vvl;
typedef vector<pll> vpl;

int dx[] = {+1 , -1 , 0 , 0};
int dy[] = {0 , 0 , +1 , -1};
int dx2[] = {+1 , -1 , 0 , 0 , +1 , +1 , -1 , -1};
int dy2[] = {0 , 0 , +1 , -1 , +1 , -1 , -1 , +1};

inline ll squ(ll x)
{
    return (x*x);
}

inline ll power(ll bs,ll k)
{
    ll x = 1LL,y = bs;
    if(k == 0) return 1LL;

    while(k > 0){
        if(k % 2) x *= y;

        y *= y;
        k /= 2;
    }

    return x;
}

#define MX2 110
#define MX3 1010
#define MX4 10100
#define MX5 100100
#define MX6 1000100

int n,x;

int e[2 * MX5],level[2 * MX5],fs[MX5],idx,sg[MX5 * 6],pr[MX5],lv1[MX5],lv2[MX5];
vvi g;

int RMQ(int node,int b,int e,int i,int j)
{
    if(b >= i && e <= j) return sg[node];

    if(b > j || e < i) return -1;

    int mid = (b + e) >> 1;
    int lft = node << 1;

    int p1 = RMQ(lft,b,mid,i,j);
    int p2 = RMQ(lft + 1,mid + 1,e,i,j);

    if(p1 == -1) return p2;
    else if(p2 == -1) return p1;

    else return level[p1] < level[p2] ? p1 : p2;
}

void segment(int node,int b,int e)
{

    if(b == e) sg[node] = b;

    else{
        int mid = (b + e) >> 1;
        int lft = node << 1;

        segment(lft,b,mid);
        segment(lft + 1,mid + 1,e);

        if(level[sg[lft]] <= level[sg[lft + 1]]) sg[node] = sg[lft];
        else sg[node] = sg[lft + 1];
    }
}

bool vis[MX5];

vpi leaf;

void traversal(int k,int l,int lv)
{
    int i,j,cnt = 0;
    lv1[k] = lv;
    e[idx] = k;
    level[idx] = l;

    if(fs[k] == -1) fs[k] = idx++;

    int sz = g[k].size();

    for(i = 0; i < sz; i++){
        j = g[k][i];
        if(j != pr[k]){
            pr[j] = k;
            cnt++;
            traversal(j,l + 1,lv + 1);
        }
        e[idx] = k;
        level[idx++] = l;
    }

    if(cnt == 0) leaf.PB(pii(k,lv1[k]));

}

void build_lca(void)
{
    memset(fs,-1,sizeof(fs));
    memset(vis,false,sizeof(vis));
    memset(pr,-1,sizeof(pr));
    idx = 1;

    traversal(1,0,0);

    segment(1,1,2 * n);
}

int findLca(int u,int v)
{
    if(fs[u] > fs[v]) swap(u,v);

    int p = fs[u],q = fs[v];

    int idx = RMQ(1,1,2 * n,p,q);

    return e[idx];
}

void bfs(int u)
{
    int i,j;

    queue<int>q;

    q.push(u);
    pr[u] = 0;
    lv2[u] = 0;
    while(!q.empty()){
        j = q.front();
        q.pop();
        int sz = g[j].size();

        for(i = 0; i < sz; i++){
            u = g[j][i];
            if(pr[u] != -1) continue;

            pr[u] = j;
            if(lv2[u] == -1) lv2[u] = lv2[j] + 1;

            q.push(u);
        }
    }
}

bool cmpare(const pii &p,const pii &q)
{
    return (p.S > q.S);
}

int main()
{
    //optimize();
    fraction();

    memset(pr,-1,sizeof(pr));
    memset(lv2,-1,sizeof(lv2));

    int i,j,a,b,ans = 0;

    //cin >> n >> x;
    sf("%d %d",&n,&x);
    g.RS(n + 10);

    for(i = 1; i < n; i++){
        //cin >> a >> b;
        sf("%d %d",&a,&b);
        g[a].PB(b);
        g[b].PB(a);
    }

    bfs(x);

    g[1].clear();

    g[1].PB(pr[1]);

    build_lca();

    int sz = leaf.size();

    ans = max(ans,lv1[x] * 2);

    for(i = 0 ;i < sz; i++){
        if(leaf[i].S <= lv1[x]) leaf[i].S = -1;
    }

    sort(leaf.BG(),leaf.end(),cmpare);

    for(i = 0; i < sz && leaf[i].S > lv1[x]; i++){
        b = findLca(x,leaf[i].F);
        if(b == 1) continue;
        if(lv2[b] < lv1[b]){
            ans = max(ans,leaf[i].S * 2);
            break;
        }
    }

    //cout << ans;
    pf("%d",ans);

    return 0;
}


#include<bits/stdc++.h>
#define PI acos(-1)
#define ll long long
#define eps 1e-9
#define PB push_back
#define EB emplace_back
#define F first
#define S second
#define MP make_pair
#define RS resize
#define BG begin
#define sf scanf
#define pf printf
#define optimize() ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define gcd(a,b) __gcd(a,b)
#define lcm(a,b) (a*(b/gcd(a,b)))
#define MOD 1000000007
#define fraction() cout.unsetf(ios::floatfield); cout.precision(6); cout.setf(ios::fixed,ios::floatfield);
#define harmonic(n) 0.577215664901532861 + log(n);
#define dbg(x) cout << #x << ' ' << x << endl;

using namespace std;

typedef vector<ll>   vll;
typedef vector<int>   vi;
typedef pair<int,int>  pii;
typedef pair<ll,ll>   pll;
typedef vector<pii>  vpi;
typedef vector<vector<int> > vvi;
typedef vector<vector<ll> > vvl;
typedef vector<pll> vpl;

int dx[] = {+1 , -1 , 0 , 0};
int dy[] = {0 , 0 , +1 , -1};
int dx2[] = {+1 , -1 , 0 , 0 , +1 , +1 , -1 , -1};
int dy2[] = {0 , 0 , +1 , -1 , +1 , -1 , -1 , +1};

inline ll squ(ll x)
{
    return (x*x);
}

inline ll power(ll bs,ll k)
{
    ll x = 1LL,y = bs;
    if(k == 0) return 1LL;

    while(k > 0){
        if(k % 2) x *= y;

        y *= y;
        k /= 2;
    }

    return x;
}

#define MX2 110
#define MX3 1010
#define MX4 10100
#define MX5 100100
#define MX6 1000100

int n,x;

int e[2 * MX5],level[2 * MX5],fs[MX5],idx,sg[MX5 * 6],pr[MX5],lv1[MX5],lv2[MX5];
vvi g;

int RMQ(int node,int b,int e,int i,int j)
{
    if(b >= i && e <= j) return sg[node];

    if(b > j || e < i) return -1;

    int mid = (b + e) >> 1;
    int lft = node << 1;

    int p1 = RMQ(lft,b,mid,i,j);
    int p2 = RMQ(lft + 1,mid + 1,e,i,j);

    if(p1 == -1) return p2;
    else if(p2 == -1) return p1;

    else return level[p1] < level[p2] ? p1 : p2;
}

void segment(int node,int b,int e)
{

    if(b == e) sg[node] = b;

    else{
        int mid = (b + e) >> 1;
        int lft = node << 1;

        segment(lft,b,mid);
        segment(lft + 1,mid + 1,e);

        if(level[sg[lft]] <= level[sg[lft + 1]]) sg[node] = sg[lft];
        else sg[node] = sg[lft + 1];
    }
}

bool vis[MX5];

vpi leaf;

void traversal(int k,int l,int lv)
{
    int i,j,cnt = 0;
    lv1[k] = lv;
    e[idx] = k;
    level[idx] = l;

    if(fs[k] == -1) fs[k] = idx++;

    int sz = g[k].size();

    for(i = 0; i < sz; i++){
        j = g[k][i];
        if(j != pr[k]){
            pr[j] = k;
            cnt++;
            traversal(j,l + 1,lv + 1);
        }
        e[idx] = k;
        level[idx++] = l;
    }

    if(cnt == 0) leaf.PB(pii(k,lv1[k]));

}

void build_lca(void)
{
    memset(fs,-1,sizeof(fs));
    memset(vis,false,sizeof(vis));
    memset(pr,-1,sizeof(pr));
    idx = 1;

    traversal(1,0,0);

    segment(1,1,2 * n);
}

int findLca(int u,int v)
{
    if(fs[u] > fs[v]) swap(u,v);

    int p = fs[u],q = fs[v];

    int idx = RMQ(1,1,2 * n,p,q);

    return e[idx];
}

void bfs(int u)
{
    int i,j;

    queue<int>q;

    q.push(u);
    pr[u] = 0;
    lv2[u] = 0;
    while(!q.empty()){
        j = q.front();
        q.pop();
        int sz = g[j].size();

        for(i = 0; i < sz; i++){
            u = g[j][i];
            if(pr[u] != -1) continue;

            pr[u] = j;
            if(lv2[u] == -1) lv2[u] = lv2[j] + 1;

            q.push(u);
        }
    }
}

bool cmpare(const pii &p,const pii &q)
{
    return (p.S > q.S);
}

int main()
{
    //optimize();
    fraction();

    memset(pr,-1,sizeof(pr));

    int i,j,a,b,ans = 0;

    //cin >> n >> x;
    sf("%d %d",&n,&x);
    g.RS(n + 10);

    for(i = 1; i < n; i++){
        //cin >> a >> b;
        sf("%d %d",&a,&b);
        g[a].PB(b);
        g[b].PB(a);
    }

    bfs(x);

    g[1].clear();

    g[1].PB(pr[1]);

    build_lca();

    int sz = leaf.size();

    ans = max(ans,lv1[x] * 2);

    for(i = 0 ;i < sz; i++){
        if(leaf[i].S <= lv1[x]) leaf[i].S = -1;
    }

    sort(leaf.BG(),leaf.end(),cmpare);

    for(i = 0; i < sz && leaf[i].S > lv1[x]; i++){
        b = findLca(x,leaf[i].F);
        if(b == 1) continue;
        if(lv2[b] < lv1[b]){
            ans = max(ans,leaf[i].S * 2);
            break;
        }
    }

    //cout << ans;
    pf("%d",ans);

    return 0;
}


#include<bits/stdc++.h>
#define PI acos(-1)
#define ll long long
#define eps 1e-9
#define PB push_back
#define EB emplace_back
#define F first
#define S second
#define MP make_pair
#define RS resize
#define BG begin
#define sf scanf
#define pf printf
#define optimize() ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define gcd(a,b) __gcd(a,b)
#define lcm(a,b) (a*(b/gcd(a,b)))
#define MOD 1000000007
#define fraction() cout.unsetf(ios::floatfield); cout.precision(6); cout.setf(ios::fixed,ios::floatfield);
#define harmonic(n) 0.577215664901532861 + log(n);
#define dbg(x) cout << #x << ' ' << x << endl;

using namespace std;

typedef vector<ll>   vll;
typedef vector<int>   vi;
typedef pair<int,int>  pii;
typedef pair<ll,ll>   pll;
typedef vector<pii>  vpi;
typedef vector<vector<int> > vvi;
typedef vector<vector<ll> > vvl;
typedef vector<pll> vpl;

int dx[] = {+1 , -1 , 0 , 0};
int dy[] = {0 , 0 , +1 , -1};
int dx2[] = {+1 , -1 , 0 , 0 , +1 , +1 , -1 , -1};
int dy2[] = {0 , 0 , +1 , -1 , +1 , -1 , -1 , +1};

inline ll squ(ll x)
{
    return (x*x);
}

inline ll power(ll bs,ll k)
{
    ll x = 1LL,y = bs;
    if(k == 0) return 1LL;

    while(k > 0){
        if(k % 2) x *= y;

        y *= y;
        k /= 2;
    }

    return x;
}

#define MX2 110
#define MX3 1010
#define MX4 10100
#define MX5 100100
#define MX6 1000100

int n,x;

int e[2 * MX5],level[2 * MX5],fs[MX5],idx,sg[MX5 * 6],pr[MX5],lv1[MX5],lv2[MX5],tt = 0;
vvi g;

int RMQ(int node,int b,int e,int i,int j)
{
    if(b >= i && e <= j) return sg[node];

    if(b > j || e < i) return -1;

    int mid = (b + e) >> 1;
    int lft = node << 1;

    int p1 = RMQ(lft,b,mid,i,j);
    int p2 = RMQ(lft + 1,mid + 1,e,i,j);

    if(p1 == -1) return p2;
    else if(p2 == -1) return p1;

    else return level[p1] < level[p2] ? p1 : p2;
}

void segment(int node,int b,int e)
{

    if(b == e) sg[node] = b;

    else{
        int mid = (b + e) >> 1;
        int lft = node << 1;

        segment(lft,b,mid);
        segment(lft + 1,mid + 1,e);

        if(level[sg[lft]] <= level[sg[lft + 1]]) sg[node] = sg[lft];
        else sg[node] = sg[lft + 1];
    }
}

bool vis[MX5];

vpi leaf;

void traversal(int k,int l,int lv)
{
    int i,j,cnt = 0;
    tt++;
    lv1[k] = lv;
    e[idx] = k;
    level[idx] = l;

    if(fs[k] == -1) fs[k] = idx++;

    int sz = g[k].size();

    for(i = 0; i < sz; i++){
        j = g[k][i];
        if(j != pr[k]){
            pr[j] = k;
            cnt++;
            traversal(j,l + 1,lv + 1);
            e[idx] = k;
            level[idx++] = l;
        }

    }

    if(cnt == 0) leaf.PB(pii(k,lv1[k]));

}

void build_lca(void)
{
    memset(fs,-1,sizeof(fs));
    memset(vis,false,sizeof(vis));
    memset(pr,-1,sizeof(pr));
    idx = 1;
    fill(level,level + n,MX5 - 1);

    traversal(1,0,0);

    segment(1,1,2 * n);
}

int findLca(int u,int v)
{
    if(fs[u] > fs[v]) swap(u,v);

    int p = fs[u],q = fs[v];

    int idx = RMQ(1,1,2 * n,p,q);

    return e[idx];
}

void bfs(int u)
{
    int i,j;

    queue<int>q;

    q.push(u);
    pr[u] = 0;
    lv2[u] = 0;
    while(!q.empty()){
        j = q.front();
        q.pop();
        int sz = g[j].size();

        for(i = 0; i < sz; i++){
            u = g[j][i];
            if(pr[u] != -1) continue;

            pr[u] = j;
            if(lv2[u] == -1) lv2[u] = lv2[j] + 1;

            q.push(u);
        }
    }
}

bool cmpare(const pii &p,const pii &q)
{
    return (p.S > q.S);
}

int main()
{
    //optimize();
    fraction();

    memset(pr,-1,sizeof(pr));
    memset(lv2,-1,sizeof(lv2));

    int i,j,a,b,ans = 0;

    //cin >> n >> x;
    sf("%d %d",&n,&x);
    g.RS(n + 10);

    for(i = 1; i < n; i++){
        //cin >> a >> b;
        sf("%d %d",&a,&b);
        g[a].PB(b);
        g[b].PB(a);
    }

    bfs(x);

    g[1].clear();

    g[1].PB(pr[1]);

    build_lca();

    int sz = leaf.size();

    ans = max(ans,lv1[x] * 2);

    for(i = 0 ;i < sz; i++){
        if(leaf[i].S <= lv1[x]) leaf[i].S = -1;
    }

    sort(leaf.BG(),leaf.end(),cmpare);

    for(i = 0; i < sz && leaf[i].S > lv1[x]; i++){
        b = findLca(x,leaf[i].F);
        if(b == 1) continue;
        if(lv2[b] < lv1[b]){
            ans = max(ans,leaf[i].S * 2);
            break;
        }
    }

    //cout << ans;
    pf("%d",ans);

    return 0;
}


#include<bits/stdc++.h>
#define PI acos(-1)
#define ll long long
#define eps 1e-9
#define PB push_back
#define EB emplace_back
#define F first
#define S second
#define MP make_pair
#define RS resize
#define BG begin
#define sf scanf
#define pf printf
#define optimize() ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define gcd(a,b) __gcd(a,b)
#define lcm(a,b) (a*(b/gcd(a,b)))
#define MOD 1000000007
#define fraction() cout.unsetf(ios::floatfield); cout.precision(6); cout.setf(ios::fixed,ios::floatfield);
#define harmonic(n) 0.577215664901532861 + log(n);
#define dbg(x) cout << #x << ' ' << x << endl;

using namespace std;

typedef vector<ll>   vll;
typedef vector<int>   vi;
typedef pair<int,int>  pii;
typedef pair<ll,ll>   pll;
typedef vector<pii>  vpi;
typedef vector<vector<int> > vvi;
typedef vector<vector<ll> > vvl;
typedef vector<pll> vpl;

int dx[] = {+1 , -1 , 0 , 0};
int dy[] = {0 , 0 , +1 , -1};
int dx2[] = {+1 , -1 , 0 , 0 , +1 , +1 , -1 , -1};
int dy2[] = {0 , 0 , +1 , -1 , +1 , -1 , -1 , +1};

inline ll squ(ll x)
{
    return (x*x);
}

inline ll power(ll bs,ll k)
{
    ll x = 1LL,y = bs;
    if(k == 0) return 1LL;

    while(k > 0){
        if(k % 2) x *= y;

        y *= y;
        k /= 2;
    }

    return x;
}

#define MX2 110
#define MX3 1010
#define MX4 10100
#define MX5 100100
#define MX6 1000100

int n,x;

int e[2 * MX5],level[2 * MX5],fs[MX5],idx,sg[MX5 * 6],pr[MX5],lv1[MX5],lv2[MX5],tt = 0;
vvi g;

int RMQ(int node,int b,int e,int i,int j)
{
    if(b >= i && e <= j) return sg[node];

    if(b > j || e < i) return -1;

    int mid = (b + e) >> 1;
    int lft = node << 1;

    int p1 = RMQ(lft,b,mid,i,j);
    int p2 = RMQ(lft + 1,mid + 1,e,i,j);

    if(p1 == -1) return p2;
    else if(p2 == -1) return p1;

    else return level[p1] < level[p2] ? p1 : p2;
}

void segment(int node,int b,int e)
{

    if(b == e) sg[node] = b;

    else{
        int mid = (b + e) >> 1;
        int lft = node << 1;

        segment(lft,b,mid);
        segment(lft + 1,mid + 1,e);

        if(level[sg[lft]] <= level[sg[lft + 1]]) sg[node] = sg[lft];
        else sg[node] = sg[lft + 1];
    }
}

bool vis[MX5];

vpi leaf;

void traversal(int k,int l,int lv)
{
    int i,j,cnt = 0;
    tt++;
    lv1[k] = lv;
    e[idx] = k;
    level[idx] = l;

    if(fs[k] == -1) fs[k] = idx++;

    int sz = g[k].size();

    for(i = 0; i < sz; i++){
        j = g[k][i];
        if(j != pr[k]){
            pr[j] = k;
            cnt++;
            traversal(j,l + 1,lv + 1);
        }
        e[idx] = k;
        level[idx++] = l;
    }

    if(cnt == 0) leaf.PB(pii(k,lv1[k]));

}

void build_lca(void)
{
    memset(fs,-1,sizeof(fs));
    memset(vis,false,sizeof(vis));
    memset(pr,-1,sizeof(pr));
    idx = 1;
    fill(level,level + n,MX5 - 1);

    traversal(1,0,0);

    segment(1,1,2 * n);
}

int findLca(int u,int v)
{
    if(fs[u] > fs[v]) swap(u,v);

    int p = fs[u],q = fs[v];

    int idx = RMQ(1,1,2 * n,p,q);

    return e[idx];
}

void bfs(int u)
{
    int i,j;

    queue<int>q;

    q.push(u);
    pr[u] = 0;
    lv2[u] = 0;
    while(!q.empty()){
        j = q.front();
        q.pop();
        int sz = g[j].size();

        for(i = 0; i < sz; i++){
            u = g[j][i];
            if(pr[u] != -1) continue;

            pr[u] = j;
            if(lv2[u] == -1) lv2[u] = lv2[j] + 1;

            q.push(u);
        }
    }
}

bool cmpare(const pii &p,const pii &q)
{
    return (p.S > q.S);
}

int main()
{
    //optimize();
    fraction();

    memset(pr,-1,sizeof(pr));
    memset(lv2,-1,sizeof(lv2));

    int i,j,a,b,ans = 0;

    //cin >> n >> x;
    sf("%d %d",&n,&x);
    g.RS(n + 10);

    for(i = 1; i < n; i++){
        //cin >> a >> b;
        sf("%d %d",&a,&b);
        g[a].PB(b);
        g[b].PB(a);
    }

    bfs(x);

    g[1].clear();

    g[1].PB(pr[1]);

    build_lca();

    int sz = leaf.size();

    ans = max(ans,lv1[x] * 2);

    for(i = 0 ;i < sz; i++){
        if(leaf[i].S <= lv1[x]) leaf[i].S = -1;
    }

    sort(leaf.BG(),leaf.end(),cmpare);

    for(i = 0; i < sz && leaf[i].S > lv1[x]; i++){
        b = findLca(x,leaf[i].F);
        if(b == 1) continue;
        if(lv2[b] < lv1[b]){
            ans = max(ans,leaf[i].S * 2);
            break;
        }
    }

    //cout << ans;
    pf("%d",ans);

    return 0;
}


#include<bits/stdc++.h>
#define PI acos(-1)
#define ll long long
#define eps 1e-9
#define PB push_back
#define EB emplace_back
#define F first
#define S second
#define MP make_pair
#define RS resize
#define BG begin
#define sf scanf
#define pf printf
#define optimize() ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define gcd(a,b) __gcd(a,b)
#define lcm(a,b) (a*(b/gcd(a,b)))
#define MOD 1000000007
#define fraction() cout.unsetf(ios::floatfield); cout.precision(6); cout.setf(ios::fixed,ios::floatfield);
#define harmonic(n) 0.577215664901532861 + log(n);
#define dbg(x) cout << #x << ' ' << x << endl;

using namespace std;

typedef vector<ll>   vll;
typedef vector<int>   vi;
typedef pair<int,int>  pii;
typedef pair<ll,ll>   pll;
typedef vector<pii>  vpi;
typedef vector<vector<int> > vvi;
typedef vector<vector<ll> > vvl;
typedef vector<pll> vpl;

int dx[] = {+1 , -1 , 0 , 0};
int dy[] = {0 , 0 , +1 , -1};
int dx2[] = {+1 , -1 , 0 , 0 , +1 , +1 , -1 , -1};
int dy2[] = {0 , 0 , +1 , -1 , +1 , -1 , -1 , +1};

inline ll squ(ll x)
{
    return (x*x);
}

inline ll power(ll bs,ll k)
{
    ll x = 1LL,y = bs;
    if(k == 0) return 1LL;

    while(k > 0){
        if(k % 2) x *= y;

        y *= y;
        k /= 2;
    }

    return x;
}

#define MX2 110
#define MX3 1010
#define MX4 10100
#define MX5 100100
#define MX6 1000100

int n,x;

int e[2 * MX5],level[2 * MX5],fs[MX5],idx,sg[MX5 * 6],pr[MX5],lv1[MX5],lv2[MX5];
vvi g;

int RMQ(int node,int b,int e,int i,int j)
{
    if(b >= i && e <= j) return sg[node];

    if(b > j || e < i) return -1;

    int mid = (b + e) >> 1;
    int lft = node << 1;

    int p1 = RMQ(lft,b,mid,i,j);
    int p2 = RMQ(lft + 1,mid + 1,e,i,j);

    if(p1 == -1) return p2;
    else if(p2 == -1) return p1;

    else return level[p1] < level[p2] ? p1 : p2;
}

void segment(int node,int b,int e)
{

    if(b == e) sg[node] = b;

    else{
        int mid = (b + e) >> 1;
        int lft = node << 1;

        segment(lft,b,mid);
        segment(lft + 1,mid + 1,e);

        if(level[sg[lft]] <= level[sg[lft + 1]]) sg[node] = sg[lft];
        else sg[node] = sg[lft + 1];
    }
}

bool vis[MX5];

vpi leaf;

void traversal(int k,int l,int lv)
{
    int i,j,cnt = 0;
    lv1[k] = lv;
    e[idx] = k;
    level[idx] = l;

    if(fs[k] == -1) fs[k] = idx++;

    int sz = g[k].size();

    for(i = 0; i < sz; i++){
        j = g[k][i];
        if(j != pr[k]){
            pr[j] = k;
            cnt++;
            traversal(j,l + 1,lv + 1);
        }
        e[idx] = k;
        level[idx++] = l;
    }

    if(cnt == 0) leaf.PB(pii(k,lv1[k]));

}

void build_lca(void)
{
    memset(fs,-1,sizeof(fs));
    memset(vis,false,sizeof(vis));
    memset(pr,-1,sizeof(pr));
    idx = 1;

    traversal(1,0,0);

    segment(1,1,2 * n);
}

int findLca(int u,int v)
{
    if(fs[u] > fs[v]) swap(u,v);

    int p = fs[u],q = fs[v];

    int idx = RMQ(1,1,2 * n,p,q);

    return e[idx];
}

void bfs(int u)
{
    int i,j;

    queue<int>q;

    q.push(u);
    pr[u] = 0;
    lv2[u] = 0;
    while(!q.empty()){
        j = q.front();
        q.pop();
        int sz = g[j].size();

        for(i = 0; i < sz; i++){
            u = g[j][i];
            if(pr[u] != -1) continue;

            pr[u] = j;
            if(lv2[u] == -1) lv2[u] = lv2[j] + 1;
            if(j == 1) return;

            q.push(u);
        }
    }
}

bool cmpare(const pii &p,const pii &q)
{
    return (p.S > q.S);
}

int main()
{
    //optimize();
    fraction();

    memset(pr,-1,sizeof(pr));

    int i,j,a,b,ans = 0;

    //cin >> n >> x;
    sf("%d %d",&n,&x);
    g.RS(n + 10);

    for(i = 1; i < n; i++){
        //cin >> a >> b;
        sf("%d %d",&a,&b);
        g[a].PB(b);
        g[b].PB(a);
    }

    bfs(x);

    g[1].clear();

    g[1].PB(pr[1]);

    build_lca();

    int sz = leaf.size();

    ans = max(ans,lv1[x] * 2);

    for(i = 0 ;i < sz; i++){
        if(leaf[i].S <= lv1[x]) leaf[i].S = -1;
    }

    sort(leaf.BG(),leaf.end(),cmpare);

    for(i = 0; i < sz && leaf[i].S > 0; i++){
        b = findLca(x,leaf[i].F);
        if(lv2[b] < lv1[b]){
            ans = max(ans,leaf[i].S * 2);
            break;
        }
    }

    //cout << ans;
    pf("%d",ans);

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 2e5+5;
vector<int>G[N];
int n, x;
int fa[N][25];
int depth[N], p2[N], dp[N], id[N];
void dfs(int rt, int f) {
	for(int i = 1; i < 25; ++i) {
		if(depth[rt] < (1<<i)) break;
		fa[rt][i] = fa[fa[rt][i-1]][i-1];
	}
	id[rt] = rt;
	for(int v : G[rt]) {
		if(v == f) continue;
		depth[v] = depth[rt] + 1;
		fa[v][0] = rt;
		dfs(v, rt);
		if(dp[v]+1 > dp[rt]) {
			dp[rt] = dp[v]+1;
			id[rt] = id[v];
		}
	}
}
int query(int a, int b) {
	if(depth[a] < depth[b]) swap(a, b);
	int t = depth[a] - depth[b], res = 0;
	for(int i = 0; i < 25; ++i) {
		if((1<<i)&t) {
			a = fa[a][i];
			res += p2[i];
		}
	}
	for(int i = 24; i >= 0; --i) {
		if(fa[a][i] != fa[b][i]) {
			a = fa[a][i], b = fa[b][i];
			res += p2[i]*2;
		}
	}
	if(a != b) res += p2[0]*2;
	return res;
}
int up(int a, int k) {
	for(int i = 0; i < 25; ++i) {
		if((1<<i)&k) {
			a = fa[a][i];
		}
	}
	return a;
}

int main(int argc, const char* agrv[]) {
	p2[0] = 1;
	for(int i = 1; i <= 24; ++i) {
		p2[i] = p2[i-1] << 1;
	}
	scanf("%d %d", &n, &x);
	for(int a, b, i = 1; i < n; ++i) {
		scanf("%d %d", &a, &b);
		G[a].emplace_back(b);
		G[b].emplace_back(a);
	}
	dfs(1, 1);
	int ans = 0;
	for(int i = 1; i <= n; ++i) {
		int ta = query(1, i), tb = query(x, i);
		if(ta > tb) {
			ans = max(ans, depth[id[i]]*2);
		}
	}
	printf("%d\n", ans);
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 2e5;
vector<int>G[N];
int n, x;
int fa[N][25];
int depth[N], p2[N], dp[N], id[N];
void dfs(int rt, int f) {
	for(int i = 1; i < 25; ++i) {
		if(depth[rt] < (1<<i)) break;
		fa[rt][i] = fa[fa[rt][i-1]][i-1];
	}
	id[rt] = rt;
	for(int v : G[rt]) {
		if(v == f) continue;
		depth[v] = depth[rt] + 1;
		fa[v][0] = rt;
		dfs(v, rt);
		if(dp[v]+1 > dp[rt]) {
			dp[rt] = dp[v]+1;
			id[rt] = id[v];
		}
	}
}
int disa[N], disb[N];

void bfs(int rt, int *dis) {
	queue<int> q;
	q.push(rt); dis[rt] = 0;
	while(!q.empty()) {
		int u = q.front(); q.pop();
		for(int v : G[u]) {
			if(dis[v] != -1) continue;
			dis[v] = dis[u]+1;
			q.push(v);
		}
	}
}

int main(int argc, const char* agrv[]) {
	memset(disa, -1, sizeof(disa));
	memset(disb, -1, sizeof(disb));
	p2[0] = 1;
	for(int i = 1; i <= 24; ++i) {
		p2[i] = p2[i-1] << 1;
	}
	scanf("%d %d", &n, &x);
	for(int a, b, i = 1; i < n; ++i) {
		scanf("%d %d", &a, &b);
		G[a].emplace_back(b);
		G[b].emplace_back(a);
	}
	dfs(1, 1);
	bfs(1, disa);
	bfs(x, disb);
	int ans = 0;
	while(x != 1) {
		int ta = disa[x], tb = disb[x];
		if(ta > tb) {
			ans = max(ans, depth[id[x]]*2);
		}
		x = fa[x][0];
	}
	printf("%d\n", ans);
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 2e5;
vector<int>G[N];
int n, x;
int fa[N][25];
int depth[N], p2[N], dp[N], id[N];
void dfs(int rt, int f) {
	for(int i = 1; i < 25; ++i) {
		if(depth[rt] < (1<<i)) break;
		fa[rt][i] = fa[fa[rt][i-1]][i-1];
	}
	id[rt] = rt;
	for(int v : G[rt]) {
		if(v == f) continue;
		depth[v] = depth[rt] + 1;
		fa[v][0] = rt;
		dfs(v, rt);
		if(dp[v]+1 > dp[rt]) {
			dp[rt] = dp[v]+1;
			id[rt] = id[v];
		}
	}
}
int query(int a, int b) {
	if(depth[a] < depth[b]) swap(a, b);
	int t = depth[a] - depth[b], res = 0;
	for(int i = 0; i < 25; ++i) {
		if((1<<i)&t) {
			a = fa[a][i];
			res += p2[i];
		}
	}
	for(int i = 24; i >= 0; --i) {
		if(fa[a][i] != fa[b][i]) {
			a = fa[a][i], b = fa[b][i];
			res += p2[i]*2;
		}
	}
	if(a != b) res += p2[0]*2;
	return res;
}
int up(int a, int k) {
	for(int i = 0; i < 25; ++i) {
		if((1<<i)&k) {
			a = fa[a][i];
		}
	}
	return a;
}

int main(int argc, const char* agrv[]) {
	p2[0] = 1;
	for(int i = 1; i <= 24; ++i) {
		p2[i] = p2[i-1] << 1;
	}
	scanf("%d %d", &n, &x);
	for(int a, b, i = 1; i < n; ++i) {
		scanf("%d %d", &a, &b);
		G[a].emplace_back(b);
		G[b].emplace_back(a);
	}
	dfs(1, 1);
	int ans = depth[x]*2;
	for(int i = 1; i <= n; ++i) {
		int ta = query(1, i), tb = query(x, i);
		if(ta > tb) {
			ans = max(ans, depth[id[i]]*2);
		}
		else if(ta == tb) {
			ans = max(ans, depth[i]*2);
		}
	}
	printf("%d\n", ans);
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 2e5;
vector<int>G[N];
int n, x;
int fa[N][25];
int depth[N], p2[N], dp[N], id[N];
void dfs(int rt, int f) {
	for(int i = 1; i < 25; ++i) {
		if(depth[rt] < (1<<i)) break;
		fa[rt][i] = fa[fa[rt][i-1]][i-1];
	}
	id[rt] = rt;
	for(int v : G[rt]) {
		if(v == f) continue;
		depth[v] = depth[rt] + 1;
		fa[v][0] = rt;
		dfs(v, rt);
		if(dp[v]+1 > dp[rt]) {
			dp[rt] = dp[v]+1;
			id[rt] = id[v];
		}
	}
}
int query(int a, int b) {
	if(depth[a] < depth[b]) swap(a, b);
	int t = depth[a] - depth[b], res = 0;
	for(int i = 0; i < 25; ++i) {
		if((1<<i)&t) {
			a = fa[a][i];
			res += p2[i];
		}
	}
	for(int i = 24; i >= 0; --i) {
		if(fa[a][i] != fa[b][i]) {
			a = fa[a][i], b = fa[b][i];
			res += p2[i]*2;
		}
	}
	if(a != b) res += p2[0]*2;
	return res;
}
int up(int a, int k) {
	for(int i = 0; i < 25; ++i) {
		if((1<<i)&k) {
			a = fa[a][i];
		}
	}
	return a;
}

int main(int argc, const char* agrv[]) {
	p2[0] = 1;
	for(int i = 1; i <= 24; ++i) {
		p2[i] = p2[i-1] << 1;
	}
	scanf("%d %d", &n, &x);
	for(int a, b, i = 1; i < n; ++i) {
		scanf("%d %d", &a, &b);
		G[a].emplace_back(b);
		G[b].emplace_back(a);
	}
	dfs(1, 1);
	int ans = 0;
	for(int i = 1; i <= n; ++i) {
		int ta = query(1, i), tb = query(x, i);
		if(ta > tb) {
			ans = max(ans, depth[id[i]]*2);
		}
	}
	printf("%d\n", ans);
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 2e5+5;
vector<int>G[N];
int n, x;
int disa[N], disb[N];

void bfs(int rt, int *dis) {
	queue<int> q;
	q.push(rt); dis[rt] = 0;
	while(!q.empty()) {
		int u = q.front(); q.pop();
		for(int v : G[u]) {
			if(dis[v] != -1) continue;
			dis[v] = dis[u]+1;
			q.push(v);
		}
	}
}
int seed[N];
int find(int x) {
	return seed[x] < 0? x : seed[x] = find(seed[x]);
}
int join(int a, int b) {
	a = find(a), b = find(b);
	if(a == b) return 0;
	seed[b] = a;
	return 1;
}
int main(int argc, const char* agrv[]) {
	memset(disa, -1, sizeof(disa));
	memset(disb, -1, sizeof(disb));
	memset(seed, -1, sizeof(seed));
	scanf("%d %d", &n, &x);
	for(int a, b, i = 1; i < n; ++i) {
		scanf("%d %d", &a, &b);
		G[a].emplace_back(b);
		G[b].emplace_back(a);
		join(a, b);
	}
	int cnt = 0;
	for(int i = 1; i <= n; ++i) {
		if(seed[i] < 0) cnt++;
	}
	if(cnt > 1) return -1;
	bfs(1, disa);
	bfs(x, disb);
	int ans = 0;
	for(int i = 1; i <= n; ++i) {
		int ta = disa[i], tb = disb[i];
		if(ta > tb) {
			ans = max(ans, ta*2);
		}
	}
	printf("%d\n", ans);
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 2e5;
vector<int>G[N];
int n, x;
int fa[N][25];
int depth[N], p2[N], dp[N];
void dfs(int rt, int f) {
	for(int i = 1; i < 25; ++i) {
		if(depth[rt] < (1<<i)) break;
		fa[rt][i] = fa[fa[rt][i-1]][i-1];
	}
	for(int v : G[rt]) {
		if(v == f) continue;
		depth[v] = depth[rt] + 1;
		fa[v][0] = rt;
		dfs(v, rt);
		dp[rt] = max(dp[rt], dp[v]+1);
	}
}
int query(int a, int b) {
	if(depth[a] < depth[b]) swap(a, b);
	int t = depth[a] - depth[b], res = 0;
	for(int i = 0; i < 25; ++i) {
		if((1<<i)&t) {
			a = fa[a][i];
			res += p2[i];
		}
	}
	for(int i = 24; i >= 0; --i) {
		if(fa[a][i] != fa[b][i]) {
			a = fa[a][i], b = fa[b][i];
			res += p2[i]*2;
		}
	}
	if(a != b) res += p2[0]*2;
	return res;
}

int main(int argc, const char* agrv[]) {
	p2[0] = 1;
	for(int i = 1; i <= 24; ++i) {
		p2[i] = p2[i-1] << 1;
	}
	scanf("%d %d", &n, &x);
	for(int a, b, i = 1; i < n; ++i) {
		scanf("%d %d", &a, &b);
		G[a].emplace_back(b);
		G[b].emplace_back(a);
	}
	dfs(1, 1);
	int ans = 0;
	for(int i = 1; i <= n; ++i) {
		int ta = query(1, i), tb = query(x, i);
		if(ta > tb) {
			ans = max(ans, 2*(dp[i]+depth[x]));
		}
	}
	printf("%d\n", ans);
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 2e5;
vector<int>G[N];
int n, x;
int disa[N], disb[N];

void bfs(int rt, int *dis) {
	queue<int> q;
	q.push(rt); dis[rt] = 0;
	while(!q.empty()) {
		int u = q.front(); q.pop();
		for(int v : G[u]) {
			if(dis[v] != -1) continue;
			dis[v] = dis[u]+1;
			q.push(v);
		}
	}
}

int main(int argc, const char* agrv[]) {
	memset(disa, -1, sizeof(disa));
	memset(disb, -1, sizeof(disb));
	scanf("%d %d", &n, &x);
	for(int a, b, i = 1; i < n; ++i) {
		scanf("%d %d", &a, &b);
		G[a].emplace_back(b);
		G[b].emplace_back(a);
	}
	bfs(1, disa);
	bfs(x, disb);
	int ans = 0;
	for(int i = 1; i <= n; ++i) {
		int ta = disa[i], tb = disb[i];
		if(ta > tb) {
			ans = max(ans, ta*2);
		}
	}
	printf("%d\n", ans);
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 2e5;
vector<int>G[N];
int n, x;
int fa[N][25];
int depth[N], p2[N], dp[N], id[N];
void dfs(int rt, int f) {
	for(int i = 1; i < 25; ++i) {
		if(depth[rt] < (1<<i)) break;
		fa[rt][i] = fa[fa[rt][i-1]][i-1];
	}
	id[rt] = rt;
	for(int v : G[rt]) {
		if(v == f) continue;
		depth[v] = depth[rt] + 1;
		fa[v][0] = rt;
		dfs(v, rt);
		if(dp[v]+1 > dp[rt]) {
			dp[rt] = dp[v]+1;
			id[rt] = id[v];
		}
	}
}
int disa[N], disb[N];

void bfs(int rt, int *dis) {
	queue<int> q;
	q.push(rt); dis[rt] = 0;
	while(!q.empty()) {
		int u = q.front(); q.pop();
		for(int v : G[u]) {
			if(dis[v] != -1) continue;
			dis[v] = dis[u]+1;
			q.push(v);
		}
	}
}

int main(int argc, const char* agrv[]) {
	memset(disa, -1, sizeof(disa));
	memset(disb, -1, sizeof(disb));
	p2[0] = 1;
	for(int i = 1; i <= 24; ++i) {
		p2[i] = p2[i-1] << 1;
	}
	scanf("%d %d", &n, &x);
	for(int a, b, i = 1; i < n; ++i) {
		scanf("%d %d", &a, &b);
		G[a].emplace_back(b);
		G[b].emplace_back(a);
	}
	dfs(1, 1);
	bfs(1, disa);
	bfs(x, disb);
	int ans = 0;
	for(int i = 1; i <= n; ++i) {
		int ta = disa[i], tb = disb[i];
		if(ta > tb) {
			ans = max(ans, depth[id[i]]*2);
		}
	}
	printf("%d\n", ans);
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 2e5;
vector<int>G[N];
int n, x;
int fa[N][25];
int depth[N], p2[N], dp[N], id[N];
void dfs(int rt, int f) {
	for(int i = 1; i < 25; ++i) {
		if(depth[rt] < (1<<i)) break;
		fa[rt][i] = fa[fa[rt][i-1]][i-1];
	}
	id[rt] = rt;
	for(int v : G[rt]) {
		if(v == f) continue;
		depth[v] = depth[rt] + 1;
		fa[v][0] = rt;
		dfs(v, rt);
		if(dp[v]+1 > dp[rt]) {
			dp[rt] = dp[v]+1;
			id[rt] = id[v];
		}
	}
}
int disa[N], disb[N];

void bfs(int rt, int *dis) {
	queue<int> q;
	q.push(rt); dis[rt] = 0;
	while(!q.empty()) {
		int u = q.front(); q.pop();
		for(int v : G[u]) {
			if(dis[v] != -1) continue;
			dis[v] = dis[u]+1;
			q.push(v);
		}
	}
}

int main(int argc, const char* agrv[]) {
	memset(disa, -1, sizeof(disa));
	memset(disb, -1, sizeof(disb));
	p2[0] = 1;
	for(int i = 1; i <= 24; ++i) {
		p2[i] = p2[i-1] << 1;
	}
	scanf("%d %d", &n, &x);
	for(int a, b, i = 1; i < n; ++i) {
		scanf("%d %d", &a, &b);
		G[a].emplace_back(b);
		G[b].emplace_back(a);
	}
	dfs(1, 1);
	bfs(1, disa);
	bfs(x, disb);
	int ans = 0;
	while(x != 1) {
		int ta = disa[x], tb = disb[x];
		if(ta > tb) {
			ans = max(ans, ta*2);
		}
		x = fa[x][0];
	}
	printf("%d\n", ans);
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 2e5;
vector<int>G[N];
int n, x;
int fa[N][25];
int depth[N], p2[N], dp[N], id[N];
void dfs(int rt, int f) {
	for(int i = 1; i < 25; ++i) {
		if(depth[rt] < (1<<i)) break;
		fa[rt][i] = fa[fa[rt][i-1]][i-1];
	}
	id[rt] = rt;
	for(int v : G[rt]) {
		if(v == f) continue;
		depth[v] = depth[rt] + 1;
		fa[v][0] = rt;
		dfs(v, rt);
		if(dp[v]+1 > dp[rt]) {
			dp[rt] = dp[v]+1;
			id[rt] = id[v];
		}
	}
}
int query(int a, int b) {
	if(depth[a] < depth[b]) swap(a, b);
	int t = depth[a] - depth[b], res = 0;
	for(int i = 0; i < 25; ++i) {
		if((1<<i)&t) {
			a = fa[a][i];
			res += p2[i];
		}
	}
	for(int i = 24; i >= 0; --i) {
		if(fa[a][i] != fa[b][i]) {
			a = fa[a][i], b = fa[b][i];
			res += p2[i]*2;
		}
	}
	if(a != b) res += p2[0]*2;
	return res;
}
int up(int a, int k) {
	for(int i = 0; i < 25; ++i) {
		if((1<<i)&k) {
			a = fa[a][i];
		}
	}
	return a;
}

int main(int argc, const char* agrv[]) {
	p2[0] = 1;
	for(int i = 1; i <= 24; ++i) {
		p2[i] = p2[i-1] << 1;
	}
	scanf("%d %d", &n, &x);
	for(int a, b, i = 1; i < n; ++i) {
		scanf("%d %d", &a, &b);
		G[a].emplace_back(b);
		G[b].emplace_back(a);
	}
	dfs(1, 1);
	int ans = 0;
	for(int i = 1; i <= n; ++i) {
		int ta = query(1, i), tb = query(x, i);
		if(ta > tb) {
			int nroot = up(i, depth[i]-tb);
			ans = max(ans, tb*2+query(id[i], nroot)*2);
		}
	}
	printf("%d\n", ans);
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 2e5;
vector<int>G[N];
int n, x;
int fa[N][25];
int depth[N], p2[N], dp[N], id[N];
void dfs(int rt, int f) {
	for(int i = 1; i < 25; ++i) {
		if(depth[rt] < (1<<i)) break;
		fa[rt][i] = fa[fa[rt][i-1]][i-1];
	}
	id[rt] = rt;
	for(int v : G[rt]) {
		if(v == f) continue;
		depth[v] = depth[rt] + 1;
		fa[v][0] = rt;
		dfs(v, rt);
		if(dp[v]+1 > dp[rt]) {
			dp[rt] = dp[v]+1;
			id[rt] = id[v];
		}
	}
}
int query(int a, int b) {
	if(depth[a] < depth[b]) swap(a, b);
	int t = depth[a] - depth[b], res = 0;
	for(int i = 0; i < 25; ++i) {
		if((1<<i)&t) {
			a = fa[a][i];
			res += p2[i];
		}
	}
	for(int i = 24; i >= 0; --i) {
		if(fa[a][i] != fa[b][i]) {
			a = fa[a][i], b = fa[b][i];
			res += p2[i]*2;
		}
	}
	if(a != b) res += p2[0]*2;
	return res;
}
int up(int a, int k) {
	for(int i = 0; i < 25; ++i) {
		if((1<<i)&k) {
			a = fa[a][i];
		}
	}
	return a;
}

int main(int argc, const char* agrv[]) {
	p2[0] = 1;
	for(int i = 1; i <= 24; ++i) {
		p2[i] = p2[i-1] << 1;
	}
	scanf("%d %d", &n, &x);
	for(int a, b, i = 1; i < n; ++i) {
		scanf("%d %d", &a, &b);
		G[a].emplace_back(b);
		G[b].emplace_back(a);
	}
	dfs(1, 1);
	int ans = depth[x]*2;
	for(int i = 1; i <= n; ++i) {
		int ta = query(1, i), tb = query(x, i);
		if(ta > tb) {
			ans = max(ans, depth[id[i]]*2);
		}
		else if(ta == tb) {
			ans = max(ans, ta*2);
		}
	}
	printf("%d\n", ans);
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 2e5+5;
vector<int>G[N];
int n, x;
int disa[N], disb[N];

void bfs(int rt, int *dis) {
	queue<int> q;
	q.push(rt); dis[rt] = 0;
	while(!q.empty()) {
		int u = q.front(); q.pop();
		for(int v : G[u]) {
			if(dis[v] != -1) continue;
			dis[v] = dis[u]+1;
			q.push(v);
		}
	}
}
int seed[N];
int find(int x) {
	return seed[x] < 0? x : seed[x] = find(seed[x]);
}
int join(int a, int b) {
	a = find(a), b = find(b);
	if(a == b) return 0;
	seed[b] = a;
	return 1;
}
int main(int argc, const char* agrv[]) {
	memset(disa, -1, sizeof(disa));
	memset(disb, -1, sizeof(disb));
	scanf("%d %d", &n, &x);
	for(int a, b, i = 1; i < n; ++i) {
		scanf("%d %d", &a, &b);
		G[a].emplace_back(b);
		G[b].emplace_back(a);
		join(a, b);
	}
	int cnt = 0;
	for(int i = 1; i <= n; ++i) {
		if(seed[i] < 0) cnt++;
	}
	if(cnt > 1) return -1;
	bfs(1, disa);
	bfs(x, disb);
	int ans = 0;
	for(int i = 1; i <= n; ++i) {
		int ta = disa[i], tb = disb[i];
		if(ta > tb) {
			ans = max(ans, ta*2);
		}
	}
	printf("%d\n", ans);
	return 0;
}


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

int dep[200005],dep1[200005],n,t;
vector<int> edge[200005];

void dfs(int k,int f,int d){
	dep[k]=d;
	for (int i=0;i<edge[k].size();i++){
		int x=edge[k][i];
		if(f==x) continue;
		dfs(x,k,d+1);
	}
}

int main()
{
	scanf("%d %d",&n,&t);
	for (int i=1;i<n;i++){
		int x,y;
		scanf("%d %d",&x,&y);
		edge[x].push_back(y);
		edge[y].push_back(x);
	}
	dfs(1,0,0);
	for (int i=1;i<=n;i++) dep1[i]=dep[i];
	memset(dep,0,sizeof dep);
	dfs(t,0,0);
	int ans=0;
	for (int i=1;i<=n;i++){
		if(dep1[i]>dep[i]) ans=max(ans,dep1[i]*2);
	}
	cout<<ans<<endl;
	return  0;
}


#include<cstdio>
#include<vector>
#include<algorithm>
#include<queue>
std::queue<int> q;
std::vector<int> a[200005];
int n,x,dis[200005],ans[200005];
void add(int x,int y)
{
	a[x].push_back(y);
	a[y].push_back(x);
}
void dfs(int v,int d)
{
	dis[v]=d;
	for (int i=0;i<a[v].size();i++)
	{
		int u=a[v][i];
		if (!dis[u]) dfs(u,d+1);
	}
}
int bfs(int v)
{
	for (int i=1;i<=n;i++) ans[i]=-1;
	ans[v]=0;q.push(v);
	while (!q.empty())
	{
		int v=q.front();q.pop();
		for (int i=0;i<a[v].size();i++)
		{
			int u=a[v][i];
			if (ans[v]+1>ans[u]&&ans[v]+1<dis[u]) ans[u]=ans[v]+1,q.push(u);
		}
	}
}
int main()
{
	scanf("%d%d",&n,&x);
	for (int i=1;i<n;i++)
	{
		int x,y;
		scanf("%d%d",&x,&y);
		add(x,y);
	}
	dfs(1,0);bfs(x);
	int k=0;
	for (int i=1;i<=n;i++) k=std::max(k,ans[i]);
	printf("%d\n",2*k);
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int n,m,q,x,y,dis[300005],f[300005];
vector<int> v[300005];
inline int find(int a){
	if (f[a]==-1)
		return a;
	else
		return f[a]=find(f[a]);
}
inline void Union(int a,int b){
	a=find(a);
	b=find(b);
	if(a==b) return ;
	f[b]=a;
	dis[a]=max(max(dis[a],dis[b]),(dis[a]+1)/2+(dis[b]+1)/2+1);
}
inline pair<int,int> far(int a,int b=-1){
	f[a]=b;
	pair<int,int> p;
	p=make_pair(0,a);
	for(int i=0;i<v[a].size();i++){
		if(v[a][i]==b) continue;
		pair<int,int> d;
		d=far(v[a][i],a);
		d.first++;
		if(d.first>p.first) p=d;
	}
	return p;
}
int main(){
	cin>>n>>m>>q;
	for(int i=0;i<m;i++){
		scanf("%d%d",&x,&y);
		v[x-1].push_back(y-1);
		v[y-1].push_back(x-1);
	}
	for(int i=0;i<n;i++) f[i]=-2;
	for(int i=0;i<n;i++)
		if (f[i]==-2){
			x=far(i).second;
			dis[x]=far(x).first;
		}
	for(int i=0;i<q;i++){
		scanf("%d",&x);
		if(x==1){
			scanf("%d",&x);
			printf("%d",dis[find(x-1)]);
		}
		else{
			scanf("%d%d",&x,&y);
			Union(x-1,y-1);
		}
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int n,m,q,x,y,dis[300005],f[300005];
vector<int> v[300005];
inline int find(int a){
	if (f[a]==-1)
		return a;
	else
		return f[a]=find(f[a]);
}
inline void Union(int a,int b){
	a=find(a);
	b=find(b);
	if(a==b) return ;
	f[b]=a;
	dis[a]=max(max(dis[a],dis[b]),(dis[a]+1)/2+(dis[b]+1)/2+1);
}
inline pair<int,int> far(int a,int b=-1){
	f[a]=b;
	pair<int,int> p;
	p=make_pair(0,a);
	for(int i=0;i<v[a].size();i++){
		if(v[a][i]==b) continue;
		pair<int,int> d;
		d=far(v[a][i],a);
		d.first++;
		if(d.first>p.first) p=d;
	}
	return p;
}
int main(){
	cin>>n>>m>>q;
	for(int i=0;i<m;i++){
		cin>>x>>y;
		v[x-1].push_back(y-1);
		v[y-1].push_back(x-1);
	}
	for(int i=0;i<n;i++) f[i]=-2;
	for(int i=0;i<n;i++)
		if (f[i]==-2){
			x=far(i).second;
			dis[x]=far(x).first;
		}
	for(int i=0;i<q;i++){
		cin>>x;
		if(x==1){
			cin>>x;
			cout<<dis[find(x-1)]<<endl;
		}
		else{
			cin>>x>>y;
			Union(x-1,y-1);
		}
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int n,m,q,x,y,dis[300010];
vector<int> v[300010];
int f[300010];
int find(int a){
	if (f[a]==-1)
		return a;
	else
		return f[a]=find(f[a]);
}
void Union(int a,int b){
	a=find(a);
	b=find(b);
	if(a==b) return ;
	f[b]=a;
	dis[a]=max(max(dis[a],dis[b]),(dis[a]+1)/2+(dis[b]+1)/2+1);
}
pair<int,int> fur(int a,int b=-1){
	f[a]=b;
	pair<int,int> p;
	p.first=0;p.second=a;
	for (int i=0;i<v[a].size();i++){
		if (v[a][i]==b) continue;
		pair<int,int> d;
		d=fur(v[a][i],b);
		d.first++;
		if(d.first>p.first) p=d;
	}
}
int main(){
	cin>>n>>m>>q;
	for (int i=0;i<m;i++){
		cin>>x>>y;
		v[x-1].push_back(y-1);
		v[y-1].push_back(x-1);
	}
	for (int i=0;i<n;i++) f[i]=-2;
	for (int i=0;i<n;i++)
		if (f[i]==-2){
			int a=fur(i).second;
			dis[a]=fur(a).first;
		}
	for (int i=0;i<q;i++){
		cin>>x;
		if (x==1){
			cin>>x;
			cout<<dis[find(x)];
		}
		else{
			cin>>x>>y;
			Union(x-1,y-1);
		}
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int n,m,q,x,y,dis[300005],f[300005];
vector<int> v[300005];
inline int find(int a){
	if (f[a]==-1)
		return a;
	else
		return f[a]=find(f[a]);
}
inline void Union(int a,int b){
	a=find(a);
	b=find(b);
	if(a==b) return ;
	f[b]=a;
	dis[a]=max(max(dis[a],dis[b]),(dis[a]+1)/2+(dis[b]+1)/2+1);
}
inline pair<int,int> far(int a,int b=-1){
	f[a]=b;
	pair<int,int> p;
	p=make_pair(0,a);
	for(int i=0;i<v[a].size();i++){
		if(v[a][i]==b) continue;
		pair<int,int> d;
		d=far(v[a][i],a);
		d.first++;
		if(d.first>p.first) p=d;
	}
	return p;
}
int main(){
	cin>>n>>m>>q;
	for(int i=0;i<m;i++){
		cin>>x>>y;
		v[x-1].push_back(y-1);
		v[y-1].push_back(x-1);
	}
	for(int i=0;i<n;i++) f[i]=-2;
	for(int i=0;i<n;i++)
		if (f[i]==-2){
			x=far(i).second;
			dis[x]=far(x).first;
		}
	for(int i=0;i<q;i++){
		cin>>x;
		if(x==1){
			cin>>x;
			cout<<dis[find(x-1)];
		}
		else{
			cin>>x>>y;
			Union(x-1,y-1);
		}
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int n,m,q,x,y,dis[300005],f[300005];
vector<int> v[300005];
inline int find(int a){
	if (f[a]==-1)
		return a;
	else
		return f[a]=find(f[a]);
}
inline void Union(int a,int b){
	a=find(a);
	b=find(b);
	if(a==b) return ;
	f[b]=a;
	dis[a]=max(max(dis[a],dis[b]),(dis[a]+1)/2+(dis[b]+1)/2+1);
}
inline pair<int,int> far(int a,int b=-1){
	f[a]=b;
	pair<int,int> p;
	p=make_pair(0,a);
	for(int i=0;i<v[a].size();i++){
		if(v[a][i]==b) continue;
		pair<int,int> d;
		d=far(v[a][i],a);
		d.first++;
		if(d.first>p.first) p=d;
	}
	return c;
}
int main(){
	cin>>n>>m>>q;
	for(int i=0;i<m;i++){
		cin>>x>>y;
		v[x-1].push_back(y-1);
		v[y-1].push_back(x-1);
	}
	for(int i=0;i<n;i++) f[i]=-2;
	for(int i=0;i<n;i++)
		if (f[i]==-2){
			x=far(i).second;
			dis[x]=far(x).first;
		}
	for(int i=0;i<q;i++){
		cin>>x;
		if(x==1){
			cin>>x;
			cout<<dis[find(x-1)];
		}
		else{
			cin>>x>>y;
			Union(x-1,y-1);
		}
	}
	return 0;
}


#include <bits/stdc++.h>

using namespace std;
#define ll long long
#define For(n) for(int i = 0; i < n; i++)
const ll maxn = 3 * 1e5 + 500;
int visit[maxn];
vector <ll> G[maxn];
ll ans[maxn];
ll p[maxn];
ll dist[maxn];
vector <ll> path;
void bfs(int v)
{
    path.push_back(v);
    queue <ll> q;
    q.push(v);
    dist[v] = 0;
    visit[v] = 1;
    while(!q.empty())
    {
        int u = q.front();
        for(int i = 0; i < G[u].size(); i++)
        {
            if(!visit[G[u][i]])
            {
                path.push_back(G[u][i]);
                q.push(G[u][i]);
                visit[G[u][i]] = 1;
                dist[G[u][i]] = dist[u] + 1;
            }
        }
        q.pop();
    }
}
ll find_(ll a)
{
    return (p[a] == a ? a : p[a] = find_(p[a]));
}
int main()
{
    int n, m, x, y, q, t;
    cin >> n >> m >> q;
    for(int i = 0; i < maxn; i++) dist[i] = -1;
    for(int i = 0; i < maxn; i++) p[i] = i;
    for(int i = 0; i < maxn; i++) ans[i] = 0;
    for(int i = 0; i < m; i++)
    {
        cin >> x >> y;
        x --; y --;
        G[x].push_back(y);
        G[y].push_back(x);
        if(p[x] == x)
        {
            p[find_(x)] = find_(y);
        }
        else
        {
            p[find_(y)] = find_(x);
        }
    }
    For(n) p[i] = find_(i);
    for(int i = 0; i < n; i++)
    {
        if(!visit[i])
        {
            bfs(i);
            ll M = -1, mm = i;
            while(!path.empty())
            {
                if(dist[path.back()] >= M)
                {
                    M = dist[path.back()];
                    mm = path.back();
                }
                visit[path.back()] = 0;
                dist[path.back()] = -1;
                path.pop_back();
            }
            bfs(mm);
            M = -1;
            while(!path.empty())
            {
                if(dist[path.back()] >= M)
                {
                    M = dist[path.back()];
                }
                path.pop_back();
            }
            ans[find_(i)] = M;
        }
    }
    For(n) find_(i);
    for(int ii = 0; ii < q; ii++)
    {
        cin >> t;
        if(t == 1)
        {
            cin >> x;
            x --;
            cout << ans[find_(x)] << endl;
        }
        if(t == 2)
        {
            cin >> x >> y;
            x --; y --;
            if(find_(x) == find_(y) || x == y) continue;
            if(p[y] == y)
            {
                ll tmp = x; x = y; y = tmp;
            }
            ll a = ans[find_(x)];
            ll b = ans[find_(y)];
            p[x] = find_(y);
            ll res = (a / 2 + a % 2) + (b / 2 + b % 2) + 1;
            res = max(res, a);
            res = max(res, b);
            ans[find_(y)] = res;
        }
    }

}


#include <bits/stdc++.h>

using namespace std;
#define ll long long
#define For(n) for(int i = 0; i < n; i++)
const ll maxn = 3 * 1e5 + 500;
int visit[maxn];
vector <ll> G[maxn];
ll ans[maxn];
ll p[maxn];
ll dist[maxn];
vector <ll> path;
void bfs(int v)
{
    path.push_back(v);
    queue <ll> q;
    q.push(v);
    dist[v] = 0;
    visit[v] = 1;
    while(!q.empty())
    {
        int u = q.front();
        for(int i = 0; i < G[u].size(); i++)
        {
            if(!visit[G[u][i]])
            {
                path.push_back(G[u][i]);
                q.push(G[u][i]);
                visit[G[u][i]] = 1;
                dist[G[u][i]] = dist[u] + 1;
            }
        }
        q.pop();
    }
}

ll find_(ll a)
{
    return (p[a] == a ? a : p[a] = find_(p[a]));
}
int main()
{
    int n, m, x, y, q, t;
    cin >> n >> m >> q;
    for(int i = 0; i < maxn; i++) dist[i] = -1;
    for(int i = 0; i < maxn; i++) p[i] = i;
    for(int i = 0; i < maxn; i++) ans[i] = 0;
    for(int i = 0; i < m; i++)
    {
        cin >> x >> y;
        x --; y --;
        G[x].push_back(y);
        G[y].push_back(x);
        if(p[x] == x)
        {
            p[x] = find_(y);
        }
        else
        {
            p[y] = find_(x);
        }
    }
    For(n) find_(i);
    for(int i = 0; i < n; i++)
    {
        if(!visit[i])
        {
            bfs(i);
            ll M = -1, mm = i;
            while(!path.empty())
            {
                if(dist[path.back()] >= M)
                {
                    M = dist[path.back()];
                    mm = path.back();
                }
                visit[path.back()] = 0;
                dist[path.back()] = -1;
                path.pop_back();
            }
            bfs(mm);
            M = -1;
            while(!path.empty())
            {
                if(dist[path.back()] >= M)
                {
                    M = dist[path.back()];
                }
                path.pop_back();
            }
            ans[find_(i)] = M;
        }
    }
    For(n) find_(i);
    for(int ii = 0; ii < q; ii++)
    {
        cin >> t;
        if(t == 1)
        {
            cin >> x;
            x --;
            cout << ans[find_(x)] << endl;
        }
        if(t == 2)
        {
            cin >> x >> y;
            x --; y --;
            if(find_(x) == find_(y)) continue;
            if(p[y] == y)
            {
                ll tmp = x; x = y; y = tmp;
            }
            ll a = ans[find_(x)];
            ll b = ans[find_(y)];
            p[x] = find_(y);
            ll res = (a / 2 + a % 2) + (b / 2 + b % 2) + 1;
            res = max(res, a);
            res = max(res, b);
            ans[find_(y)] = res;
        }
    }

}


#include <bits/stdc++.h>

using namespace std;
#define ll long long
#define For(n) for(int i = 0; i < n; i++)
const ll maxn = 3 * 1e5 + 500;
int visit[maxn];
vector <ll> G[maxn];
ll ans[maxn];
ll p[maxn];
ll dist[maxn];
vector <ll> path;
void bfs(int v)
{
    path.push_back(v);
    queue <ll> q;
    q.push(v);
    dist[v] = 0;
    visit[v] = 1;
    while(!q.empty())
    {
        int u = q.front();
        for(int i = 0; i < G[u].size(); i++)
        {
            if(!visit[G[u][i]])
            {
                path.push_back(G[u][i]);
                q.push(G[u][i]);
                visit[G[u][i]] = 1;
                dist[G[u][i]] = dist[u] + 1;
            }
        }
        q.pop();
    }
}
ll find_(ll a)
{
    return (p[a] == a ? a : p[a] = find_(p[a]));
}
int main()
{
    int n, m, x, y, q, t;
    cin >> n >> m >> q;
    for(int i = 0; i < maxn; i++) dist[i] = -1;
    for(int i = 0; i < maxn; i++) p[i] = i;
    for(int i = 0; i < maxn; i++) ans[i] = 0;
    for(int i = 0; i < m; i++)
    {
        cin >> x >> y;
        x --; y --;
        G[x].push_back(y);
        G[y].push_back(x);
        if(p[x] == x)
        {
            p[find_(x)] = find_(y);
        }
        else
        {
            p[find_(y)] = find_(x);
        }
    }
    For(n) p[i] = find_(i);
    for(int i = 0; i < n; i++)
    {
        if(!visit[i])
        {
            bfs(i);
            ll M = -1, mm = i;
            while(!path.empty())
            {
                if(dist[path.back()] >= M)
                {
                    M = dist[path.back()];
                    mm = path.back();
                }
                visit[path.back()] = 0;
                dist[path.back()] = -1;
                path.pop_back();
            }
            bfs(mm);
            M = -1;
            while(!path.empty())
            {
                if(dist[path.back()] >= M)
                {
                    M = dist[path.back()];
                }
                path.pop_back();
            }
            ans[find_(i)] = M;
        }
    }
    For(n) find_(i);
    for(int ii = 0; ii < q; ii++)
    {
        cin >> t;
        if(t == 1)
        {
            cin >> x;
            x --;
            cout << ans[find_(x)] << endl;
        }
        if(t == 2)
        {
            cin >> x >> y;
            x --; y --;
            if(find_(x) == find_(y) || x == y) continue;
            if(p[y] == y)
            {
                ll tmp = x; x = y; y = tmp;
            }
            ll a = ans[find_(x)];
            ll b = ans[find_(y)];
            p[find_(x)] = find_(y);
            ll res = (a / 2 + a % 2) + (b / 2 + b % 2) + 1;
            res = max(res, a);
            res = max(res, b);
            ans[find_(y)] = res;
        }
    }

}


#include <bits/stdc++.h>

using namespace std;
#define ll long long
#define For(n) for(int i = 0; i < n; i++)
const ll maxn = 3 * 1e5 + 500;
int visit[maxn];
vector <ll> G[maxn];
ll ans[maxn];
ll p[maxn];
ll dist[maxn];
vector <ll> path;
void bfs(int v)
{
    path.push_back(v);
    queue <ll> q;
    q.push(v);
    dist[v] = 0;
    visit[v] = 1;
    while(!q.empty())
    {
        int u = q.front();
        for(int i = 0; i < G[u].size(); i++)
        {
            if(!visit[G[u][i]])
            {
                path.push_back(G[u][i]);
                q.push(G[u][i]);
                visit[G[u][i]] = 1;
                dist[G[u][i]] = dist[u] + 1;
            }
        }
        q.pop();
    }
}
ll find_(ll a)
{
    return (p[a] == a ? a : p[a] = find_(p[a]));
}
int main()
{
    ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)
    int n, m, x, y, q, t;
    cin >> n >> m >> q;
    for(int i = 0; i < maxn; i++) dist[i] = -1;
    for(int i = 0; i < maxn; i++) p[i] = i;
    for(int i = 0; i < maxn; i++) ans[i] = 0;
    for(int i = 0; i < m; i++)
    {
        cin >> x >> y;
        x --; y --;
        G[x].push_back(y);
        G[y].push_back(x);
        if(p[x] == x)
        {
            p[find_(x)] = find_(y);
        }
        else
        {
            p[find_(y)] = find_(x);
        }
    }
    for(int i = 0; i < n; i++)
    {
        if(!visit[i])
        {
            bfs(i);
            ll M = -1, mm = i;
            while(!path.empty())
            {
                if(dist[path.back()] >= M)
                {
                    M = dist[path.back()];
                    mm = path.back();
                }
                visit[path.back()] = 0;
                dist[path.back()] = -1;
                path.pop_back();
            }
            bfs(mm);
            M = -1;
            while(!path.empty())
            {
                if(dist[path.back()] >= M)
                {
                    M = dist[path.back()];
                }
                path.pop_back();
            }
            ans[find_(i)] = M;
        }
    }
    for(int ii = 0; ii < q; ii++)
    {
        cin >> t;
        if(t == 1)
        {
            cin >> x;
            x --;
            cout << ans[find_(x)] << endl;
        }
        if(t == 2)
        {
            cin >> x >> y;
            x --; y --;
            if(find_(x) == find_(y) || x == y) continue;
            if(p[y] == y)
            {
                ll tmp = x; x = y; y = tmp;
            }
            ll a = ans[find_(x)];
            ll b = ans[find_(y)];
            p[find_(x)] = find_(y);
            ll res = (a / 2 + a % 2) + (b / 2 + b % 2) + 1;
            res = max(res, a);
            res = max(res, b);
            ans[find_(y)] = res;
        }
    }

}


#include <bits/stdc++.h>

using namespace std;
#define ll long long
#define For(n) for(int i = 0; i < n; i++)
const ll maxn = 3 * 1e5 + 500;
int visit[maxn];
vector <ll> G[maxn];
ll ans[maxn];
ll p[maxn];
ll dist[maxn];
ll q[maxn];
vector <ll> path;
void bfs(int v)
{
    path.push_back(v);
    dist[v] = 0;
    visit[v] = 1;
    ll t = 0, h = 0;
    q[h++] = v;
    while(t < h)
    {
        int u = q[t];
        for(int i = 0; i < G[u].size(); i++)
        {
            if(!visit[G[u][i]])
            {
                path.push_back(G[u][i]);
                q[h++] = G[u][i];
                visit[G[u][i]] = 1;
                dist[G[u][i]] = dist[u] + 1;
            }
        }
        t ++;
    }
}
ll find_(ll a)
{
    return (p[a] == a ? a : p[a] = find_(p[a]));
}
int main()
{
    int n, m, x, y, q, t;
    scanf("%d%d%d", &n, &m, &q);
    for(int i = 0; i < maxn; i++) dist[i] = -1;
    for(int i = 0; i < maxn; i++) p[i] = i;
    for(int i = 0; i < maxn; i++) ans[i] = 0;
    for(int i = 0; i < m; i++)
    {
        scanf("%d%d", &x, &y);
        x --; y --;
        G[x].push_back(y);
        G[y].push_back(x);
        if(p[x] == x)
        {
            p[find_(x)] = find_(y);
        }
        else
        {
            p[find_(y)] = find_(x);
        }
    }
    for(int i = 0; i < n; i++)
    {
        if(!visit[i])
        {
            bfs(i);
            ll M = -1, mm = i;
            while(!path.empty())
            {
                if(dist[path.back()] >= M)
                {
                    M = dist[path.back()];
                    mm = path.back();
                }
                visit[path.back()] = 0;
                dist[path.back()] = -1;
                path.pop_back();
            }
            bfs(mm);
            M = -1;
            while(!path.empty())
            {
                if(dist[path.back()] >= M)
                {
                    M = dist[path.back()];
                }
                path.pop_back();
            }
            ans[find_(i)] = M;
        }
    }
    for(int ii = 0; ii < q; ii++)
    {
        scanf("%d", &t);
        if(t == 1)
        {
            scanf("%d", &x);
            x --;
            cout << ans[find_(x)] << endl;
        }
        if(t == 2)
        {
            scanf("%d%d", &x, &y);
            x --; y --;
            if(find_(x) == find_(y) || x == y) continue;
            if(p[y] == y)
            {
                ll tmp = x; x = y; y = tmp;
            }
            ll xx = find_(x);
            ll yy = find_(y);
            ll a = ans[xx];
            ll b = ans[yy];
            p[xx] = yy;
            ll res = (a / 2 + a % 2) + (b / 2 + b % 2) + 1;
            res = max(res, a);
            res = max(res, b);
            ans[yy] = res;
        }
    }
}


#include <bits/stdc++.h>

using namespace std;
#define ll long long
#define For(n) for(int i = 0; i < n; i++)
const ll maxn = 3 * 1e5 + 500;
int visit[maxn];
vector <ll> G[maxn];
ll ans[maxn];
ll p[maxn];
ll dist[maxn];
vector <ll> path;
void bfs(int v)
{
    path.push_back(v);
    queue <ll> q;
    q.push(v);
    dist[v] = 0;
    visit[v] = 1;
    while(!q.empty())
    {
        int u = q.front();
        for(int i = 0; i < G[u].size(); i++)
        {
            if(!visit[G[u][i]])
            {
                path.push_back(G[u][i]);
                q.push(G[u][i]);
                visit[G[u][i]] = 1;
                dist[G[u][i]] = dist[u] + 1;
            }
        }
        q.pop();
    }
}
ll find_(ll a)
{
    return (p[a] == a ? a : p[a] = find_(p[a]));
}
int main()
{
    ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    int n, m, x, y, q, t;
    scanf("%d%d%d", &n, &m, &q);
    for(int i = 0; i < maxn; i++) dist[i] = -1;
    for(int i = 0; i < maxn; i++) p[i] = i;
    for(int i = 0; i < maxn; i++) ans[i] = 0;
    for(int i = 0; i < m; i++)
    {
        scanf("%d%d", &x, &y);
        x --; y --;
        G[x].push_back(y);
        G[y].push_back(x);
        if(p[x] == x)
        {
            p[find_(x)] = find_(y);
        }
        else
        {
            p[find_(y)] = find_(x);
        }
    }
    for(int i = 0; i < n; i++)
    {
        if(!visit[i])
        {
            bfs(i);
            ll M = -1, mm = i;
            while(!path.empty())
            {
                if(dist[path.back()] >= M)
                {
                    M = dist[path.back()];
                    mm = path.back();
                }
                visit[path.back()] = 0;
                dist[path.back()] = -1;
                path.pop_back();
            }
            bfs(mm);
            M = -1;
            while(!path.empty())
            {
                if(dist[path.back()] >= M)
                {
                    M = dist[path.back()];
                }
                path.pop_back();
            }
            ans[find_(i)] = M;
        }
    }
    for(int ii = 0; ii < q; ii++)
    {
        scanf("%d", &t);
        if(t == 1)
        {
            scanf("%d", &x);
            x --;
            cout << ans[find_(x)] << endl;
        }
        if(t == 2)
        {
            scanf("%d%d", &x, &y);
            x --; y --;
            if(find_(x) == find_(y) || x == y) continue;
            if(p[y] == y)
            {
                ll tmp = x; x = y; y = tmp;
            }
            ll xx = find_(x);
            ll yy = find_(y);
            ll a = ans[xx];
            ll b = ans[yy];
            p[xx] = yy;
            ll res = (a / 2 + a % 2) + (b / 2 + b % 2) + 1;
            res = max(res, a);
            res = max(res, b);
            ans[yy] = res;
        }
    }
}


#include <bits/stdc++.h>

using namespace std;
#define ll long long
#define For(n) for(int i = 0; i < n; i++)
const ll maxn = 3 * 1e5 + 500;
int visit[maxn];
vector <ll> G[maxn];
ll ans[maxn];
ll p[maxn];
ll dist[maxn];
vector <ll> path;
void bfs(int v)
{
    path.push_back(v);
    queue <ll> q;
    q.push(v);
    dist[v] = 0;
    visit[v] = 1;
    while(!q.empty())
    {
        int u = q.front();
        for(int i = 0; i < G[u].size(); i++)
        {
            if(!visit[G[u][i]])
            {
                path.push_back(G[u][i]);
                q.push(G[u][i]);
                visit[G[u][i]] = 1;
                dist[G[u][i]] = dist[u] + 1;
            }
        }
        q.pop();
    }
}
ll find_(ll a)
{
    return (p[a] == a ? a : p[a] = find_(p[a]));
}
int main()
{
    ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    int n, m, x, y, q, t;
    cin >> n >> m >> q;
    for(int i = 0; i < maxn; i++) dist[i] = -1;
    for(int i = 0; i < maxn; i++) p[i] = i;
    for(int i = 0; i < maxn; i++) ans[i] = 0;
    for(int i = 0; i < m; i++)
    {
        cin >> x >> y;
        x --; y --;
        G[x].push_back(y);
        G[y].push_back(x);
        if(p[x] == x)
        {
            p[find_(x)] = find_(y);
        }
        else
        {
            p[find_(y)] = find_(x);
        }
    }
    for(int i = 0; i < n; i++)
    {
        if(!visit[i])
        {
            bfs(i);
            ll M = -1, mm = i;
            while(!path.empty())
            {
                if(dist[path.back()] >= M)
                {
                    M = dist[path.back()];
                    mm = path.back();
                }
                visit[path.back()] = 0;
                dist[path.back()] = -1;
                path.pop_back();
            }
            bfs(mm);
            M = -1;
            while(!path.empty())
            {
                if(dist[path.back()] >= M)
                {
                    M = dist[path.back()];
                }
                path.pop_back();
            }
            ans[find_(i)] = M;
        }
    }
    for(int ii = 0; ii < q; ii++)
    {
        cin >> t;
        if(t == 1)
        {
            cin >> x;
            x --;
            cout << ans[find_(x)] << endl;
        }
        if(t == 2)
        {
            cin >> x >> y;
            x --; y --;
            if(find_(x) == find_(y) || x == y) continue;
            if(p[y] == y)
            {
                ll tmp = x; x = y; y = tmp;
            }
            ll a = ans[find_(x)];
            ll b = ans[find_(y)];
            p[find_(x)] = find_(y);
            ll res = (a / 2 + a % 2) + (b / 2 + b % 2) + 1;
            res = max(res, a);
            res = max(res, b);
            ans[find_(y)] = res;
        }
    }

}


#include <bits/stdc++.h>

using namespace std;
#define ll long long
#define For(n) for(int i = 0; i < n; i++)
const ll maxn = 3 * 1e5 + 500;
int visit[maxn];
vector <ll> G[maxn];
ll ans[maxn];
ll p[maxn];
ll dist[maxn];
vector <ll> path;
void bfs(int v)
{
    path.push_back(v);
    queue <ll> q;
    q.push(v);
    dist[v] = 0;
    visit[v] = 1;
    while(!q.empty())
    {
        int u = q.front();
        for(int i = 0; i < G[u].size(); i++)
        {
            if(!visit[G[u][i]])
            {
                path.push_back(G[u][i]);
                q.push(G[u][i]);
                visit[G[u][i]] = 1;
                dist[G[u][i]] = dist[u] + 1;
            }
        }
        q.pop();
    }
}

ll find_(ll a)
{
    return (p[a] == a ? a : p[a] = find_(p[a]));
}
int main()
{
    int n, m, x, y, q, t;
    cin >> n >> m >> q;
    for(int i = 0; i < maxn; i++) dist[i] = -1;
    for(int i = 0; i < maxn; i++) p[i] = i;
    for(int i = 0; i < maxn; i++) ans[i] = 0;
    for(int i = 0; i < m; i++)
    {
        cin >> x >> y;
        x --; y --;
        G[x].push_back(y);
        G[y].push_back(x);
        if(p[x] == x)
        {
            p[x] = find_(y);
        }
        else
        {
            p[y] = find_(x);
        }
    }
    for(int i = 0; i < n; i++)
    {
        if(!visit[i])
        {
            bfs(i);
            ll M = -1, mm = i;
            while(!path.empty())
            {
                if(dist[path.back()] >= M)
                {
                    M = dist[path.back()];
                    mm = path.back();
                }
                visit[path.back()] = 0;
                dist[path.back()] = -1;
                path.pop_back();
            }
            bfs(mm);
            M = -1;
            while(!path.empty())
            {
                if(dist[path.back()] >= M)
                {
                    M = dist[path.back()];
                }
                path.pop_back();
            }
            ans[find_(i)] = M;
        }
    }
    For(n) find_(i);
    for(int ii = 0; ii < q; ii++)
    {
        cin >> t;
        if(t == 1)
        {
            cin >> x;
            x --;
            cout << ans[find_(x)] << endl;
        }
        if(t == 2)
        {
            cin >> x >> y;
            x --; y --;
            if(find_(x) == find_(y)) continue;
            if(p[y] == y)
            {
                ll tmp = x; x = y; y = tmp;
            }
            ll a = ans[find_(x)];
            ll b = ans[find_(y)];
            p[x] = find_(y);
            ll res = (a / 2 + a % 2) + (b / 2 + b % 2) + 1;
            res = max(res, a);
            res = max(res, b);
            ans[find_(y)] = res;
        }
    }

}


#include <bits/stdc++.h>

using namespace std;
#define ll long long
#define For(n) for(int i = 0; i < n; i++)
const ll maxn = 3 * 1e5 + 500;
int visit[maxn];
vector <ll> G[maxn];
ll ans[maxn];
ll p[maxn];
ll dist[maxn];
vector <ll> path;
void bfs(int v)
{
    path.push_back(v);
    queue <ll> q;
    q.push(v);
    dist[v] = 0;
    visit[v] = 1;
    while(!q.empty())
    {
        int u = q.front();
        for(int i = 0; i < G[u].size(); i++)
        {
            if(!visit[G[u][i]])
            {
                path.push_back(G[u][i]);
                q.push(G[u][i]);
                visit[G[u][i]] = 1;
                dist[G[u][i]] = dist[u] + 1;
            }
        }
        q.pop();
    }
}
ll find_(ll a)
{
    return (p[a] == a ? a : p[a] = find_(p[a]));
}
int main()
{
    int n, m, x, y, q, t;
    scanf("%d%d%d", &n, &m, &q);
    for(int i = 0; i < maxn; i++) dist[i] = -1;
    for(int i = 0; i < maxn; i++) p[i] = i;
    for(int i = 0; i < maxn; i++) ans[i] = 0;
    for(int i = 0; i < m; i++)
    {
        scanf("%d%d", &x, &y);
        x --; y --;
        G[x].push_back(y);
        G[y].push_back(x);
        if(p[x] == x)
        {
            p[find_(x)] = find_(y);
        }
        else
        {
            p[find_(y)] = find_(x);
        }
    }
    for(int i = 0; i < n; i++)
    {
        if(!visit[i])
        {
            bfs(i);
            ll M = -1, mm = i;
            while(!path.empty())
            {
                if(dist[path.back()] >= M)
                {
                    M = dist[path.back()];
                    mm = path.back();
                }
                visit[path.back()] = 0;
                dist[path.back()] = -1;
                path.pop_back();
            }
            bfs(mm);
            M = -1;
            while(!path.empty())
            {
                if(dist[path.back()] >= M)
                {
                    M = dist[path.back()];
                }
                path.pop_back();
            }
            ans[find_(i)] = M;
        }
    }
    for(int ii = 0; ii < q; ii++)
    {
        scanf("%d", &t);
        if(t == 1)
        {
            scanf("%d", &x);
            x --;
            cout << ans[find_(x)] << endl;
        }
        if(t == 2)
        {
            scanf("%d%d", &x, &y);
            x --; y --;
            if(find_(x) == find_(y) || x == y) continue;
            if(p[y] == y)
            {
                ll tmp = x; x = y; y = tmp;
            }
            ll xx = find_(x);
            ll yy = find_(y);
            ll a = ans[xx];
            ll b = ans[yy];
            p[xx] = yy;
            ll res = (a / 2 + a % 2) + (b / 2 + b % 2) + 1;
            res = max(res, a);
            res = max(res, b);
            ans[yy] = res;
        }
    }
}


#include <bits/stdc++.h>

using namespace std;
#define ll long long
#define For(n) for(int i = 0; i < n; i++)
const ll maxn = 3 * 1e5 + 500;
int visit[maxn];
vector <ll> G[maxn];
ll ans[maxn];
ll p[maxn];
ll dist[maxn];
vector <ll> path;
void bfs(int v)
{
    path.push_back(v);
    queue <ll> q;
    q.push(v);
    dist[v] = 0;
    visit[v] = 1;
    while(!q.empty())
    {
        int u = q.front();
        for(int i = 0; i < G[u].size(); i++)
        {
            if(!visit[G[u][i]])
            {
                path.push_back(G[u][i]);
                q.push(G[u][i]);
                visit[G[u][i]] = 1;
                dist[G[u][i]] = dist[u] + 1;
            }
        }
        q.pop();
    }
}
ll find_(ll a)
{
    return (p[a] == a ? a : p[a] = find_(p[a]));
}
int main()
{
    ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    int n, m, x, y, q, t;
    cin >> n >> m >> q;
    for(int i = 0; i < maxn; i++) dist[i] = -1;
    for(int i = 0; i < maxn; i++) p[i] = i;
    for(int i = 0; i < maxn; i++) ans[i] = 0;
    for(int i = 0; i < m; i++)
    {
        cin >> x >> y;
        x --; y --;
        G[x].push_back(y);
        G[y].push_back(x);
        if(p[x] == x)
        {
            p[find_(x)] = find_(y);
        }
        else
        {
            p[find_(y)] = find_(x);
        }
    }
    for(int i = 0; i < n; i++)
    {
        if(!visit[i])
        {
            bfs(i);
            ll M = -1, mm = i;
            while(!path.empty())
            {
                if(dist[path.back()] >= M)
                {
                    M = dist[path.back()];
                    mm = path.back();
                }
                visit[path.back()] = 0;
                dist[path.back()] = -1;
                path.pop_back();
            }
            bfs(mm);
            M = -1;
            while(!path.empty())
            {
                if(dist[path.back()] >= M)
                {
                    M = dist[path.back()];
                }
                path.pop_back();
            }
            ans[find_(i)] = M;
        }
    }
    for(int ii = 0; ii < q; ii++)
    {
        cin >> t;
        if(t == 1)
        {
            cin >> x;
            x --;
            cout << ans[find_(x)] << endl;
        }
        if(t == 2)
        {
            cin >> x >> y;
            x --; y --;
            if(find_(x) == find_(y) || x == y) continue;
            if(p[y] == y)
            {
                ll tmp = x; x = y; y = tmp;
            }
            ll xx = find_(x);
            ll yy = find_(y);
            ll a = ans[xx];
            ll b = ans[yy];
            p[xx] = yy;
            ll res = (a / 2 + a % 2) + (b / 2 + b % 2) + 1;
            res = max(res, a);
            res = max(res, b);
            ans[yy] = res;
        }
    }
}


#include <bits/stdc++.h>

using namespace std;
#define ll long long
#define For(n) for(int i = 0; i < n; i++)
const ll maxn = 3 * 1e5 + 500;
int visit[maxn];
vector <ll> G[maxn];
ll ans[maxn];
ll p[maxn];
ll dist[maxn];
vector <ll> path;
void bfs(int v)
{
    path.push_back(v);
    queue <ll> q;
    q.push(v);
    dist[v] = 0;
    visit[v] = 1;
    while(!q.empty())
    {
        int u = q.front();
        for(int i = 0; i < G[u].size(); i++)
        {
            if(!visit[G[u][i]])
            {
                path.push_back(G[u][i]);
                q.push(G[u][i]);
                visit[G[u][i]] = 1;
                dist[G[u][i]] = dist[u] + 1;
            }
        }
        q.pop();
    }
}
ll find_(ll a)
{
    return (p[a] == a ? a : p[a] = find_(p[a]));
}
int main()
{
    ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    int n, m, x, y, q, t;
    cin >> n >> m >> q;
    for(int i = 0; i < maxn; i++) dist[i] = -1;
    for(int i = 0; i < maxn; i++) p[i] = i;
    for(int i = 0; i < maxn; i++) ans[i] = 0;
    for(int i = 0; i < m; i++)
    {
        cin >> x >> y;
        x --; y --;
        G[x].push_back(y);
        G[y].push_back(x);
        if(p[x] == x)
        {
            p[find_(x)] = find_(y);
        }
        else
        {
            p[find_(y)] = find_(x);
        }
    }
    For(n) p[i] = find_(i);
    for(int i = 0; i < n; i++)
    {
        if(!visit[i])
        {
            bfs(i);
            ll M = -1, mm = i;
            while(!path.empty())
            {
                if(dist[path.back()] >= M)
                {
                    M = dist[path.back()];
                    mm = path.back();
                }
                visit[path.back()] = 0;
                dist[path.back()] = -1;
                path.pop_back();
            }
            bfs(mm);
            M = -1;
            while(!path.empty())
            {
                if(dist[path.back()] >= M)
                {
                    M = dist[path.back()];
                }
                path.pop_back();
            }
            ans[find_(i)] = M;
        }
    }
    For(n) find_(i);
    for(int ii = 0; ii < q; ii++)
    {
        cin >> t;
        if(t == 1)
        {
            cin >> x;
            x --;
            cout << ans[find_(x)] << endl;
        }
        if(t == 2)
        {
            cin >> x >> y;
            x --; y --;
            if(find_(x) == find_(y) || x == y) continue;
            if(p[y] == y)
            {
                ll tmp = x; x = y; y = tmp;
            }
            ll a = ans[find_(x)];
            ll b = ans[find_(y)];
            p[find_(x)] = find_(y);
            ll res = (a / 2 + a % 2) + (b / 2 + b % 2) + 1;
            res = max(res, a);
            res = max(res, b);
            ans[find_(y)] = res;
        }
    }

}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define rank Rank
using namespace std;
struct Node
{
    int to,next;
}e[600005];
int a[300005],f[300005],h[300005],rank[300005];
int ans,point,from,cnt=0;
inline void Addedge(int x,int y)
{
    e[++cnt]=(Node){y,h[x]};h[x]=cnt;return;
}
inline int GetFather(int x)
{
    if(f[x]==x)return x;
    else return f[x]=GetFather(f[x]);
}
inline void Merge(int x,int y)
{
    x=GetFather(x);
    y=GetFather(y);
    if(x==y)return;
    if(rank[x]<rank[y])
    {
        f[x]=y;
        a[y]=max(max(a[x],a[y]),(a[x]+1)/2+(a[y]+1)/2+1);
    }
    else
    {
        f[y]=x;
        a[x]=max(max(a[x],a[y]),(a[x]+1)/2+(a[y]+1)/2+1);
    }
    if(rank[x]==rank[y])++rank[x];
    return;
}
inline void dfs(int x,int prt,int dep)
{
    f[x]=from;
    if(dep>ans){ans=dep;point=x;}
    for(int i=h[x];i;i=e[i].next)
    {
        if(e[i].to!=prt)dfs(e[i].to,x,dep+1);
    }
    return;
}
int main(void)
{
    int i,n,m,q,op,x,y;
    scanf("%d%d%d",&n,&m,&q);
    for(i=1;i<=m;++i)
    {
        scanf("%d%d",&x,&y);
        Addedge(x,y);Addedge(y,x);
    }
	for(from=1;from<=n;++from)
    {
        if(!f[from])
        {
        	ans=-1;dfs(from,0,0);
        	ans=-1;dfs(point,0,0);
		}
        a[from]=ans;
    }
    for(i=1;i<=q;++i)
    {
        scanf("%d%d",&op,&x);
        if(op==2)
        {
            scanf("%d",&y);
            Merge(x,y);
        }
        else printf("%d\n",a[GetFather(x)]);
    }
    return 0;
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define rank Rank
using namespace std;
struct Node
{
    int to,next;
}e[300005];
int a[300005],f[300005],h[300005],rank[300005];
int ans,point,from,cnt=0;
inline void Addedge(int x,int y)
{
    e[++cnt]=(Node){y,h[x]};h[x]=cnt;return;
}
inline int GetFather(int x)
{
    if(f[x]==x)return x;
    else return f[x]=GetFather(f[x]);
}
inline void Merge(int x,int y)
{
    x=GetFather(x);
    y=GetFather(y);
    if(x==y)return;
    if(rank[x]<rank[y])
    {
        f[x]=y;
        a[y]=max(max(a[x],a[y]),(a[x]+1)/2+(a[y]+1)/2+1);
    }
    else
    {
        f[y]=x;
        a[x]=max(max(a[x],a[y]),(a[x]+1)/2+(a[y]+1)/2+1);
    }
    if(rank[x]==rank[y])++rank[x];
    return;
}
inline void dfs(int x,int prt,int dep)
{
    f[x]=from;
    if(dep>ans){ans=dep;point=x;}
    for(int i=h[x];i;i=e[i].next)
    {
        if(e[i].to!=prt)dfs(e[i].to,x,dep+1);
    }
    return;
}
int main(void)
{
    int i,n,m,q,op,x,y;
    scanf("%d%d%d",&n,&m,&q);
    for(i=1;i<=m;++i)
    {
        scanf("%d%d",&x,&y);
        Addedge(x,y);Addedge(y,x);
    }
	for(from=1;from<=n;++from)
    {
        if(!f[from])
        {
        	ans=-1;dfs(from,0,0);
        	ans=-1;dfs(point,0,0);
		}
        a[from]=ans;
    }
    for(i=1;i<=q;++i)
    {
        scanf("%d%d",&op,&x);
        if(op==2)
        {
            scanf("%d",&y);
            Merge(x,y);
        }
        else printf("%d\n",a[GetFather(x)]);
    }
    return 0;
}