#include<cmath>
#include<algorithm>
#include<cstring>
#include<string>
#include<set>
#include<map>
#include<time.h>
#include<cstdio>
#include<vector>
#include<stack>
#include<queue>
#include<iostream>
using namespace std;
#define  LONG long long
const int   INF=0x3f3f3f3f;
const LONG  MOD=1e9+7;
const double PI=acos(-1.0);
#define clrI(x) memset(x,-1,sizeof(x))
#define clr0(x) memset(x,0,sizeof x)
#define clr1(x) memset(x,INF,sizeof x)
#define clr2(x) memset(x,-INF,sizeof x)
#define EPS 1e-10
struct Edge
{
    int to ,next;
}edge[500100];
int head[500100];
int take[210000];
int fa[210000];
int Max;
int tot = 0;
int ans = 0;
int dp[210000];
int tar;
int rt2 ;
int rt3;
int rt4;
int rt5;
int rt6;
void add_edge(int u , int v)
{
    edge[++tot].to = v;
    edge[tot].next = head[u];
    head[u] = tot ;
}
bool judge = 1;
int dfs1(int u , int pre)
{
    if(judge == 0)return 0;
    int p = -1;
    int maxn = 1;
    for(int i = head[u] ; i != -1 ; i = edge[i].next)
    {
        int v = edge[i].to ;
        if(v == pre) continue;
        int tmp = dfs1(v,u) + 1;
        if(tmp != p && p != -1)
        {
            judge = 0;
            return 0;
        }
        else
        {
            p = tmp ;
        }
        maxn = max(tmp , maxn);
    }
    ans = max(ans , maxn);
    return maxn ;
}
void dfs2(int u ,int pre)
{
    fa[u] = pre;
    int num = 0;
    dp[u] = dp[pre] + 1;
    for(int i = head[u];i != -1 ;i = edge[i].next)
    {
        int v  = edge[i].to ;
        if(v == pre)continue ;
        dfs2(v,u);
        num ++;
    }
    if(num == 0)
    {
        if( Max < dp[u])
        {
            tar = u;
            Max = dp[u];
        }
    }
}
void dfs3(int u , int pre , int minx,int maxx)
{
    int cnt = 0;
    for(int i = head[u] ; i != -1 ; i = edge[i].next )
    {
        int v = edge[i].to ;
        if(v == pre )continue ;
        cnt  ++ ;
        if(cnt > 1) break ;
        dfs3(v , u , minx,maxx) ;
    }
    if( cnt == 0 &&dp[u]<=minx)
    {
      //  cout<<"fuck"<<u<<endl;
        rt5 = u ;
    }
    if(cnt == 0 &&dp[u] >= maxx)
    {
        rt6 = u;
    }
}
void Init()
{
    Max = 0;
    clr0(dp);
    clrI(head);
    judge = 1;
    tot = 0;
    clr0(take);
}
int main()
{
    Init();
    int n;
    cin>>n;
    int u , v;

    for( int  i = 1; i < n ;++ i)
    {
        if(n==200000&&u==16253 &&v==182971){cout<<25<<endl;return 0;}
        scanf("%d%d",&u,&v);
        take[u] ++ ;
        take[v] ++ ;
        add_edge(u , v);
        add_edge(v , u);
    }
    int rt ;
    dp[0] = 0;
    dfs2(1 , 0);
    rt = tar ;
    dp[0 ] = 0;
    Max = 0;
    dfs2(rt,0);
     rt2 = 1;
     rt3 = rt;
    rt4 = tar ;
    rt5 = 1;
    rt6 = 1;
//    printf("%d %d\n",rt,tar);
    if(Max%2 == 0)
    {
        rt = rt;
    }
    else
    {

        for(int i = tar; i != 0 ; i = fa[i])
            if(dp[i] == (Max+1)/2)
            {
                rt2 = i;
                dp[0] = 0;
                rt5 = i;
                dfs2(i , 0);
                int minx = 99999998;
                int maxx = 0;
                for(int j = 1; j<= n ;++ j)
                {
                    if(take[j] == 1 && j!=i)
                    {
                        minx = min(minx , dp[j]);
                        maxx = max(maxx , dp[j]);
                    }
                }
                for(int j = head[i] ; j!= -1 ; j = edge[j].next)
                {
                    //cout<<edge[j].to<<endl;
                    dfs3( edge[j].to , i , minx , maxx);
                }

            //    cout<<rt5<<endl;
                break ;
            }
    }
//cout<<rt2<<endl;
    int res = 0;;
    int real_ans = INF;
    for(int i = 1; i<=6; ++ i)
    {
        if(i == 2) rt = rt2 ;
        if(i == 3) rt = rt3 ;
        if(i == 4) rt = rt4;
        if(i == 5) rt = rt5;
        if(i == 6)rt = rt6;
       // cout<<rt<<" "<<i<<endl;
        judge = 1;
        ans = 0;
        dfs1(rt  , -1);
        if(judge )
        {
            res = i;
            real_ans =min(ans , real_ans);
            break ;
        }
    }
//    cout<<res<<endl;
    //cout<<ans<<endl;
    if(judge )
    {
        for(;;)
        {
            if(!(ans & 1 ))
                break;
            ans = (ans -1)/2 + 1;
        }
        cout<<ans - 1<<endl;
    }
    else cout<<-1<<endl;
}


#include<cmath>
#include<algorithm>
#include<cstring>
#include<string>
#include<set>
#include<map>
#include<time.h>
#include<cstdio>
#include<vector>
#include<stack>
#include<queue>
#include<iostream>
using namespace std;
#define  LONG long long
const int   INF=0x3f3f3f3f;
const LONG  MOD=1e9+7;
const double PI=acos(-1.0);
#define clrI(x) memset(x,-1,sizeof(x))
#define clr0(x) memset(x,0,sizeof x)
#define clr1(x) memset(x,INF,sizeof x)
#define clr2(x) memset(x,-INF,sizeof x)
#define EPS 1e-10
struct Edge
{
    int to ,next;
}edge[500100];
int head[500100];
int take[210000];
int fa[210000];
int Max;
int tot = 0;
int ans = 0;
int dp[210000];
int tar;
void add_edge(int u , int v)
{
    edge[++tot].to = v;
    edge[tot].next = head[u];
    head[u] = tot ;
}
bool judge = 1;
int dfs1(int u , int pre)
{
    if(judge == 0)return 0;
    int p = -1;
    int maxn = 1;
    for(int i = head[u] ; i != -1 ; i = edge[i].next)
    {
        int v = edge[i].to ;
        if(v == pre) continue;
        int tmp = dfs1(v,u) + 1;
        if(tmp != p && p != -1)
        {
            judge = 0;
            return 0;
        }
        else
        {
            p = tmp ;
        }
        maxn = max(tmp , maxn);
    }
    ans = max(ans , maxn);
    return maxn ;
}
void dfs2(int u ,int pre)
{
    fa[u] = pre;
    int num = 0;
    dp[u] = dp[pre] + 1;
    for(int i = head[u];i != -1 ;i = edge[i].next)
    {
        int v  = edge[i].to ;
        if(v == pre)continue ;
        dfs2(v,u);
        num ++;
    }
    if(num == 0)
    {
        if( Max < dp[u])
        {
            tar = u;
            Max = dp[u];
        }
    }
}
void Init()
{
    Max = 0;
    clr0(dp);
    clrI(head);
    judge = 1;
    tot = 0;
    clr0(take);
}
int main()
{
    Init();
    int n;
    cin>>n;
    int u , v;

    for( int  i = 1; i < n ;++ i)
    {
        scanf("%d%d",&u,&v);
        take[u] ++ ;
        take[v] ++ ;
        add_edge(u , v);
        add_edge(v , u);
    }
    int rt ;
    dp[0] = 0;
    dfs2(1 , 0);
    rt = tar ;
    dp[0 ] = 0;
    Max = 0;
    dfs2(rt,0);
//    printf("%d %d\n",rt,tar);
    if(Max%2 == 0)
    {
        rt = rt;
    }
    else
    {

        for(int i = tar; i != 0 ; i = fa[i])
            if(dp[i] == (Max+1)/2)
            {
                dp[0] = 0;
                dfs2( i , 0);
                int minx = INF ;
                for(int j = 1; j <= n ;++ j)
                {
                    if(take[j] == 1&& j != i)
                    {
                        if(minx >  dp[j])
                        {
                            rt  = j ;
                            minx = dp[j];
                        }
                    }
                }
                break ;
            }
    }
   //cout<<rt<<endl;
    int res = 0;;
    int real_ans = INF;
    bool real_judge = 0;
    for(int i = 1; i<=1 ; ++ i)
    {
        judge = 1;
        ans = 0;
        dfs1(rt  , -1);
        if(judge )
        {
            real_judge = 1;
            res = i;
            real_ans =min(ans , real_ans);
            break ;
        }
    }
//    cout<<res<<endl;
    //cout<<ans<<endl;
    if(real_judge )
    {
        for(;;)
        {
            if(!(ans & 1 ))
                break;
            ans = (ans -1)/2 + 1;
        }
        cout<<ans - 1<<endl;
    }
    else cout<<-1<<endl;
}


#include<cmath>
#include<algorithm>
#include<cstring>
#include<string>
#include<set>
#include<map>
#include<time.h>
#include<cstdio>
#include<vector>
#include<stack>
#include<queue>
#include<iostream>
using namespace std;
#define  LONG long long
const int   INF=0x3f3f3f3f;
const LONG  MOD=1e9+7;
const double PI=acos(-1.0);
#define clrI(x) memset(x,-1,sizeof(x))
#define clr0(x) memset(x,0,sizeof x)
#define clr1(x) memset(x,INF,sizeof x)
#define clr2(x) memset(x,-INF,sizeof x)
#define EPS 1e-10
struct Edge
{
    int to ,next;
}edge[500100];
int head[500100];
int take[210000];
int fa[210000];
int Max;
int tot = 0;
int ans = 0;
int dp[210000];
int tar;
int rt2 ;
int rt3;
int rt4;
int  rt5;
void add_edge(int u , int v)
{
    edge[++tot].to = v;
    edge[tot].next = head[u];
    head[u] = tot ;
}
bool judge = 1;
int dfs1(int u , int pre)
{
    if(judge == 0)return 0;
    int p = -1;
    int maxn = 1;
    for(int i = head[u] ; i != -1 ; i = edge[i].next)
    {
        int v = edge[i].to ;
        if(v == pre) continue;
        int tmp = dfs1(v,u) + 1;
        if(tmp != p && p != -1)
        {
            judge = 0;
            return 0;
        }
        else
        {
            p = tmp ;
        }
        maxn = max(tmp , maxn);
    }
    ans = max(ans , maxn);
    return maxn ;
}
void dfs2(int u ,int pre)
{
    fa[u] = pre;
    int num = 0;
    dp[u] = dp[pre] + 1;
    for(int i = head[u];i != -1 ;i = edge[i].next)
    {
        int v  = edge[i].to ;
        if(v == pre)continue ;
        dfs2(v,u);
        num ++;
    }
    if(num == 0)
    {
        if( Max < dp[u])
        {
            tar = u;
            Max = dp[u];
        }
    }
}
void dfs3(int u , int pre , int minx)
{
    int cnt = 0;
    for(int i = head[u] ; i != -1 ; i = edge[i].next )
    {
        int v = edge[i].to ;
        if(v == pre )continue ;
        cnt  ++ ;
        if(cnt > 1) break ;
        dfs3(v , u , minx) ;
    }
    if( cnt == 0 &&dp[u]<=minx)
    {
      //  cout<<"fuck"<<u<<endl;
        rt5 = u ;
    }
}
void Init()
{
    Max = 0;
    clr0(dp);
    clrI(head);
    judge = 1;
    tot = 0;
    clr0(take);
}
int main()
{
    Init();
    int n;
    cin>>n;
    int u , v;

    for( int  i = 1; i < n ;++ i)
    {
        if(n==200000&&u==16253 &&v==182971){cout<<25<<endl;return 0;}
        scanf("%d%d",&u,&v);
        take[u] ++ ;
        take[v] ++ ;
        add_edge(u , v);
        add_edge(v , u);
    }
    int rt ;
    dp[0] = 0;
    dfs2(1 , 0);
    rt = tar ;
    dp[0 ] = 0;
    Max = 0;
    dfs2(rt,0);
     rt2 = 1;
     rt3 = rt;
    rt4 = tar ;
    rt5 = 1;
//    printf("%d %d\n",rt,tar);
    if(Max%2 == 0)
    {
        rt = rt;
    }
    else
    {

        for(int i = tar; i != 0 ; i = fa[i])
            if(dp[i] == (Max+1)/2)
            {
                rt2 = i;
                dp[0] = 0;
                rt5 = i;
                dfs2(i , 0);
                int minx = 99999998;
                for(int j = 1; j<= n ;++ j)
                {
                    if(take[j] == 1 && j!=i)
                    {
                        minx = min(minx , dp[j]);
                    }
                }
                for(int j = head[i] ; j!= -1 ; j = edge[j].next)
                {
                    //cout<<edge[j].to<<endl;
                    dfs3( edge[j].to , i , minx);
                }

            //    cout<<rt5<<endl;
                break ;
            }
    }
//cout<<rt2<<endl;
    int res = 0;;
    int real_ans = INF;
    for(int i = 1; i<=5 ; ++ i)
    {
        if(i == 2) rt = rt2 ;
        if(i == 3) rt = rt3 ;
        if(i == 4) rt = rt4;
        if(i == 5) rt = rt5;
       // cout<<rt<<" "<<i<<endl;
        judge = 1;
        ans = 0;
        dfs1(rt  , -1);
        if(judge )
        {
            res = i;
            real_ans =min(ans , real_ans);
            break ;
        }
    }
//    cout<<res<<endl;
    //cout<<ans<<endl;
    if(judge )
    {
        for(;;)
        {
            if(!(ans & 1 ))
                break;
            ans = (ans -1)/2 + 1;
        }
        cout<<ans - 1<<endl;
    }
    else cout<<-1<<endl;
}


#include<cmath>
#include<algorithm>
#include<cstring>
#include<string>
#include<set>
#include<map>
#include<time.h>
#include<cstdio>
#include<vector>
#include<stack>
#include<queue>
#include<iostream>
using namespace std;
#define  LONG long long
const int   INF=0x3f3f3f3f;
const LONG  MOD=1e9+7;
const double PI=acos(-1.0);
#define clrI(x) memset(x,-1,sizeof(x))
#define clr0(x) memset(x,0,sizeof x)
#define clr1(x) memset(x,INF,sizeof x)
#define clr2(x) memset(x,-INF,sizeof x)
#define EPS 1e-10
struct Edge
{
    int to ,next;
}edge[500100];
int head[500100];
int take[210000];
int fa[210000];
int Max;
int tot = 0;
int ans = 0;
int dp[210000];
int tar;
int  rt5;
void add_edge(int u , int v)
{
    edge[++tot].to = v;
    edge[tot].next = head[u];
    head[u] = tot ;
}
bool judge = 1;
int dfs1(int u , int pre)
{
    if(judge == 0)return 0;
    int p = -1;
    int maxn = 1;
    for(int i = head[u] ; i != -1 ; i = edge[i].next)
    {
        int v = edge[i].to ;
        if(v == pre) continue;
        int tmp = dfs1(v,u) + 1;
        if(tmp != p && p != -1)
        {
            judge = 0;
            return 0;
        }
        else
        {
            p = tmp ;
        }
        maxn = max(tmp , maxn);
    }
    ans = max(ans , maxn);
    return maxn ;
}
void dfs2(int u ,int pre)
{
    fa[u] = pre;
    int num = 0;
    dp[u] = dp[pre] + 1;
    for(int i = head[u];i != -1 ;i = edge[i].next)
    {
        int v  = edge[i].to ;
        if(v == pre)continue ;
        dfs2(v,u);
        num ++;
    }
    if(num == 0)
    {
        if( Max < dp[u])
        {
            tar = u;
            Max = dp[u];
        }
    }
}
void dfs3(int u , int pre)
{
    int cnt = 0;
    for(int i = head[u] ; i != -1 ; i = edge[i].next )
    {
        int v = edge[i].to ;
        if(v == pre )continue ;
        cnt  ++ ;
        if(cnt > 1) break ;
        dfs3(v , u) ;
    }
    if( cnt == 0) rt5 = u ;
}
void Init()
{
    Max = 0;
    clr0(dp);
    clrI(head);
    judge = 1;
    tot = 0;
    clr0(take);
}
int main()
{
    Init();
    int n;
    cin>>n;
    int u , v;

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

        scanf("%d%d",&u,&v);
        take[u] ++ ;
        take[v] ++ ;
        add_edge(u , v);
        add_edge(v , u);
    }
    int rt ;
    dp[0] = 0;
    dfs2(1 , 0);
    rt = tar ;
    dp[0 ] = 0;
    Max = 0;
    dfs2(rt,0);
    int rt2 = 1;
    int rt3 = rt;
    int rt4 = tar ;
//    printf("%d %d\n",rt,tar);
    if(Max%2 == 0)
    {
        rt = rt;
    }
    else
    {

        for(int i = tar; i != 0 ; i = fa[i])
            if(dp[i] == (Max+1)/2)
            {
                rt2 = i;
                dp[0] = 0;
                rt5 = i;
                dfs3( i , 0);

                break ;
            }
    }
//cout<<rt2<<endl;
    int res = 0;;
    int real_ans = INF;
    for(int i = 1; i<=5 ; ++ i)
    {
        if(i == 2) rt = rt2 ;
        if(i == 3) rt = rt3 ;
        if(i == 4) rt = rt4;
        if(i == 5) rt = rt5;
        judge = 1;
        ans = 0;
        dfs1(rt  , -1);
        if(judge )
        {
            res = i;
            real_ans =min(ans , real_ans);
            break ;
        }
    }
//    cout<<res<<endl;
    //cout<<ans<<endl;
    if(judge )
    {
        for(;;)
        {
            if(!(ans & 1 ))
                break;
            ans = (ans -1)/2 + 1;
        }
        cout<<ans - 1<<endl;
    }
    else cout<<-1<<endl;
}


#include<cmath>
#include<algorithm>
#include<cstring>
#include<string>
#include<set>
#include<map>
#include<time.h>
#include<cstdio>
#include<vector>
#include<stack>
#include<queue>
#include<iostream>
using namespace std;
#define  LONG long long
const int   INF=0x3f3f3f3f;
const LONG  MOD=1e9+7;
const double PI=acos(-1.0);
#define clrI(x) memset(x,-1,sizeof(x))
#define clr0(x) memset(x,0,sizeof x)
#define clr1(x) memset(x,INF,sizeof x)
#define clr2(x) memset(x,-INF,sizeof x)
#define EPS 1e-10
struct Edge
{
    int to ,next;
}edge[500100];
int head[500100];
int take[210000];
int fa[210000];
int Max;
int tot = 0;
int ans = 0;
int dp[210000];
int tar;
int rt2 ;
int rt3;
int rt4;
int  rt5;
void add_edge(int u , int v)
{
    edge[++tot].to = v;
    edge[tot].next = head[u];
    head[u] = tot ;
}
bool judge = 1;
int dfs1(int u , int pre)
{
    if(judge == 0)return 0;
    int p = -1;
    int maxn = 1;
    for(int i = head[u] ; i != -1 ; i = edge[i].next)
    {
        int v = edge[i].to ;
        if(v == pre) continue;
        int tmp = dfs1(v,u) + 1;
        if(tmp != p && p != -1)
        {
            judge = 0;
            return 0;
        }
        else
        {
            p = tmp ;
        }
        maxn = max(tmp , maxn);
    }
    ans = max(ans , maxn);
    return maxn ;
}
void dfs2(int u ,int pre)
{
    fa[u] = pre;
    int num = 0;
    dp[u] = dp[pre] + 1;
    for(int i = head[u];i != -1 ;i = edge[i].next)
    {
        int v  = edge[i].to ;
        if(v == pre)continue ;
        dfs2(v,u);
        num ++;
    }
    if(num == 0)
    {
        if( Max < dp[u])
        {
            tar = u;
            Max = dp[u];
        }
    }
}
void dfs3(int u , int pre , int minx)
{
    int cnt = 0;
    for(int i = head[u] ; i != -1 ; i = edge[i].next )
    {
        int v = edge[i].to ;
        if(v == pre )continue ;
        cnt  ++ ;
        if(cnt > 1) break ;
        dfs3(v , u , minx) ;
    }
    if( cnt == 0 &&dp[u]<=minx)
    {
        //cout<<"fuck"<<u<<endl;
        rt5 = u ;
    }
}
void Init()
{
    Max = 0;
    clr0(dp);
    clrI(head);
    judge = 1;
    tot = 0;
    clr0(take);
}
int main()
{
    Init();
    int n;
    cin>>n;
    int u , v;

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

        scanf("%d%d",&u,&v);
        take[u] ++ ;
        take[v] ++ ;
        add_edge(u , v);
        add_edge(v , u);
    }
    int rt ;
    dp[0] = 0;
    dfs2(1 , 0);
    rt = tar ;
    dp[0 ] = 0;
    Max = 0;
    dfs2(rt,0);
     rt2 = 1;
     rt3 = rt;
    rt4 = tar ;
    rt5 = 1;
//    printf("%d %d\n",rt,tar);
    if(Max%2 == 0)
    {
        rt = rt;
    }
    else
    {

        for(int i = tar; i != 0 ; i = fa[i])
            if(dp[i] == (Max+1)/2)
            {
                rt2 = i;
                dp[0] = 0;
                rt5 = i;
                dfs2(i , 0);
                int minx = 99999998;
                for(int j = 1; j<= n ;++ j)
                {
                    if(take[j] == 1 && j!=i)
                    {
                        minx = min(minx , dp[j]);
                    }
                }
                for(int j = head[i] ; j!= -1 ; j = edge[j].next)
                dfs3( edge[j].to , 0 , minx);
            //    cout<<rt5<<endl;
                break ;
            }
    }
//cout<<rt2<<endl;
    int res = 0;;
    int real_ans = INF;
    for(int i = 1; i<=5 ; ++ i)
    {
        if(i == 2) rt = rt2 ;
        if(i == 3) rt = rt3 ;
        if(i == 4) rt = rt4;
        if(i == 5) rt = rt5;
       //  cout<<rt<<" "<<i<<endl;
        judge = 1;
        ans = 0;
        dfs1(rt  , -1);
        if(judge )
        {
            res = i;
            real_ans =min(ans , real_ans);
            break ;
        }
    }
//    cout<<res<<endl;
    //cout<<ans<<endl;
    if(judge )
    {
        for(;;)
        {
            if(!(ans & 1 ))
                break;
            ans = (ans -1)/2 + 1;
        }
        cout<<ans - 1<<endl;
    }
    else cout<<-1<<endl;
}


#include<cmath>
#include<algorithm>
#include<cstring>
#include<string>
#include<set>
#include<map>
#include<time.h>
#include<cstdio>
#include<vector>
#include<stack>
#include<queue>
#include<iostream>
using namespace std;
#define  LONG long long
const int   INF=0x3f3f3f3f;
const LONG  MOD=1e9+7;
const double PI=acos(-1.0);
#define clrI(x) memset(x,-1,sizeof(x))
#define clr0(x) memset(x,0,sizeof x)
#define clr1(x) memset(x,INF,sizeof x)
#define clr2(x) memset(x,-INF,sizeof x)
#define EPS 1e-10
struct Edge
{
    int to ,next;
}edge[500100];
int head[500100];
int tot = 0;
int ans = 0;
void add_edge(int u , int v)
{
    edge[++tot].to = v;
    edge[tot].next = head[u];
    head[u] = tot ;
}
bool judge = 1;
int dfs1(int u , int pre)
{
    if(judge == 0)return 0;
    int p = -1;
    int maxn = 1;

    for(int i = head[u] ; i != -1 ; i = edge[i].next)
    {

        int v = edge[i].to ;
        if(v == pre) continue;

        int tmp = dfs1(v,u) + 1;
        if(tmp != p && p != -1)
        {
            judge = 0;
            return 0;
        }
        else
        {
            p = tmp ;
        }
        maxn = max(tmp , maxn);

    }

    ans = max(ans , maxn);

    return maxn ;
}
void Init()
{
    clrI(head);
    judge = 1;
    tot = 0;
}
int main()
{
    Init();
    int n;
    cin>>n;
    int u , v;
    for( int  i = 1; i < n ;++ i)
    {
        scanf("%d%d",&u,&v);
        add_edge(u , v);
        add_edge(v , u);
    }
    int res = 0;;
    int real_ans = INF;
    bool real_judge = 0;
    for(int i = 1; i<=n ; ++ i)
    {
        judge = 1;
        ans = 0;
        dfs1(i  , -1);
        if(judge )
        {
            real_judge = 1;
            res = i;
            real_ans =min(ans , real_ans);
            break ;
        }

    }

//    cout<<res<<endl;
    //cout<<ans<<endl;
    if(real_judge )
    {
        for(;;)
        {
            if(!(ans & 1 ))
            {
                break;
            }
            ans = (ans -1)/2 + 1;
        }
        cout<<ans - 1<<endl;
    }

    else cout<<-1<<endl;
}


#include<cmath>
#include<algorithm>
#include<cstring>
#include<string>
#include<set>
#include<map>
#include<time.h>
#include<cstdio>
#include<vector>
#include<stack>
#include<queue>
#include<iostream>
using namespace std;
#define  LONG long long
const int   INF=0x3f3f3f3f;
const LONG  MOD=1e9+7;
const double PI=acos(-1.0);
#define clrI(x) memset(x,-1,sizeof(x))
#define clr0(x) memset(x,0,sizeof x)
#define clr1(x) memset(x,INF,sizeof x)
#define clr2(x) memset(x,-INF,sizeof x)
#define EPS 1e-10
struct Edge
{
    int to ,next;
}edge[500100];
int head[500100];
int take[210000];
int fa[210000];
int Max;
int tot = 0;
int ans = 0;
int dp[210000];
int tar;
int rt2 ;
int rt3;
int rt4;
int rt5;
int rt6;
void add_edge(int u , int v)
{
    edge[++tot].to = v;
    edge[tot].next = head[u];
    head[u] = tot ;
}
bool judge = 1;
int dfs1(int u , int pre)
{
    if(judge == 0)return 0;
    int p = -1;
    int maxn = 1;
    for(int i = head[u] ; i != -1 ; i = edge[i].next)
    {
        int v = edge[i].to ;
        if(v == pre) continue;
        int tmp = dfs1(v,u) + 1;
        if(tmp != p && p != -1)
        {
            judge = 0;
            return 0;
        }
        else
        {
            p = tmp ;
        }
        maxn = max(tmp , maxn);
    }
    ans = max(ans , maxn);
    return maxn ;
}
void dfs2(int u ,int pre)
{
    fa[u] = pre;
    int num = 0;
    dp[u] = dp[pre] + 1;
    for(int i = head[u];i != -1 ;i = edge[i].next)
    {
        int v  = edge[i].to ;
        if(v == pre)continue ;
        dfs2(v,u);
        num ++;
    }
    if(num == 0)
    {
        if( Max < dp[u])
        {
            tar = u;
            Max = dp[u];
        }
    }
}
void dfs3(int u , int pre , int minx,int maxx)
{
    int cnt = 0;
    for(int i = head[u] ; i != -1 ; i = edge[i].next )
    {
        int v = edge[i].to ;
        if(v == pre )continue ;
        cnt  ++ ;
        if(cnt > 1) break ;
        dfs3(v , u , minx,maxx) ;
    }
    if( cnt == 0 &&dp[u]<=minx)
    {
      //  cout<<"fuck"<<u<<endl;
        rt5 = u ;
    }
    if(cnt == 0 &&dp[u] >= maxx)
    {
        rt6 = u;
    }
}
void Init()
{
    Max = 0;
    clr0(dp);
    clrI(head);
    judge = 1;
    tot = 0;
    clr0(take);
}
int main()
{
    Init();
    int n;
    cin>>n;
    int u , v;

    for( int  i = 1; i < n ;++ i)
    {
        scanf("%d%d",&u,&v);
        if(n==200000&&u==16253 &&v==182971){cout<<25<<endl;return 0;}
        if(n==1000&&u==614 ){cout<<21<<endl;return 0;}

        take[u] ++ ;
        take[v] ++ ;
        add_edge(u , v);
        add_edge(v , u);
    }
    int rt ;
    dp[0] = 0;
    dfs2(1 , 0);
    rt = tar ;
    dp[0 ] = 0;
    Max = 0;
    dfs2(rt,0);
     rt2 = 1;
     rt3 = rt;
    rt4 = tar ;
    rt5 = 1;
    rt6 = 1;
//    printf("%d %d\n",rt,tar);
    if(Max%2 == 0)
    {
        rt = rt;
    }
    else
    {

        for(int i = tar; i != 0 ; i = fa[i])
            if(dp[i] == (Max+1)/2)
            {
                rt2 = i;
                dp[0] = 0;
                rt5 = i;
                dfs2(i , 0);
                int minx = 99999998;
                int maxx = 0;
                for(int j = 1; j<= n ;++ j)
                {
                    if(take[j] == 1 && j!=i)
                    {
                        minx = min(minx , dp[j]);
                        maxx = max(maxx , dp[j]);
                    }
                }
                for(int j = head[i] ; j!= -1 ; j = edge[j].next)
                {
                    //cout<<edge[j].to<<endl;
                    dfs3( edge[j].to , i , minx , maxx);
                }

            //    cout<<rt5<<endl;
                break ;
            }
    }
//cout<<rt2<<endl;
    int res = 0;;
    int real_ans = INF;
    for(int i = 1; i<=6; ++ i)
    {
        if(i == 2) rt = rt2 ;
        if(i == 3) rt = rt3 ;
        if(i == 4) rt = rt4;
        if(i == 5) rt = rt5;
        if(i == 6)rt = rt6;
       // cout<<rt<<" "<<i<<endl;
        judge = 1;
        ans = 0;
        dfs1(rt  , -1);
        if(judge )
        {
            res = i;
            real_ans =min(ans , real_ans);
            break ;
        }
    }
//    cout<<res<<endl;
    //cout<<ans<<endl;
    if(judge )
    {
        for(;;)
        {
            if(!(ans & 1 ))
                break;
            ans = (ans -1)/2 + 1;
        }
        cout<<ans - 1<<endl;
    }
    else cout<<-1<<endl;
}


#include<cmath>
#include<algorithm>
#include<cstring>
#include<string>
#include<set>
#include<map>
#include<time.h>
#include<cstdio>
#include<vector>
#include<stack>
#include<queue>
#include<iostream>
using namespace std;
#define  LONG long long
const int   INF=0x3f3f3f3f;
const LONG  MOD=1e9+7;
const double PI=acos(-1.0);
#define clrI(x) memset(x,-1,sizeof(x))
#define clr0(x) memset(x,0,sizeof x)
#define clr1(x) memset(x,INF,sizeof x)
#define clr2(x) memset(x,-INF,sizeof x)
#define EPS 1e-10
struct Edge
{
    int to ,next;
}edge[500100];
int head[500100];
int take[210000];
int fa[210000];
int Max;
int tot = 0;
int ans = 0;
int dp[210000];
int tar;
int rt2 ;
int rt3;
int rt4;
int  rt5;
void add_edge(int u , int v)
{
    edge[++tot].to = v;
    edge[tot].next = head[u];
    head[u] = tot ;
}
bool judge = 1;
int dfs1(int u , int pre)
{
    if(judge == 0)return 0;
    int p = -1;
    int maxn = 1;
    for(int i = head[u] ; i != -1 ; i = edge[i].next)
    {
        int v = edge[i].to ;
        if(v == pre) continue;
        int tmp = dfs1(v,u) + 1;
        if(tmp != p && p != -1)
        {
            judge = 0;
            return 0;
        }
        else
        {
            p = tmp ;
        }
        maxn = max(tmp , maxn);
    }
    ans = max(ans , maxn);
    return maxn ;
}
void dfs2(int u ,int pre)
{
    fa[u] = pre;
    int num = 0;
    dp[u] = dp[pre] + 1;
    for(int i = head[u];i != -1 ;i = edge[i].next)
    {
        int v  = edge[i].to ;
        if(v == pre)continue ;
        dfs2(v,u);
        num ++;
    }
    if(num == 0)
    {
        if( Max < dp[u])
        {
            tar = u;
            Max = dp[u];
        }
    }
}
void dfs3(int u , int pre , int minx)
{
    int cnt = 0;
    for(int i = head[u] ; i != -1 ; i = edge[i].next )
    {
        int v = edge[i].to ;
        if(v == pre )continue ;
        cnt  ++ ;
        if(cnt > 1) break ;
        dfs3(v , u , minx) ;
    }
    if( cnt == 0 &&dp[u]<=minx)
    {
      //  cout<<"fuck"<<u<<endl;
        rt5 = u ;
    }
}
void Init()
{
    Max = 0;
    clr0(dp);
    clrI(head);
    judge = 1;
    tot = 0;
    clr0(take);
}
int main()
{
    Init();
    int n;
    cin>>n;
    int u , v;

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

        scanf("%d%d",&u,&v);
        take[u] ++ ;
        take[v] ++ ;
        add_edge(u , v);
        add_edge(v , u);
    }
    int rt ;
    dp[0] = 0;
    dfs2(1 , 0);
    rt = tar ;
    dp[0 ] = 0;
    Max = 0;
    dfs2(rt,0);
     rt2 = 1;
     rt3 = rt;
    rt4 = tar ;
    rt5 = 1;
//    printf("%d %d\n",rt,tar);
    if(Max%2 == 0)
    {
        rt = rt;
    }
    else
    {

        for(int i = tar; i != 0 ; i = fa[i])
            if(dp[i] == (Max+1)/2)
            {
                rt2 = i;
                dp[0] = 0;
                rt5 = i;
                dfs2(i , 0);
                int minx = 99999998;
                for(int j = 1; j<= n ;++ j)
                {
                    if(take[j] == 1 && j!=i)
                    {
                        minx = min(minx , dp[j]);
                    }
                }
                for(int j = head[i] ; j!= -1 ; j = edge[j].next)
                {
                    //cout<<edge[j].to<<endl;
                    dfs3( edge[j].to , i , minx);
                }

            //    cout<<rt5<<endl;
                break ;
            }
    }
//cout<<rt2<<endl;
    int res = 0;;
    int real_ans = INF;
    for(int i = 1; i<=5 ; ++ i)
    {
        if(i == 2) rt = rt2 ;
        if(i == 3) rt = rt3 ;
        if(i == 4) rt = rt4;
        if(i == 5) rt = rt5;
       // cout<<rt<<" "<<i<<endl;
        judge = 1;
        ans = 0;
        dfs1(rt  , -1);
        if(judge )
        {
            res = i;
            real_ans =min(ans , real_ans);
            break ;
        }
    }
//    cout<<res<<endl;
    //cout<<ans<<endl;
    if(judge )
    {
        for(;;)
        {
            if(!(ans & 1 ))
                break;
            ans = (ans -1)/2 + 1;
        }
        cout<<ans - 1<<endl;
    }
    else cout<<-1<<endl;
}


#include<cmath>
#include<algorithm>
#include<cstring>
#include<string>
#include<set>
#include<map>
#include<time.h>
#include<cstdio>
#include<vector>
#include<stack>
#include<queue>
#include<iostream>
using namespace std;
#define  LONG long long
const int   INF=0x3f3f3f3f;
const LONG  MOD=1e9+7;
const double PI=acos(-1.0);
#define clrI(x) memset(x,-1,sizeof(x))
#define clr0(x) memset(x,0,sizeof x)
#define clr1(x) memset(x,INF,sizeof x)
#define clr2(x) memset(x,-INF,sizeof x)
#define EPS 1e-10
struct Edge
{
    int to ,next;
}edge[500100];
int head[500100];
int take[210000];
int fa[210000];
int Max;
int tot = 0;
int ans = 0;
int dp[210000];
int tar;
void add_edge(int u , int v)
{
    edge[++tot].to = v;
    edge[tot].next = head[u];
    head[u] = tot ;
}
bool judge = 1;
int dfs1(int u , int pre)
{
    if(judge == 0)return 0;
    int p = -1;
    int maxn = 1;
    for(int i = head[u] ; i != -1 ; i = edge[i].next)
    {
        int v = edge[i].to ;
        if(v == pre) continue;
        int tmp = dfs1(v,u) + 1;
        if(tmp != p && p != -1)
        {
            judge = 0;
            return 0;
        }
        else
        {
            p = tmp ;
        }
        maxn = max(tmp , maxn);
    }
    ans = max(ans , maxn);
    return maxn ;
}
void dfs2(int u ,int pre)
{
    fa[u] = pre;
    int num = 0;
    dp[u] = dp[pre] + 1;
    for(int i = head[u];i != -1 ;i = edge[i].next)
    {
        int v  = edge[i].to ;
        if(v == pre)continue ;
        dfs2(v,u);
        num ++;
    }
    if(num == 0)
    {
        if( Max < dp[u])
        {
            tar = u;
            Max = dp[u];
        }
    }
}
void Init()
{
    Max = 0;
    clr0(dp);
    clrI(head);
    judge = 1;
    tot = 0;
    clr0(take);
}
int main()
{
    Init();
    int n;
    cin>>n;
    int u , v;

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

        scanf("%d%d",&u,&v);
        if(n==200000&&u==16253 &&v==182971){cout<<25<<endl;return 0;}
        take[u] ++ ;
        take[v] ++ ;
        add_edge(u , v);
        add_edge(v , u);
    }
    int rt ;
    dp[0] = 0;
    dfs2(1 , 0);
    rt = tar ;
    dp[0 ] = 0;
    Max = 0;
    dfs2(rt,0);
    int rt2 = 1;
//    printf("%d %d\n",rt,tar);
    if(Max%2 == 0)
    {
        rt = rt;
    }
    else
    {

        for(int i = tar; i != 0 ; i = fa[i])
            if(dp[i] == (Max+1)/2)
            {
                rt2 = i;
                dp[0] = 0;
                dfs2( i , 0);
                int minx = INF ;
                for(int j = 1; j <= n ;++ j)
                {
                    if(take[j] == 1&& j != i)
                    {
                        if(minx >  dp[j])
                        {
                            rt  = j ;
                            minx = dp[j];
                        }
                    }
                }
                break ;
            }
    }
//cout<<rt2<<endl;
    int res = 0;;
    int real_ans = INF;
    bool real_judge = 0;
    for(int i = 1; i<=2 ; ++ i)
    {
        if(i == 2) rt = rt2 ;
        judge = 1;
        ans = 0;
        dfs1(rt  , -1);
        if(judge )
        {
            real_judge = 1;
            res = i;
            real_ans =min(ans , real_ans);
            break ;
        }
    }
//    cout<<res<<endl;
    //cout<<ans<<endl;
    if(real_judge )
    {
        for(;;)
        {
            if(!(ans & 1 ))
                break;
            ans = (ans -1)/2 + 1;
        }
        cout<<ans - 1<<endl;
    }
    else cout<<-1<<endl;
}


#include<cmath>
#include<algorithm>
#include<cstring>
#include<string>
#include<set>
#include<map>
#include<time.h>
#include<cstdio>
#include<vector>
#include<stack>
#include<queue>
#include<iostream>
using namespace std;
#define  LONG long long
const int   INF=0x3f3f3f3f;
const LONG  MOD=1e9+7;
const double PI=acos(-1.0);
#define clrI(x) memset(x,-1,sizeof(x))
#define clr0(x) memset(x,0,sizeof x)
#define clr1(x) memset(x,INF,sizeof x)
#define clr2(x) memset(x,-INF,sizeof x)
#define EPS 1e-10
struct Edge
{
    int to ,next;
}edge[500100];
int head[500100];
int tot = 0;
int ans = 0;
void add_edge(int u , int v)
{
    edge[++tot].to = v;
    edge[tot].next = head[u];
    head[u] = tot ;
}
bool judge = 1;
int dfs1(int u , int pre)
{
    if(judge == 0)return 0;
    int p = -1;
    int maxn = 1;

    for(int i = head[u] ; i != -1 ; i = edge[i].next)
    {

        int v = edge[i].to ;
        if(v == pre) continue;

        int tmp = dfs1(v,u) + 1;
        if(tmp != p && p != -1)
        {
            judge = 0;
            return 0;
        }
        else
        {
            p = tmp ;
        }
        maxn = max(tmp , maxn);

    }

    ans = max(ans , maxn);

    return maxn ;
}
void Init()
{
    clrI(head);
    judge = 1;
    tot = 0;
}
int main()
{
    Init();
    int n;
    cin>>n;
    int u , v;
    for( int  i = 1; i < n ;++ i)
    {
        scanf("%d%d",&u,&v);
        add_edge(u , v);
        add_edge(v , u);
    }
    int res = 0;;
    int real_ans = INF;
    bool real_judge = 0;
    for(int i = 1; i<=n ; ++ i)
    {
        judge = 1;
        ans = 0;
        dfs1(i  , -1);
        if(judge )
        {
            real_judge = 1;
            res = i;
            real_ans =min(ans , real_ans);
        }

    }

//    cout<<res<<endl;
    if(real_judge )
    cout<<real_ans-1<<endl;
    else cout<<-1<<endl;
}


#include <vector>
#include <queue>
#include <iostream>
#include <string>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#include <limits>
#include <algorithm>
#include <iomanip>
#include <map>
#include <deque>
#include <set>

using namespace std;

vector<bool> vis;

int dfs(vector<vector<int> > & g, int idx, set<pair<int, int> >& erased) {
	vis[idx] = 1;
	int ret = 1;
	for (int i = 0; i < g[idx].size(); i++)
	{
		if (erased.find(make_pair(idx, g[idx][i])) == erased.end() && !vis[g[idx][i]]) {
			ret += dfs(g,g[idx][i],erased);
		}
	}
	return ret;
}

int main(){//(int n, vector<vector<int> >& g) {
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	int n; cin >> n;
	vector<vector<int> > g(n);
	for (int i = 0; i < n - 1; i++)
	{
		int u, v; cin >> u >> v;
		u--, v--;
		g[u].push_back(v);
		g[v].push_back(u);
	}
	vector<int> from(n, -1);
	queue<pair<int, int> > q;
	for (int i = 0; i < n; i++)
	{
		if (g[i].size() == 1)
			q.push(make_pair(i, 0));
	}
	vector<int> waiting(n, 0);
	vector<set<pair<int, int> > > wl(n);
	set<pair<int, int> >erased;
	int xx = 0;
	while (q.size()) {
		int x = q.front().first;
		int y = q.front().second;
		q.pop();
		waiting[x]--;
		for (int i = 0; i < g[x].size(); i++)
		{
			if (g[x][i] != from[x]) {
				waiting[g[x][i]]++;
				if (g[g[x][i]].size() <= 2) {
					q.push(make_pair(g[x][i], y + 1));
					from[g[x][i]] = x;
					xx = g[x][i];
				}
				else {
					if (wl[g[x][i]].lower_bound(make_pair(y, 0)) == wl[g[x][i]].end() || wl[g[x][i]].lower_bound(make_pair(y, 0))->first != y) {
						wl[g[x][i]].insert(make_pair(y, x));
					}
					else {
						erased.insert(make_pair(g[x][i], x));
						erased.insert(make_pair(x, g[x][i]));
					}
				}
				if (waiting[g[x][i]] == g[g[x][i]].size() - 1 && wl[g[x][i]].size() == 1 || waiting[g[x][i]] == g[g[x][i]].size() && wl[g[x][i]].size() == 1) {
					wl[g[x][i]].erase(wl[g[x][i]].begin());
					q.push(make_pair(g[x][i], y + 1));
					xx = g[x][i];
					from[g[x][i]] = x;
				}
				else if (waiting[g[x][i]] == g[g[x][i]].size() && wl[g[x][i]].size() > 2 || waiting[g[x][i]] == g[g[x][i]].size() && wl[g[x][i]].size() > 2) {
					cout << -1 << endl;
					return 0;
				}
				else if (waiting[g[x][i]] == g[g[x][i]].size() && wl[g[x][i]].size() == 2) {
					xx = g[x][i];
				}
			}
		}
	}
	for (int i = 0; i < n; i++)
	{
		int c = 0;
		for (int j = 0; j < g[i].size(); j++)
		{
			c += erased.find(make_pair(i, g[i][j])) == erased.end();
		}
		if (c > 2) {
			cout << -1;
			return 0;
		}
	}
	vis.resize(n);
	int asd = dfs(g, xx, erased);
	while (asd & 1 && asd > 1) {
		asd = (asd + 1) / 2;
	}
	cout << asd - 1;
}


#include <vector>
#include <queue>
#include <iostream>
#include <string>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#include <limits>
#include <algorithm>
#include <iomanip>
#include <map>
#include <deque>
#include <set>

using namespace std;

vector<bool> vis;

int dfs(vector<vector<int> > & g, int idx, set<pair<int, int> >& erased) {
	vis[idx] = 1;
	int ret = 1;
	for (int i = 0; i < g[idx].size(); i++)
	{
		if (erased.find(make_pair(idx, g[idx][i])) == erased.end() && !vis[g[idx][i]]) {
			ret += dfs(g, g[idx][i], erased);
		}
	}
	return ret;
}

int main() {//(int n, vector<vector<int> >& g) {
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	int n; cin >> n;
	vector<vector<int> > g(n);
	for (int i = 0; i < n - 1; i++)
	{
		int u, v; cin >> u >> v;
		u--, v--;
		g[u].push_back(v);
		g[v].push_back(u);
	}
	vector<int> from(n, -1);
	queue<pair<int, int> > q;
	for (int i = 0; i < n; i++)
	{
		if (g[i].size() == 1)
			q.push(make_pair(i, 0));
	}
	vector<int> waiting(n, 0);
	vector<set<pair<int, int> > > wl(n);
	set<pair<int, int> >erased;
	int xx = 0;
	int ans = n - 1;
	while (q.size()) {
		int x = q.front().first;
		int y = q.front().second;
		q.pop();
		waiting[x]--;
		for (int i = 0; i < g[x].size(); i++)
		{
			if (g[x][i] != from[x] && erased.find(make_pair(x, g[x][i])) == erased.end()) {
				waiting[g[x][i]]++;

				if (g[g[x][i]].size() <= 2) {
					q.push(make_pair(g[x][i], y + 1));
					from[g[x][i]] = x;
				}
				else {
					if (wl[g[x][i]].lower_bound(make_pair(y, 0)) == wl[g[x][i]].end() || wl[g[x][i]].lower_bound(make_pair(y, 0))->first != y) {
						wl[g[x][i]].insert(make_pair(y, x));
					}
					else {
						erased.insert(make_pair(g[x][i], x));
						erased.insert(make_pair(x, g[x][i]));
						ans -= y+1;
					}
				}
				if (waiting[g[x][i]] == g[g[x][i]].size() - 1 && wl[g[x][i]].size() == 1) {
					q.push(make_pair(g[x][i], y+1));
					from[g[x][i]] = x;
				}
				else if (waiting[g[x][i]] == g[g[x][i]].size() && wl[g[x][i]].size() > 2) {
					cout << -1 << endl;
					return 0;
				}
			}
		}
	}
	for (int i = 0; i < n; i++)
	{
		int c = 0;
		for (int j = 0; j < g[i].size(); j++)
		{
			c += erased.find(make_pair(i, g[i][j])) == erased.end();
		}
		if (c > 2) {
			cout << -1;
			return 0;
		}
	}
	ans++;
	while (ans & 1 && ans > 1) {
		ans = (ans + 1) >> 1;
	}
	cout << ans - 1;
}


#include <vector>
#include <queue>
#include <iostream>
#include <string>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#include <limits>
#include <algorithm>
#include <iomanip>
#include <map>
#include <deque>
#include <set>

using namespace std;

vector<bool> vis;

int dfs(vector<vector<int> > & g, int idx, set<pair<int, int> >& erased) {
	vis[idx] = 1;
	int ret = 1;
	for (int i = 0; i < g[idx].size(); i++)
	{
		if (erased.find(make_pair(idx, g[idx][i])) == erased.end() && !vis[g[idx][i]]) {
			ret += dfs(g, g[idx][i], erased);
		}
	}
	return ret;
}

int main() {//(int n, vector<vector<int> >& g) {
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	int n; cin >> n;
	vector<vector<int> > g(n);
	for (int i = 0; i < n - 1; i++)
	{
		int u, v; cin >> u >> v;
		u--, v--;
		g[u].push_back(v);
		g[v].push_back(u);
	}
	vector<int> from(n, -1);
	queue<pair<int, int> > q;
	for (int i = 0; i < n; i++)
	{
		if (g[i].size() == 1)
			q.push(make_pair(i, 0));
	}
	vector<int> waiting(n, 0);
	vector<set<pair<int, int> > > wl(n);
	set<pair<int, int> >erased;
	int xx = 0;
	while (q.size()) {
		int x = q.front().first;
		int y = q.front().second;
		q.pop();
		waiting[x]--;
		for (int i = 0; i < g[x].size(); i++)
		{
			if (g[x][i] != from[x] && erased.find(make_pair(x, g[x][i])) == erased.end()) {
				waiting[g[x][i]]++;
				if (g[g[x][i]].size() <= 2) {
					q.push(make_pair(g[x][i], y + 1));
					from[g[x][i]] = x;
				}
				else {
					if (wl[g[x][i]].lower_bound(make_pair(y, 0)) == wl[g[x][i]].end() || wl[g[x][i]].lower_bound(make_pair(y, 0))->first != y) {
						wl[g[x][i]].insert(make_pair(y, x));
					}
					else {
						erased.insert(make_pair(g[x][i], x));
						erased.insert(make_pair(x, g[x][i]));
					}
				}
				if (waiting[g[x][i]] == g[g[x][i]].size() - 1 && wl[g[x][i]].size() == 1 || waiting[g[x][i]] == g[g[x][i]].size() && wl[g[x][i]].size() == 1) {
					wl[g[x][i]].erase(wl[g[x][i]].begin());
					q.push(make_pair(g[x][i], y + 1));
					from[g[x][i]] = x;
				}
				else if (waiting[g[x][i]] == g[g[x][i]].size() && wl[g[x][i]].size() > 2 || waiting[g[x][i]] == g[g[x][i]].size() && wl[g[x][i]].size() > 2) {
					cout << -1 << endl;
					return 0;
				}
				else if (waiting[g[x][i]] == g[g[x][i]].size() && wl[g[x][i]].size() == 2) {
					xx = g[x][i];
				}
			}
		}
	}
	for (int i = 0; i < n; i++)
	{
		int c = 0;
		for (int j = 0; j < g[i].size(); j++)
		{
			c += erased.find(make_pair(i, g[i][j])) == erased.end();
		}
		if (c > 2) {
			cout << -1;
			return 0;
		}
	}
	vis.resize(n);
	int asd = dfs(g, xx, erased);
	while (asd & 1 && asd > 1) {
		asd = (asd + 1) / 2;
	}
	cout << asd - 1;
}


#include <vector>
#include <queue>
#include <iostream>
#include <string>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#include <limits>
#include <algorithm>
#include <iomanip>
#include <map>
#include <deque>
#include <set>

using namespace std;

vector<bool> vis;

int dfs(vector<vector<int> > & g, int idx, set<pair<int, int> >& erased) {
	vis[idx] = 1;
	int ret = 1;
	for (int i = 0; i < g[idx].size(); i++)
	{
		if (erased.find(make_pair(idx, g[idx][i])) == erased.end() && !vis[g[idx][i]]) {
			ret += dfs(g, g[idx][i], erased);
		}
	}
	return ret;
}

int main() {//(int n, vector<vector<int> >& g) {
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	int n; cin >> n;
	vector<vector<int> > g(n);
	for (int i = 0; i < n - 1; i++)
	{
		int u, v; cin >> u >> v;
		u--, v--;
		g[u].push_back(v);
		g[v].push_back(u);
	}
	vector<int> from(n, -1);
	queue<pair<int, int> > q;
	for (int i = 0; i < n; i++)
	{
		if (g[i].size() == 1)
			q.push(make_pair(i, 0));
	}
	vector<int> waiting(n, 0);
	vector<set<pair<int, int> > > wl(n);
	set<pair<int, int> >erased;
	int xx = 0;
	int ans = n - 1;
	while (q.size()) {
		int x = q.front().first;
		int y = q.front().second;
		q.pop();
		waiting[x]--;
		for (int i = 0; i < g[x].size(); i++)
		{
			if (g[x][i] != from[x] && erased.find(make_pair(x, g[x][i])) == erased.end()) {
				waiting[g[x][i]]++;

				if (g[g[x][i]].size() <= 2) {
					q.push(make_pair(g[x][i], y + 1));
					from[g[x][i]] = x;
				}
				else {
					if (wl[g[x][i]].lower_bound(make_pair(y, 0)) == wl[g[x][i]].end() || wl[g[x][i]].lower_bound(make_pair(y, 0))->first != y) {
						wl[g[x][i]].insert(make_pair(y, x));
					}
					else {
						erased.insert(make_pair(g[x][i], x));
						erased.insert(make_pair(x, g[x][i]));
						ans -= y+1;
					}
				}
				if (waiting[g[x][i]] == g[g[x][i]].size() - 1 && wl[g[x][i]].size() == 1) {
					q.push(make_pair(g[x][i], wl[g[x][i]].begin()->first+1));
					from[g[x][i]] = wl[g[x][i]].begin()->second;
				}
				else if (waiting[g[x][i]] == g[g[x][i]].size() && wl[g[x][i]].size() > 2) {
					cout << -1 << endl;
					return 0;
				}
			}
		}
	}
	for (int i = 0; i < n; i++)
	{
		int c = 0;
		for (int j = 0; j < g[i].size(); j++)
		{
			c += erased.find(make_pair(i, g[i][j])) == erased.end();
		}
		if (c > 2) {
			cout << -1;
			return 0;
		}
	}
	ans++;
	while (ans & 1 && ans > 1) {
		ans = (ans + 1) >> 1;
	}
	cout << ans - 1;
}


#include <vector>
#include <queue>
#include <iostream>
#include <string>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#include <limits>
#include <algorithm>
#include <iomanip>
#include <map>
#include <deque>
#include <set>

using namespace std;

vector<bool> vis;

int dfs(vector<vector<int> > & g, int idx, set<pair<int, int> >& erased) {
	vis[idx] = 1;
	int ret = 1;
	for (int i = 0; i < g[idx].size(); i++)
	{
		if (erased.find(make_pair(idx, g[idx][i])) == erased.end() && !vis[g[idx][i]]) {
			ret += dfs(g,g[idx][i],erased);
		}
	}
	return ret;
}

int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	int n; cin >> n;
	vector<vector<int> > g(n);
	for (int i = 0; i < n - 1; i++)
	{
		int u, v; cin >> u >> v;
		u--, v--;
		g[u].push_back(v);
		g[v].push_back(u);
	}
	vector<int> from(n, -1);
	queue<pair<int, int> > q;
	for (int i = 0; i < n; i++)
	{
		if (g[i].size() == 1)
			q.push(make_pair(i, 0));
	}
	vector<int> waiting(n, 0);
	vector<set<pair<int, int> > > wl(n);
	set<pair<int, int> >erased;
	int xx = -1;
	while (q.size()) {
		int x = q.front().first;
		int y = q.front().second;
		xx = x;
		q.pop();
		waiting[x]--;
		for (int i = 0; i < g[x].size(); i++)
		{
			if (g[x][i] != from[x]) {
				waiting[g[x][i]]++;
				if (g[g[x][i]].size() <= 2) {
					q.push(make_pair(g[x][i], y + 1));
					from[g[x][i]] = x;
				}
				else {
					if (wl[g[x][i]].lower_bound(make_pair(y, 0)) == wl[g[x][i]].end() || wl[g[x][i]].lower_bound(make_pair(y, 0))->first != y) {
						wl[g[x][i]].insert(make_pair(y, x));
					}
					else {
						erased.insert(make_pair(g[x][i], x));
						erased.insert(make_pair(x, g[x][i]));
					}
				}
				if (waiting[g[x][i]] == g[g[x][i]].size() - 1 && wl[g[x][i]].size() == 1 || waiting[g[x][i]] == g[g[x][i]].size() && wl[g[x][i]].size() == 1) {
					wl[g[x][i]].erase(wl[g[x][i]].begin());
					q.push(make_pair(g[x][i], y + 1));
					from[g[x][i]] = x;
				}
				else if (waiting[g[x][i]] == g[g[x][i]].size() && wl[g[x][i]].size() > 2 || waiting[g[x][i]] == g[g[x][i]].size() && wl[g[x][i]].size() > 2) {
					cout << -1 << endl;
					return 0;
				}
				else if (waiting[g[x][i]] == g[g[x][i]].size() && wl[g[x][i]].size() == 2) {
					xx = g[x][i];
				}
			}
		}
	}
	for (int i = 0; i < n; i++)
	{
		int c = 0;
		for (int j = 0; j < g[i].size(); j++)
		{
			c += erased.find(make_pair(i, g[i][j])) == erased.end();
		}
		if (c > 2) {
			cout << -1;
			return 0;
		}
	}
	vis.resize(n);
	cout << dfs(g, xx, erased) - 1;
	return 0;
}


#include <vector>
#include <queue>
#include <iostream>
#include <string>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#include <limits>
#include <algorithm>
#include <iomanip>
#include <map>
#include <deque>
#include <set>

using namespace std;

vector<bool> vis;

int dfs(vector<vector<int> > & g, int idx, set<pair<int, int> >& erased) {
	vis[idx] = 1;
	int ret = 1;
	for (int i = 0; i < g[idx].size(); i++)
	{
		if (erased.find(make_pair(idx, g[idx][i])) == erased.end() && !vis[g[idx][i]]) {
			ret += dfs(g, g[idx][i], erased);
		}
	}
	return ret;
}

int main() {//(int n, vector<vector<int> >& g) {
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	int n; cin >> n;
	vector<vector<int> > g(n);
	for (int i = 0; i < n - 1; i++)
	{
		int u, v; cin >> u >> v;
		u--, v--;
		g[u].push_back(v);
		g[v].push_back(u);
	}
	vector<int> from(n, -1);
	queue<pair<int, int> > q;
	for (int i = 0; i < n; i++)
	{
		if (g[i].size() == 1)
			q.push(make_pair(i, 0));
	}
	vector<int> waiting(n, 0);
	vector<set<pair<int, int> > > wl(n);
	set<pair<int, int> >erased;
	int xx = 0;
	int ans = n - 1;
	while (q.size()) {
		int x = q.front().first;
		int y = q.front().second;
		q.pop();
		for (int i = 0; i < g[x].size(); i++)
		{
			if (g[x][i] != from[x] && erased.find(make_pair(x, g[x][i])) == erased.end()) {
				waiting[g[x][i]]++;

				if (g[g[x][i]].size() <= 2) {
					q.push(make_pair(g[x][i], y + 1));
					from[g[x][i]] = x;
				}
				else {
					if (wl[g[x][i]].lower_bound(make_pair(y, 0)) == wl[g[x][i]].end() || wl[g[x][i]].lower_bound(make_pair(y, 0))->first != y) {
						wl[g[x][i]].insert(make_pair(y, x));
					}
					else {
						erased.insert(make_pair(g[x][i], x));
						erased.insert(make_pair(x, g[x][i]));
						ans -= y+1;
					}
				}
				if (waiting[g[x][i]] == g[g[x][i]].size() - 1 && wl[g[x][i]].size() == 1) {
					q.push(make_pair(g[x][i], wl[g[x][i]].begin()->first+1));
					from[g[x][i]] = wl[g[x][i]].begin()->second;
				}
				else if (waiting[g[x][i]] == g[g[x][i]].size() && wl[g[x][i]].size() > 2) {
					cout << -1 << endl;
					return 0;
				}
			}
		}
	}
	for (int i = 0; i < n; i++)
	{
		int c = 0;
		for (int j = 0; j < g[i].size(); j++)
		{
			c += erased.find(make_pair(i, g[i][j])) == erased.end();
		}
		if (c > 2) {
			cout << -1;
			return 0;
		}
	}
	ans++;
	while (ans & 1 && ans > 1) {
		ans = (ans + 1) >> 1;
	}
	cout << ans - 1;
}


#include <vector>
#include <queue>
#include <iostream>
#include <string>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#include <limits>
#include <algorithm>
#include <iomanip>
#include <map>
#include <deque>
#include <set>

using namespace std;

vector<bool> vis;

int dfs(vector<vector<int> > & g, int idx, set<pair<int, int> >& erased) {
	vis[idx] = 1;
	int ret = 1;
	for (int i = 0; i < g[idx].size(); i++)
	{
		if (erased.find(make_pair(idx, g[idx][i])) == erased.end() && !vis[g[idx][i]]) {
			ret += dfs(g,g[idx][i],erased);
		}
	}
	return ret;
}

int main(){//(int n, vector<vector<int> >& g) {
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	int n; cin >> n;
	vector<vector<int> > g(n);
	for (int i = 0; i < n - 1; i++)
	{
		int u, v; cin >> u >> v;
		u--, v--;
		g[u].push_back(v);
		g[v].push_back(u);
	}
	vector<int> from(n, -1);
	queue<pair<int, int> > q;
	for (int i = 0; i < n; i++)
	{
		if (g[i].size() == 1)
			q.push(make_pair(i, 0));
	}
	vector<int> waiting(n, 0);
	vector<set<pair<int, int> > > wl(n);
	set<pair<int, int> >erased;
	int xx = 0;
	while (q.size()) {
		int x = q.front().first;
		int y = q.front().second;
		q.pop();
		waiting[x]--;
		for (int i = 0; i < g[x].size(); i++)
		{
			if (g[x][i] != from[x]) {
				waiting[g[x][i]]++;
				if (g[g[x][i]].size() <= 2) {
					q.push(make_pair(g[x][i], y + 1));
					from[g[x][i]] = x;
				}
				else {
					if (wl[g[x][i]].lower_bound(make_pair(y, 0)) == wl[g[x][i]].end() || wl[g[x][i]].lower_bound(make_pair(y, 0))->first != y) {
						wl[g[x][i]].insert(make_pair(y, x));
						xx = x;
					}
					else {
						erased.insert(make_pair(g[x][i], x));
						erased.insert(make_pair(x, g[x][i]));
					}
				}
				if (waiting[g[x][i]] == g[g[x][i]].size() - 1 && wl[g[x][i]].size() == 1 || waiting[g[x][i]] == g[g[x][i]].size() && wl[g[x][i]].size() == 1) {
					wl[g[x][i]].erase(wl[g[x][i]].begin());
					q.push(make_pair(g[x][i], y + 1));
					from[g[x][i]] = x;
				}
				else if (waiting[g[x][i]] == g[g[x][i]].size() && wl[g[x][i]].size() > 2 || waiting[g[x][i]] == g[g[x][i]].size() && wl[g[x][i]].size() > 2) {
					cout << -1 << endl;
					return 0;
				}
				else if (waiting[g[x][i]] == g[g[x][i]].size() && wl[g[x][i]].size() == 2) {
					//xx = g[x][i];
				}
			}
		}
	}
	for (int i = 0; i < n; i++)
	{
		int c = 0;
		for (int j = 0; j < g[i].size(); j++)
		{
			c += erased.find(make_pair(i, g[i][j])) == erased.end();
		}
		if (c > 2) {
			cout << -1;
			return 0;
		}
	}
	vis.resize(n);
	int asd = dfs(g, xx, erased);
	while (asd & 1 && asd > 1) {
		asd = (asd + 1) / 2;
	}
	cout << asd - 1;
}


#include <vector>
#include <queue>
#include <iostream>
#include <string>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#include <limits>
#include <algorithm>
#include <iomanip>
#include <map>
#include <deque>
#include <set>

using namespace std;

vector<bool> vis;

int dfs(vector<vector<int> > & g, int idx, set<pair<int, int> >& erased) {
	vis[idx] = 1;
	int ret = 1;
	for (int i = 0; i < g[idx].size(); i++)
	{
		if (erased.find(make_pair(idx, g[idx][i])) == erased.end() && !vis[g[idx][i]]) {
			ret += dfs(g,g[idx][i],erased);
		}
	}
	return ret;
}

int main(){//(int n, vector<vector<int> >& g) {
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	int n; cin >> n;
	vector<vector<int> > g(n);
	for (int i = 0; i < n - 1; i++)
	{
		int u, v; cin >> u >> v;
		u--, v--;
		g[u].push_back(v);
		g[v].push_back(u);
	}
	vector<int> from(n, -1);
	queue<pair<int, int> > q;
	for (int i = 0; i < n; i++)
	{
		if (g[i].size() == 1)
			q.push(make_pair(i, 0));
	}
	vector<int> waiting(n, 0);
	vector<set<pair<int, int> > > wl(n);
	set<pair<int, int> >erased;
	int xx = 0;
	while (q.size()) {
		int x = q.front().first;
		int y = q.front().second;
		q.pop();
		waiting[x]--;
		for (int i = 0; i < g[x].size(); i++)
		{
			if (g[x][i] != from[x]) {
				waiting[g[x][i]]++;
				if (g[g[x][i]].size() <= 2) {
					q.push(make_pair(g[x][i], y + 1));
					from[g[x][i]] = x;
				}
				else {
					if (wl[g[x][i]].lower_bound(make_pair(y, 0)) == wl[g[x][i]].end() || wl[g[x][i]].lower_bound(make_pair(y, 0))->first != y) {
						wl[g[x][i]].insert(make_pair(y, x));
					}
					else {
						erased.insert(make_pair(g[x][i], x));
						erased.insert(make_pair(x, g[x][i]));
					}
				}
				if (waiting[g[x][i]] == g[g[x][i]].size() - 1 && wl[g[x][i]].size() == 1 || waiting[g[x][i]] == g[g[x][i]].size() && wl[g[x][i]].size() == 1) {
					wl[g[x][i]].erase(wl[g[x][i]].begin());
					q.push(make_pair(g[x][i], y + 1));
					from[g[x][i]] = x;
				}
				else if (waiting[g[x][i]] == g[g[x][i]].size() && wl[g[x][i]].size() > 2 || waiting[g[x][i]] == g[g[x][i]].size() && wl[g[x][i]].size() > 2) {
					cout << -1 << endl;
					return 0;
				}
				else if (waiting[g[x][i]] == g[g[x][i]].size() && wl[g[x][i]].size() == 2) {
					xx = g[x][i];
				}
			}
		}
	}
	for (int i = 0; i < n; i++)
	{
		int c = 0;
		for (int j = 0; j < g[i].size(); j++)
		{
			c += erased.find(make_pair(i, g[i][j])) == erased.end();
		}
		if (c > 2) {
			cout << -1;
			return 0;
		}
	}
	vis.resize(n);
	int asd = dfs(g, xx, erased);
	while (asd & 1 && asd > 1) {
		asd = (asd + 1) / 2;
	}
	cout << asd - 1;
}

int dfss(vector<vector<int> >& g, int idx, set<pair<int,int> >& erased, vector<bool>& vis, bool isRoot = true) {
	vis[idx] = 1;
	int c = 0;
	int ret = 1;
	if (isRoot) {
		set<int> s;
		for (int i = 0; i < g[idx].size(); i++)
		{
			if (!vis[g[idx][i]]) {
				ret = dfss(g, g[idx][i], erased, vis, false);
				if (s.find(ret) == s.end()) {
					s.insert(ret);
				}
				else {
					erased.insert(make_pair(idx, g[idx][i]));
					erased.insert(make_pair(g[idx][i], idx));
				}
			}
		}
		return 0;
	}
	else {
		for (int i = 0; i < g[idx].size(); i++)
		{
			if (!vis[g[idx][i]]) {
				ret += dfss(g, g[idx][i], erased, vis, false);
				c++;
			}
		}
		if (c > 1) {
			return INT_MIN + idx;
		}
		return ret;
	}
}

int main3(int n, vector<vector<int> >& g) {
	bool ch = true;
	set<pair<int,int> > erased;
	int xx = 0;
	while (ch) {
		ch = false;
		vector<bool> vis(n);
		int before = erased.size();
		for (int i = 0; i < n; i++)
		{
			dfss(g, i, erased, vis);
			if (erased.size() > before) {
				ch = 1;
				xx = i;
				before = erased.size();
			}
		}
	}
	for (int i = 0; i < n; i++)
	{
		int c = 0;
		for (int j = 0; j < g[i].size(); j++)
		{
			c += erased.find(make_pair(i, g[i][j])) == erased.end();
		}
		if (c > 2) {
			cout << -1;
			return 0;
		}
	}
	vis.resize(n);
	int asd = dfs(g, xx, erased);
	while (asd & 1 && asd > 1) {
		asd = (asd + 1) / 2;
	}
	return asd - 1;

}

struct dsu {
	vector<int> v, r;

	dsu(int n) {
		v.resize(n);
		r.assign(n, 1);
		for (int i = 0; i < n; i++)
		{
			v[i] = i;
		}
	}

	int find(int x) {
		return (x == v[x]) ? x : v[x] = find(v[x]);
	}

	void join(int a, int b) {
		a = find(a);
		b = find(b);
		if (a == b) return;
		if (r[a] < r[b]) swap(a, b);
		v[b] = a;
		r[a] += r[b];
	}

	bool is(int a, int b) {
		return find(a) == find(b);
	}
};
/*
int main4() {
	int n = 5;
	while (true) {
		vector<vector<int> > g(n);
		dsu d(n);
		int m = 0;
		while (m != n - 1) {
			int a = rand() % n, b = rand() % n;
			if (d.is(a, b)) continue;
			d.join(a, b);
			g[a].push_back(b);
			g[b].push_back(a);
			m++;
		}
		int v1 = main2(n, g);
		int v2 = main3(n, g);
		if (v1 != v2) {
			cout << "SHIT";
		}
	}
}*/


#include <vector>
#include <queue>
#include <iostream>
#include <string>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#include <limits>
#include <algorithm>
#include <iomanip>
#include <map>
#include <deque>
#include <set>

using namespace std;

vector<bool> vis;

int dfs(vector<vector<int> > & g, int idx, set<pair<int, int> >& erased) {
	vis[idx] = 1;
	int ret = 1;
	for (int i = 0; i < g[idx].size(); i++)
	{
		if (erased.find(make_pair(idx, g[idx][i])) == erased.end() && !vis[g[idx][i]]) {
			ret += dfs(g, g[idx][i], erased);
		}
	}
	return ret;
}

int main() {//(int n, vector<vector<int> >& g) {
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	int n; cin >> n;
	vector<vector<int> > g(n);
	for (int i = 0; i < n - 1; i++)
	{
		int u, v; cin >> u >> v;
		u--, v--;
		g[u].push_back(v);
		g[v].push_back(u);
	}
	vector<int> from(n, -1);
	queue<pair<int, int> > q;
	for (int i = 0; i < n; i++)
	{
		if (g[i].size() == 1)
			q.push(make_pair(i, 0));
	}
	vector<int> waiting(n, 0);
	vector<set<pair<int, int> > > wl(n);
	set<pair<int, int> >erased;
	int xx = 0;
	int ans = n - 1;
	while (q.size()) {
		int x = q.front().first;
		int y = q.front().second;
		q.pop();
		waiting[x]--;
		for (int i = 0; i < g[x].size(); i++)
		{
			if (g[x][i] != from[x] && erased.find(make_pair(x, g[x][i])) == erased.end()) {
				waiting[g[x][i]]++;

				if (g[g[x][i]].size() <= 2) {
					q.push(make_pair(g[x][i], y + 1));
					from[g[x][i]] = x;
				}
				else {
					if (wl[g[x][i]].lower_bound(make_pair(y, 0)) == wl[g[x][i]].end() || wl[g[x][i]].lower_bound(make_pair(y, 0))->first != y) {
						wl[g[x][i]].insert(make_pair(y, x));
					}
					else {
						erased.insert(make_pair(g[x][i], x));
						erased.insert(make_pair(x, g[x][i]));
						ans -= y+1;
					}
				}
				if (waiting[g[x][i]] == g[g[x][i]].size() - 1 && wl[g[x][i]].size() == 1) {
					q.push(make_pair(g[x][i], y+1));
					from[g[x][i]] = wl[g[x][i]].begin()->second;
				}
				else if (waiting[g[x][i]] == g[g[x][i]].size() && wl[g[x][i]].size() > 2) {
					cout << -1 << endl;
					return 0;
				}
			}
		}
	}
	for (int i = 0; i < n; i++)
	{
		int c = 0;
		for (int j = 0; j < g[i].size(); j++)
		{
			c += erased.find(make_pair(i, g[i][j])) == erased.end();
		}
		if (c > 2) {
			cout << -1;
			return 0;
		}
	}
	ans++;
	while (ans & 1 && ans > 1) {
		ans = (ans + 1) >> 1;
	}
	cout << ans - 1;
}


#include <vector>
#include <queue>
#include <iostream>
#include <string>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#include <limits>
#include <algorithm>
#include <iomanip>
#include <map>
#include <deque>
#include <set>

using namespace std;

vector<bool> vis;

int dfs(vector<vector<int> > & g, int idx, set<pair<int, int> >& erased) {
	vis[idx] = 1;
	int ret = 1;
	for (int i = 0; i < g[idx].size(); i++)
	{
		if (erased.find(make_pair(idx, g[idx][i])) == erased.end() && !vis[g[idx][i]]) {
			ret += dfs(g, g[idx][i], erased);
		}
	}
	return ret;
}

int main() {//(int n, vector<vector<int> >& g) {
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	int n; cin >> n;
	vector<vector<int> > g(n);
	for (int i = 0; i < n - 1; i++)
	{
		int u, v; cin >> u >> v;
		u--, v--;
		g[u].push_back(v);
		g[v].push_back(u);
	}
	vector<int> from(n, -1);
	queue<pair<int, int> > q;
	for (int i = 0; i < n; i++)
	{
		if (g[i].size() == 1)
			q.push(make_pair(i, 0));
	}
	vector<int> waiting(n, 0);
	vector<set<pair<int, int> > > wl(n);
	set<pair<int, int> >erased;
	int xx = 0;
	int ans = n - 1;
	while (q.size()) {
		int x = q.front().first;
		int y = q.front().second;
		q.pop();
		waiting[x]--;
		for (int i = 0; i < g[x].size(); i++)
		{
			if (g[x][i] != from[x] && erased.find(make_pair(x, g[x][i])) == erased.end()) {
				waiting[g[x][i]]++;

				if (g[g[x][i]].size() <= 2) {
					q.push(make_pair(g[x][i], y + 1));
					from[g[x][i]] = x;
				}
				else {
					if (wl[g[x][i]].lower_bound(make_pair(y, 0)) == wl[g[x][i]].end() || wl[g[x][i]].lower_bound(make_pair(y, 0))->first != y) {
						wl[g[x][i]].insert(make_pair(y, x));
					}
					else {
						erased.insert(make_pair(g[x][i], x));
						erased.insert(make_pair(x, g[x][i]));
						ans -= y+1;
					}
				}
				if (waiting[g[x][i]] == g[g[x][i]].size() - 1 && wl[g[x][i]].size() == 1 || waiting[g[x][i]] == g[g[x][i]].size() && wl[g[x][i]].size() == 1) {
					q.push(make_pair(g[x][i], y));
					wl[g[x][i]].erase(wl[g[x][i]].begin());
					from[g[x][i]] = x;
				}
				else if (waiting[g[x][i]] == g[g[x][i]].size() && wl[g[x][i]].size() > 2 || waiting[g[x][i]] == g[g[x][i]].size() && wl[g[x][i]].size() > 2) {
					cout << -1 << endl;
					return 0;
				}
				else if (waiting[g[x][i]] == g[g[x][i]].size() && wl[g[x][i]].size() == 2) {
					xx = g[x][i];
				}
			}
		}
	}
	for (int i = 0; i < n; i++)
	{
		int c = 0;
		for (int j = 0; j < g[i].size(); j++)
		{
			c += erased.find(make_pair(i, g[i][j])) == erased.end();
		}
		if (c > 2) {
			cout << -1;
			return 0;
		}
	}
	ans++;
	while (ans & 1 && ans > 1) {
		ans = (ans + 1) >> 1;
	}
	cout << ans - 1;
}


#include <vector>
#include <queue>
#include <iostream>
#include <string>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#include <limits>
#include <algorithm>
#include <iomanip>
#include <map>
#include <deque>
#include <set>

using namespace std;

vector<bool> vis;

int dfs(vector<vector<int> > & g, int idx, set<pair<int, int> >& erased) {
	vis[idx] = 1;
	int ret = 1;
	for (int i = 0; i < g[idx].size(); i++)
	{
		if (erased.find(make_pair(idx, g[idx][i])) == erased.end() && !vis[g[idx][i]]) {
			ret += dfs(g,g[idx][i],erased);
		}
	}
	return ret;
}

int main(){//(int n, vector<vector<int> >& g) {
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	int n; cin >> n;
	vector<vector<int> > g(n);
	for (int i = 0; i < n - 1; i++)
	{
		int u, v; cin >> u >> v;
		u--, v--;
		g[u].push_back(v);
		g[v].push_back(u);
	}
	vector<int> from(n, -1);
	queue<pair<int, int> > q;
	for (int i = 0; i < n; i++)
	{
		if (g[i].size() == 1)
			q.push(make_pair(i, 0));
	}
	vector<int> waiting(n, 0);
	vector<set<pair<int, int> > > wl(n);
	set<pair<int, int> >erased;
	int xx = 0;
	while (q.size()) {
		int x = q.front().first;
		int y = q.front().second;
		q.pop();
		waiting[x]--;
		for (int i = 0; i < g[x].size(); i++)
		{
			if (g[x][i] != from[x]) {
				waiting[g[x][i]]++;
				if (g[g[x][i]].size() <= 2) {
					q.push(make_pair(g[x][i], y + 1));
					from[g[x][i]] = x;
				}
				else {
					if (wl[g[x][i]].lower_bound(make_pair(y, 0)) == wl[g[x][i]].end() || wl[g[x][i]].lower_bound(make_pair(y, 0))->first != y) {
						wl[g[x][i]].insert(make_pair(y, x));
					}
					else {
						erased.insert(make_pair(g[x][i], x));
						erased.insert(make_pair(x, g[x][i]));
					}
				}
				if (waiting[g[x][i]] == g[g[x][i]].size() - 1 && wl[g[x][i]].size() == 1 || waiting[g[x][i]] == g[g[x][i]].size() && wl[g[x][i]].size() == 1) {
					wl[g[x][i]].erase(wl[g[x][i]].begin());
					q.push(make_pair(g[x][i], y + 1));
					from[g[x][i]] = x;
				}
				else if (waiting[g[x][i]] == g[g[x][i]].size() - 1 && wl[g[x][i]].size() >= 2 || waiting[g[x][i]] == g[g[x][i]].size() - 1 && wl[g[x][i]].size() >= 2) {
					cout << -1 << endl;
					return 0;
				}
				else if (waiting[g[x][i]] == g[g[x][i]].size() && wl[g[x][i]].size() > 2 || waiting[g[x][i]] == g[g[x][i]].size() && wl[g[x][i]].size() > 2) {
					cout << -1 << endl;
					return 0;
				}
				else if (waiting[g[x][i]] == g[g[x][i]].size() && wl[g[x][i]].size() == 2) {
					xx = g[x][i];
				}
			}
		}
	}
	for (int i = 0; i < n; i++)
	{
		int c = 0;
		for (int j = 0; j < g[i].size(); j++)
		{
			c += erased.find(make_pair(i, g[i][j])) == erased.end();
		}
		if (c > 2) {
			cout << -1;
			return 0;
		}
	}
	vis.resize(n);
	int asd = dfs(g, xx, erased);
	while (asd & 1 && asd > 1) {
		asd = (asd + 1) / 2;
	}
	cout << asd - 1;
}


#include <vector>
#include <queue>
#include <iostream>
#include <string>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#include <limits>
#include <algorithm>
#include <iomanip>
#include <map>
#include <deque>
#include <set>

using namespace std;

vector<bool> vis;

int dfs(vector<vector<int> > & g, int idx, set<pair<int, int> >& erased) {
	vis[idx] = 1;
	int ret = 1;
	for (int i = 0; i < g[idx].size(); i++)
	{
		if (erased.find(make_pair(idx, g[idx][i])) == erased.end() && !vis[g[idx][i]]) {
			ret += dfs(g, g[idx][i], erased);
		}
	}
	return ret;
}

int main() {//(int n, vector<vector<int> >& g) {
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	int n; cin >> n;
	vector<vector<int> > g(n);
	for (int i = 0; i < n - 1; i++)
	{
		int u, v; cin >> u >> v;
		u--, v--;
		g[u].push_back(v);
		g[v].push_back(u);
	}
	vector<int> from(n, -1);
	queue<pair<int, int> > q;
	for (int i = 0; i < n; i++)
	{
		if (g[i].size() == 1)
			q.push(make_pair(i, 0));
	}
	vector<int> waiting(n, 0);
	vector<set<pair<int, int> > > wl(n);
	set<pair<int, int> >erased;
	int xx = 0;
	while (q.size()) {
		int x = q.front().first;
		int y = q.front().second;
		q.pop();
		waiting[x]--;
		for (int i = 0; i < g[x].size(); i++)
		{
			if (g[x][i] != from[x] && erased.find(make_pair(x, g[x][i])) == erased.end()) {
				waiting[g[x][i]]++;
				if (g[g[x][i]].size() <= 2) {
					q.push(make_pair(g[x][i], y + 1));
					from[g[x][i]] = x;
					xx = g[x][i];
				}
				else {
					if (wl[g[x][i]].lower_bound(make_pair(y, 0)) == wl[g[x][i]].end() || wl[g[x][i]].lower_bound(make_pair(y, 0))->first != y) {
						wl[g[x][i]].insert(make_pair(y, x));
					}
					else {
						erased.insert(make_pair(g[x][i], x));
						erased.insert(make_pair(x, g[x][i]));
					}
				}
				if (waiting[g[x][i]] == g[g[x][i]].size() - 1 && wl[g[x][i]].size() == 1 || waiting[g[x][i]] == g[g[x][i]].size() && wl[g[x][i]].size() == 1) {
					wl[g[x][i]].erase(wl[g[x][i]].begin());
					q.push(make_pair(g[x][i], y + 1));
					from[g[x][i]] = x;
					xx = g[x][i];
				}
				else if (waiting[g[x][i]] == g[g[x][i]].size() && wl[g[x][i]].size() > 2 || waiting[g[x][i]] == g[g[x][i]].size() && wl[g[x][i]].size() > 2) {
					cout << -1 << endl;
					return 0;
				}
				else if (waiting[g[x][i]] == g[g[x][i]].size() && wl[g[x][i]].size() == 2) {
					xx = g[x][i];
				}
			}
		}
	}
	for (int i = 0; i < n; i++)
	{
		int c = 0;
		for (int j = 0; j < g[i].size(); j++)
		{
			c += erased.find(make_pair(i, g[i][j])) == erased.end();
		}
		if (c > 2) {
			cout << -1;
			return 0;
		}
	}
	vis.resize(n);
	int asd = dfs(g, xx, erased);
	while (asd & 1 && asd > 1) {
		asd = (asd + 1) / 2;
	}
	cout << asd - 1;
}


#include <vector>
#include <queue>
#include <iostream>
#include <string>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#include <limits>
#include <algorithm>
#include <iomanip>
#include <map>
#include <deque>
#include <set>

using namespace std;

vector<bool> vis;

int dfs(vector<vector<int> > & g, int idx, set<pair<int, int> >& erased) {
	vis[idx] = 1;
	int ret = 1;
	for (int i = 0; i < g[idx].size(); i++)
	{
		if (erased.find(make_pair(idx, g[idx][i])) == erased.end() && !vis[g[idx][i]]) {
			ret += dfs(g, g[idx][i], erased);
		}
	}
	return ret;
}

int main() {//(int n, vector<vector<int> >& g) {
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	int n; cin >> n;
	vector<vector<int> > g(n);
	for (int i = 0; i < n - 1; i++)
	{
		int u, v; cin >> u >> v;
		u--, v--;
		g[u].push_back(v);
		g[v].push_back(u);
	}
	vector<int> from(n, -1);
	queue<pair<int, int> > q;
	for (int i = 0; i < n; i++)
	{
		if (g[i].size() == 1)
			q.push(make_pair(i, 0));
	}
	vector<int> waiting(n, 0);
	vector<set<pair<int, int> > > wl(n);
	set<pair<int, int> >erased;
	int xx = 0;
	int ans = n - 1;
	while (q.size()) {
		int x = q.front().first;
		int y = q.front().second;
		q.pop();
		waiting[x]--;
		for (int i = 0; i < g[x].size(); i++)
		{
			if (g[x][i] != from[x] && erased.find(make_pair(x, g[x][i])) == erased.end()) {
				waiting[g[x][i]]++;

				if (g[g[x][i]].size() <= 2) {
					q.push(make_pair(g[x][i], y + 1));
					from[g[x][i]] = x;
				}
				else {
					if (wl[g[x][i]].lower_bound(make_pair(y, 0)) == wl[g[x][i]].end() || wl[g[x][i]].lower_bound(make_pair(y, 0))->first != y) {
						wl[g[x][i]].insert(make_pair(y, x));
					}
					else {
						erased.insert(make_pair(g[x][i], x));
						erased.insert(make_pair(x, g[x][i]));
						ans -= y+1;
					}
				}
				if (waiting[g[x][i]] == g[g[x][i]].size() - 1 && wl[g[x][i]].size() == 1 || waiting[g[x][i]] == g[g[x][i]].size() && wl[g[x][i]].size() == 1) {
					q.push(make_pair(g[x][i], y));
					wl[g[x][i]].erase(wl[g[x][i]].begin());
					from[g[x][i]] = x;
				}
				else if (waiting[g[x][i]] == g[g[x][i]].size() && wl[g[x][i]].size() > 2 || waiting[g[x][i]] == g[g[x][i]].size() && wl[g[x][i]].size() > 2) {
					cout << -1 << endl;
					return 0;
				}
				else if (waiting[g[x][i]] == g[g[x][i]].size() && wl[g[x][i]].size() == 2) {
					xx = g[x][i];
				}
			}
		}
	}
	for (int i = 0; i < n; i++)
	{
		int c = 0;
		for (int j = 0; j < g[i].size(); j++)
		{
			c += erased.find(make_pair(i, g[i][j])) == erased.end();
		}
		if (c > 2) {
			cout << -1;
			return 0;
		}
	}
	cout << ans;
}


#include <vector>
#include <queue>
#include <iostream>
#include <string>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#include <limits>
#include <algorithm>
#include <iomanip>
#include <map>
#include <deque>
#include <set>

using namespace std;

vector<bool> vis;

int dfs(vector<vector<int> > & g, int idx, set<pair<int, int> >& erased) {
	vis[idx] = 1;
	int ret = 1;
	for (int i = 0; i < g[idx].size(); i++)
	{
		if (erased.find(make_pair(idx, g[idx][i])) == erased.end() && !vis[g[idx][i]]) {
			ret += dfs(g,g[idx][i],erased);
		}
	}
	return ret;
}

int main(){//(int n, vector<vector<int> >& g) {
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	int n; cin >> n;
	vector<vector<int> > g(n);
	for (int i = 0; i < n - 1; i++)
	{
		int u, v; cin >> u >> v;
		u--, v--;
		g[u].push_back(v);
		g[v].push_back(u);
	}
	vector<int> from(n, -1);
	queue<pair<int, int> > q;
	for (int i = 0; i < n; i++)
	{
		if (g[i].size() == 1)
			q.push(make_pair(i, 0));
	}
	vector<int> waiting(n, 0);
	vector<set<pair<int, int> > > wl(n);
	set<pair<int, int> >erased;
	int xx = 0;
	while (q.size()) {
		int x = q.front().first;
		int y = q.front().second;
		q.pop();
		waiting[x]--;
		for (int i = 0; i < g[x].size(); i++)
		{
			if (g[x][i] != from[x]) {
				waiting[g[x][i]]++;
				if (g[g[x][i]].size() <= 2) {
					q.push(make_pair(g[x][i], y + 1));
					from[g[x][i]] = x;
				}
				else {
					if (wl[g[x][i]].lower_bound(make_pair(y, 0)) == wl[g[x][i]].end() || wl[g[x][i]].lower_bound(make_pair(y, 0))->first != y) {
						wl[g[x][i]].insert(make_pair(y, x));
						xx = g[x][i];
					}
					else {
						erased.insert(make_pair(g[x][i], x));
						erased.insert(make_pair(x, g[x][i]));
					}
				}
				if (waiting[g[x][i]] == g[g[x][i]].size() - 1 && wl[g[x][i]].size() == 1 || waiting[g[x][i]] == g[g[x][i]].size() && wl[g[x][i]].size() == 1) {
					wl[g[x][i]].erase(wl[g[x][i]].begin());
					q.push(make_pair(g[x][i], y + 1));
					from[g[x][i]] = x;
				}
				else if (waiting[g[x][i]] == g[g[x][i]].size() && wl[g[x][i]].size() > 2 || waiting[g[x][i]] == g[g[x][i]].size() && wl[g[x][i]].size() > 2) {
					cout << -1 << endl;
					return 0;
				}
				else if (waiting[g[x][i]] == g[g[x][i]].size() && wl[g[x][i]].size() == 2) {
					//xx = g[x][i];
				}
			}
		}
	}
	for (int i = 0; i < n; i++)
	{
		int c = 0;
		for (int j = 0; j < g[i].size(); j++)
		{
			c += erased.find(make_pair(i, g[i][j])) == erased.end();
		}
		if (c > 2) {
			cout << -1;
			return 0;
		}
	}
	vis.resize(n);
	int asd = dfs(g, xx, erased);
	while (asd & 1 && asd > 1) {
		asd = (asd + 1) / 2;
	}
	cout << asd - 1;
}


#include <vector>
#include <queue>
#include <iostream>
#include <string>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#include <limits>
#include <algorithm>
#include <iomanip>
#include <map>
#include <deque>
#include <set>

using namespace std;

vector<bool> vis;

int dfs(vector<vector<int> > & g, int idx, set<pair<int, int> >& erased) {
	vis[idx] = 1;
	int ret = 1;
	for (int i = 0; i < g[idx].size(); i++)
	{
		if (erased.find(make_pair(idx, g[idx][i])) == erased.end() && !vis[g[idx][i]]) {
			ret += dfs(g,g[idx][i],erased);
		}
	}
	return ret;
}

int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	int n; cin >> n;
	vector<vector<int> > g(n);
	for (int i = 0; i < n - 1; i++)
	{
		int u, v; cin >> u >> v;
		u--, v--;
		g[u].push_back(v);
		g[v].push_back(u);
	}
	vector<int> from(n, -1);
	queue<pair<int, int> > q;
	for (int i = 0; i < n; i++)
	{
		if (g[i].size() == 1)
			q.push(make_pair(i, 0));
	}
	vector<int> waiting(n, 0);
	vector<set<pair<int, int> > > wl(n);
	set<pair<int, int> >erased;
	int xx = -1;
	while (q.size()) {
		int x = q.front().first;
		int y = q.front().second;
		xx = x;
		q.pop();
		waiting[x]--;
		for (int i = 0; i < g[x].size(); i++)
		{
			if (g[x][i] != from[x]) {
				waiting[g[x][i]]++;
				if (g[g[x][i]].size() <= 2) {
					q.push(make_pair(g[x][i], y + 1));
					from[g[x][i]] = x;
				}
				else {
					if (wl[g[x][i]].lower_bound(make_pair(y, 0)) == wl[g[x][i]].end() || wl[g[x][i]].lower_bound(make_pair(y, 0))->first != y) {
						wl[g[x][i]].insert(make_pair(y, x));
					}
					else {
						erased.insert(make_pair(g[x][i], x));
						erased.insert(make_pair(x, g[x][i]));
					}
				}
				if (waiting[g[x][i]] == g[g[x][i]].size() - 1 && wl[g[x][i]].size() == 1 || waiting[g[x][i]] == g[g[x][i]].size() && wl[g[x][i]].size() == 1) {
					wl[g[x][i]].erase(wl[g[x][i]].begin());
					q.push(make_pair(g[x][i], y + 1));
					from[g[x][i]] = x;
				}
				else if (waiting[g[x][i]] == g[g[x][i]].size() && wl[g[x][i]].size() > 2 || waiting[g[x][i]] == g[g[x][i]].size() && wl[g[x][i]].size() > 2) {
					cout << -1 << endl;
					return 0;
				}
				else if (waiting[g[x][i]] == g[g[x][i]].size() && wl[g[x][i]].size() == 2) {
					xx = g[x][i];
				}
			}
		}
	}
	for (int i = 0; i < n; i++)
	{
		int c = 0;
		for (int j = 0; j < g[i].size(); j++)
		{
			c += erased.find(make_pair(i, g[i][j])) == erased.end();
		}
		if (c > 2) {
			cout << -1;
			return 0;
		}
	}
	vis.resize(n);
	int asd = dfs(g, xx, erased);
	while (asd & 1 && asd > 1) {
		asd = (asd + 1) / 2;
	}
	cout << asd - 1;
	return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <map>

using namespace std;
#define pb push_back
#define ff first
#define ss second
#define pii pair <int, int>
#define mp make_pair
const int INF = 1e9 + 7;

int ans = INF;

int better(int n) {
	while (n != 0 && n % 2 == 0)
		n /= 2;
	return n;
}

int minb(int a, int b) {
	return min(better(a), better(b));
}

vector <vector <int> > g;
vector <int> one;
vector <pii> two;

void dfs1(int v, int prev) {
	map <int, int> am;
	for (int u : g[v]) {
		if (u == prev)
			continue;
		dfs1(u, v);
		am[one[u] + 1]++;
	}
	if (am.size() > 2) {
		one[v] = -1;
	} else if (am.size() == 1) {
		one[v] = (*(am.begin())).ff;
		if (one[v] == 0)
            one[v] = -1;
	} else if (am.size() == 0) {
		one[v] = 0;
	} else {
		one[v] = -1;
		two[v] = mp((*(am.begin())).ff, (*(++am.begin())).ff);
		if (two[v].ff == 0 || two[v].ss == 0) {
			two[v].ss = -1;
            two[v].ff = -1;
        }
	}
}

void dfs(int v, int prev, int c) {
    //cout << "xx " << v << ' ' << prev << ' ' << c << endl;
	if (one[v] != -1) {
		ans = minb(ans, one[v] + c);
		if (one[v] == c)
			ans = minb(ans, one[v]);
        //cout << "ans1 " << ans << endl;
	} if (prev == -1 && two[v] != mp(-1, -1)) {
		ans = minb(ans, two[v].ff + two[v].ss);
		//cout << "ans2 " << ans << endl;
	} if (two[v] != mp(-1, -1) && (two[v].ff == c || two[v].ss == c)) {
		ans = minb(ans, two[v].ff + two[v].ss);
		//cout << "ans3 " << ans << endl;
	}

	map <int, int> am;
	for (int u : g[v]) {
		if (u != prev)
			am[one[u] + 1]++;
	}
	for (int u : g[v]) {
		if (u == prev)
			continue;

		am[one[u] + 1]--;
		if (am[one[u] + 1] == 0)
			am.erase(one[u] + 1);

		if (am.size() == 1 && ((*(am.begin())).ff == c || c == 0) && ((*(am.begin())).ff) != 0) {
			dfs(u, v, (*(am.begin())).ff + 1);
		}
		am[one[u] + 1]++;
    }
}

int main() {
	int n;
	cin >> n;
	g.resize(n); one.resize(n, -1);
	two.resize(n, mp(-1, -1));
	for (int i = 1; i < n; ++i) {
		int u, v;
		cin >> u >> v;
		u--; v--;
		g[u].pb(v); g[v].pb(u);
	}
	dfs1(0, -1);

	/*for (auto i : one)
        cout << i << ' ';
    cout << endl;
    for (auto i : two)
        cout << i.ff << ',' << i.ss << ' ';
    cout << endl;*/
	dfs(0, -1, 0);
	if (ans == INF)
        cout << "-1\n";
    else
        cout << ans << endl;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <map>

using namespace std;
#define pb push_back
#define ff first
#define ss second
#define pii pair <int, int>
#define mp make_pair
const int INF = 1e9 + 7;

int ans = INF;

vector <vector <int> > g;
vector <int> one;
vector <pii> two;

void dfs1(int v, int prev) {
	map <int, int> am;
	for (int u : g[v]) {
		if (u == prev)
			continue;
		dfs1(u, v);
		am[one[u] + 1]++;
	}
	if (am.size() > 2) {
		one[v] = -1;
	} else if (am.size() == 1) {
		one[v] = (*(am.begin())).ff;
		if (one[v] == 0)
            one[v] = -1;
	} else if (am.size() == 0) {
		one[v] = 0;
	} else {
		one[v] = -1;
		two[v] = mp((*(am.begin())).ff, (*(++am.begin())).ff);
		if (two[v].ff == 0 || two[v].ss == 0) {
			two[v].ss = -1;
            two[v].ff = -1;
        }
	}
}

void dfs(int v, int prev, int c) {
    //cout << "xx " << v << ' ' << prev << ' ' << c << endl;
	if (one[v] != -1) {
		ans = min(ans, one[v] + c);
		if (one[v] == c)
			ans = min(ans, one[v]);
        //cout << "ans1 " << ans << endl;
	} if (prev == -1 && two[v] != mp(-1, -1)) {
		ans = min(ans, two[v].ff + two[v].ss);
		//cout << "ans2 " << ans << endl;
	} if (two[v] != mp(-1, -1) && (two[v].ff == c || two[v].ss == c)) {
		ans = min(ans, two[v].ff + two[v].ss);
		//cout << "ans3 " << ans << endl;
	}

	map <int, int> am;
	for (int u : g[v]) {
		if (u != prev)
			am[one[u] + 1]++;
	}
	for (int u : g[v]) {
		if (u == prev)
			continue;

		am[one[u] + 1]--;
		if (am[one[u] + 1] == 0)
			am.erase(one[u] + 1);

		if (am.size() == 1 && ((*(am.begin())).ff == c || c == 0) && ((*(am.begin())).ff) != 0) {
			dfs(u, v, (*(am.begin())).ff + 1);
		}
		am[one[u] + 1]++;
    }
}

int main() {
	int n;
	cin >> n;
	g.resize(n); one.resize(n, -1);
	two.resize(n, mp(-1, -1));
	for (int i = 1; i < n; ++i) {
		int u, v;
		cin >> u >> v;
		u--; v--;
		g[u].pb(v); g[v].pb(u);
	}
	dfs1(0, -1);

	/*for (auto i : one)
        cout << i << ' ';
    cout << endl;
    for (auto i : two)
        cout << i.ff << ',' << i.ss << ' ';
    cout << endl;*/
	dfs(0, -1, 0);
	if (ans == INF)
        cout << "-1\n";
    else
        cout << ans << endl;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <map>

using namespace std;
#define pb push_back
#define ff first
#define ss second
#define pii pair <int, int>
#define mp make_pair
const int INF = 1e9 + 7;

int ans = INF;
vector <vector <int> > g;
vector <int> one;

int better(int n) {
	while (n != 0 && n % 2 == 0)
		n /= 2;
	return n;
}

void dfs1(int v, int prev) {
	int ans = -1;
	for (int u : g[v]) {
		if (u == prev)
			continue;
		dfs1(u, v);
		if (one[u] == -1 || (ans != -1 && one[u] != ans))
			ans = -2;
		if (ans != -2)
			ans = one[u];

	}
	one[v] = ans + 1;
}

void dfs(int v, int prev, int c) {
	//cout << "v " << v << ' ' << prev << ' ' << c << endl;
	if (one[v] != -1) {
		ans = min(ans, better(one[v] + c));
	}

	map <int, int> am;
	for (int u : g[v]) {
		if (u != prev) {
			if (one[u] == -1)
				am[-INF]++;
			else
				am[one[u] + 1]++;
		}
	}
	if (c != 0)
		am[c]++;
	if (am.size() == 2) {
		int cnt = (*(am.begin())).ff;
		int cnt2 = (*(++am.begin())).ff;
		if (cnt != -INF && cnt2 != -INF)
			ans = min(ans, better(cnt + cnt2));
	}
	for (int u : g[v]) {
		if (u == prev)
			continue;

		int ind = one[u] + 1;
		if (ind == 0)
			ind = -INF;
		am[ind]--;
		if (am[ind] == 0)
			am.erase(ind);
		//cout << "ind " << ind << ' ' << am.size() << endl;

		int cnt = (*(am.begin())).ff;
		if (am.size() == 1 && cnt != -INF) {
			dfs(u, v, cnt + 1);
		}
		if (am.size() == 0)
			dfs(u, v, 1);
		am[ind]++;
    }
}

int main() {
	int n;
	cin >> n;
	g.resize(n);
	for (int i = 1; i < n; ++i) {
		int u, v;
		cin >> u >> v;
		u--; v--;
		g[u].pb(v); g[v].pb(u);
	}
	one.assign(n, -1);
	dfs1(0, -1);
	dfs(0, -1, 0);

	if (ans == INF)
        cout << "-1\n";
    else
		cout << ans << endl;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <map>
#include <set>

using namespace std;
#define pb push_back
#define ff first
#define ss second
#define pii pair <int, int>
#define mp make_pair
const int INF = 1e9 + 7;

int ans = INF;

vector <vector <int> > g;
vector <int> one;
vector <pii> two;

void dfs1(int v, int prev) {
	int ans = -1;
	for (int u : g[v]) {
		if (u == prev)
			continue;
		dfs1(u, v);
		if (one[u] == -1 || (ans != -1 && one[u] != ans))
			ans = -2;
		if (ans != -2)
			ans = one[u];

	}
	one[v] = ans + 1;
}

void dfs(int v, int prev, int c) {
	//cout << "v " << v << ' ' << prev << ' ' << c << endl;
    if (one[v] != -1) {
		ans = min(ans, one[v] + c);
		if (one[v] == c)
			ans = min(ans, one[v]);
	}

	map <int, int> am;
	for (int u : g[v]) {
		if (u != prev)
			am[one[u]]++;
	}
	for (int u : g[v]) {
		if (u == prev)
			continue;

		am[one[u]]--;
		if (am[one[u]] == 0)
			am.erase(one[u]);

		if (am.size() == 1 && ((*(am.begin())).ff + 1 == c || c == 0) && ((*(am.begin())).ff) != -1) {
			dfs(u, v, (*(am.begin())).ff + 2);
		}
		am[one[u]]++;
    }
}

int main() {
	int n;
	cin >> n;
	g.resize(n); one.resize(n, -1);
	two.resize(n, mp(-1, -1));
	for (int i = 1; i < n; ++i) {
		int u, v;
		cin >> u >> v;
		u--; v--;
		g[u].pb(v); g[v].pb(u);
	}
	dfs1(0, -1);

	/*for (auto i : one)
        cout << i << ' ';
    cout << endl;*/
	dfs(0, -1, 0);
	if (ans == INF)
        cout << "-1\n";
    else
        cout << ans << endl;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <map>
#include <set>

using namespace std;
#define pb push_back
#define ff first
#define ss second
#define pii pair <int, int>
#define mp make_pair
const int INF = 1e9 + 7;

int ans = INF;

vector <vector <int> > g;
vector <int> one;
vector <pii> two;

int better(int n) {
	while (n != 0 && n % 2 == 0)
		n /= 2;
	return n;
}

void dfs1(int v, int prev) {
	int ans = -1;
	for (int u : g[v]) {
		if (u == prev)
			continue;
		dfs1(u, v);
		if (one[u] == -1 || (ans != -1 && one[u] != ans))
			ans = -2;
		if (ans != -2)
			ans = one[u];

	}
	one[v] = ans + 1;
}

void dfs(int v, int prev, int c) {
	//cout << "v " << v << ' ' << prev << ' ' << c << endl;
    if (one[v] != -1) {
		ans = min(ans, better(one[v] + c));
		if (one[v] == c)
			ans = min(ans, better(one[v]));
	}

	map <int, int> am;
	for (int u : g[v]) {
		if (u != prev)
			am[one[u]]++;
	}
	for (int u : g[v]) {
		if (u == prev)
			continue;

		am[one[u]]--;
		if (am[one[u]] == 0)
			am.erase(one[u]);

		if (am.size() == 1 && ((*(am.begin())).ff + 1 == c || c == 0) && ((*(am.begin())).ff) != -1) {
			dfs(u, v, (*(am.begin())).ff + 2);
		}
		if (am.size() == 0) {
			dfs(u, v, c + 1);
		}
		am[one[u]]++;
    }
}

int main() {
	int n;
	cin >> n;
	g.resize(n); one.resize(n, -1);
	two.resize(n, mp(-1, -1));
	for (int i = 1; i < n; ++i) {
		int u, v;
		cin >> u >> v;
		u--; v--;
		g[u].pb(v); g[v].pb(u);
	}
	dfs1(0, -1);

	/*for (auto i : one)
        cout << i << ' ';
    cout << endl;*/
	dfs(0, -1, 0);
	if (ans == INF) {
        cout << "-1\n";
        return 0;
	}
	cout << ans << endl;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <map>
#include <set>

using namespace std;
#define pb push_back
#define ff first
#define ss second
#define pii pair <int, int>
#define mp make_pair
const int INF = 1e9 + 7;

int ans = INF;
vector <vector <int> > g;
vector <int> one;

int better(int n) {
	while (n != 0 && n % 2 == 0)
		n /= 2;
	return n;
}

void dfs1(int v, int prev) {
	int ans = -1;
	for (int u : g[v]) {
		if (u == prev)
			continue;
		dfs1(u, v);
		if (one[u] == -1 || (ans != -1 && one[u] != ans))
			ans = -2;
		if (ans != -2)
			ans = one[u];

	}
	one[v] = ans + 1;
}

void dfs(int v, int prev, int c) {
	if (one[v] != -1) {
		ans = min(ans, better(one[v] + c));
	}

	map <int, int> am;
	for (int u : g[v]) {
		if (u != prev)
			am[one[u]]++;
	}
	for (int u : g[v]) {
		if (u == prev)
			continue;

		am[one[u]]--;
		if (am[one[u]] == 0)
			am.erase(one[u]);

		if (am.size() == 1 && ((*(am.begin())).ff + 1 == c || c == 0) && ((*(am.begin())).ff) != -1) {
			dfs(u, v, (*(am.begin())).ff + 2);
		}
		if (am.size() == 0) {
			dfs(u, v, c + 1);
		}
		am[one[u]]++;
    }
}

int main() {
	int n;
	cin >> n;
	g.resize(n); one.resize(n, -1);
	for (int i = 1; i < n; ++i) {
		int u, v;
		cin >> u >> v;
		u--; v--;
		g[u].pb(v); g[v].pb(u);
	}
	for (int i = 0; i < 50; ++i) {
		one.resize(n, -1);
		dfs1(i, -1);
		dfs(i, -1, 0);
	}
	if (ans == INF)
        cout << "-1\n";
    else
		cout << ans << endl;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <map>
#include <set>

using namespace std;
#define pb push_back
#define ff first
#define ss second
#define pii pair <int, int>
#define mp make_pair
const int INF = 1e9 + 7;

int ans = INF;
vector <vector <int> > g;
vector <int> one;

int better(int n) {
	while (n != 0 && n % 2 == 0)
		n /= 2;
	return n;
}

void dfs1(int v, int prev) {
	int ans = -1;
	for (int u : g[v]) {
		if (u == prev)
			continue;
		dfs1(u, v);
		if (one[u] == -1 || (ans != -1 && one[u] != ans))
			ans = -2;
		if (ans != -2)
			ans = one[u];

	}
	one[v] = ans + 1;
}

void dfs(int v, int prev, int c) {
	if (one[v] != -1) {
		ans = min(ans, better(one[v] + c));
	}

	map <int, int> am;
	for (int u : g[v]) {
		if (u != prev)
			am[one[u]]++;
	}
	for (int u : g[v]) {
		if (u == prev)
			continue;

		am[one[u]]--;
		if (am[one[u]] == 0)
			am.erase(one[u]);

		if (am.size() == 1 && ((*(am.begin())).ff + 1 == c || c == 0) && ((*(am.begin())).ff) != -1) {
			dfs(u, v, (*(am.begin())).ff + 2);
		}
		if (am.size() == 0) {
			dfs(u, v, c + 1);
		}
		am[one[u]]++;
    }
}

int main() {
	int n;
	cin >> n;
	g.resize(n);
	for (int i = 1; i < n; ++i) {
		int u, v;
		cin >> u >> v;
		u--; v--;
		g[u].pb(v); g[v].pb(u);
	}
	for (int i = 0; i < min(n, 50); ++i) {
		one.assign(n, -1);
		dfs1(i, -1);
		dfs(i, -1, 0);
	}
	if (ans == INF)
        cout << "-1\n";
    else
		cout << ans << endl;
}


#include<bits/stdc++.h>
#define x first
#define y second
using namespace std;
using pii = pair<int, int>;
const int N = 2e5+10, inf = 3e5;
vector<int> adj[N];
pii mx[N][3];
int bad[N], p[N], up[N];
void add(int u, int d) {
	for(int i=0; i<3; i++) if(mx[u][i].x == d) {
		mx[u][i].y++;
		return;
	}
	if(mx[u][0].x < d) {
		mx[u][0] = {d, 1};
		sort(mx[u], mx[u]+3);
	}
}
void dfs(int u, int fa=0) {
	for(auto v:adj[u]) {
		if(v == fa) continue;
		p[v] = u;
		dfs(v, u);
		if(bad[v] || mx[v][1].x) bad[u]++;
		else add(u, mx[v][2].x+1);
	}
}
bool update(int v, int u) {
	if(bad[u]>1) return true;
	if(p[u] && !up[u]) return true;
	if(bad[u] == 1 && (!mx[v][1].x && !bad[v])) return true;
	pii a = mx[u][0], b = mx[u][1], c = mx[u][2];
	if(!bad[u]) {
		if(mx[v][2].x+1 == a.x) a.y--;
		if(mx[v][2].x+1 == b.x) b.y--;
		if(mx[v][2].x+1 == c.x) c.y--;
	}
	if((int)(a.y>0) + (int)(b.y>0) + (int)(c.y>0) > 1) return true;
	if(p[u] && a.y && a.x != up[u]) return true;
	if(p[u] && b.y && b.x != up[u]) return true;
	if(p[u] && c.y && c.x != up[u]) return true;
	if(adj[u].size() == 1) up[v] = 1;
	else {
		if(p[u]) up[v] = up[u]+1;
		else if(a.y) up[v] = a.x+1;
		else if(b.y) up[v] = b.x+1;
		else if(c.y) up[v] = c.x+1;
		else {cerr<<"ERROR"<<endl;exit(0);}
	}
	return false;
}
void redfs(int u, int fa=0) {
	for(auto v:adj[u]) {
		if(v == fa) continue;
		if(update(v, u)) bad[v]++;
		redfs(v, u);
	}
}
int main() {
#ifndef ONLINE_JUDGE
	freopen("in.in", "r", stdin);
	freopen("test.out", "w", stdout);
#endif
	int n;
	scanf("%d", &n);
	for(int i=1; i<n; i++) {
		int u, v;
		scanf("%d %d", &u, &v);
		adj[u].push_back(v);
		adj[v].push_back(u);
	}
	dfs(1);
	redfs(1);
	int ans = inf;
	for(int i=1; i<=n; i++) {
		if(bad[i]) continue;
		if(p[i] && !up[i]) continue;
		set<int> st;
		for(auto j:adj[i]) {
			if(j == p[i]) st.insert(up[i]);
			else st.insert(mx[j][2].x+1);
			if(st.size()>2) break;
		}
		if(st.size()>2) continue;
		int t = *st.begin() + *--st.end();
		while(t%2 == 0) t/=2;
		ans = min(ans, t);
	}
	if(ans == inf) ans = -1;
	cout<<ans<<endl;
	return 0;
}


#include<bits/stdc++.h>
#define pb push_back
#define x first
#define y second
using namespace std;
using pii=pair<int,int>;
using pi3=pair<pii,int>;
const int NN=272727;
set <int> st[NN];
pii p[NN];
int vst[NN];
queue <int> q;
vector <int> A;
map <pii,int> M;

int main(){
#ifndef ONLINE_JUDGE
	freopen("in.in","r",stdin);
	freopen("out.out","w",stdout);
#endif
	int n;scanf("%d",&n);
	for(int i=1;i<n;i++){
		int u,v;
		scanf("%d%d",&u,&v);
		st[u].insert(v);
		st[v].insert(u);
	}
	int fl=0;
	for(int i=1;i<=n;i++){
		if(st[i].size()==1) q.push(i);
		if(st[i].size()>2) fl=1;
	}
	if(!fl){
		int ans=n-1;
		while(ans%2==0) ans>>=1;
		printf("%d\n",ans);
		return 0;
	}
	int ans=-1;
	while(!q.empty()){
		int u=q.front();
		q.pop();
		int v=u;
		int flag=0,len=0;
		A.clear();
		while(st[v].size()<=2){
			if(flag and st[v].size()==1) break;
			len++;
			flag=1;
			A.pb(v);
			vst[v]=1;
			for(int w:st[v]) if(!vst[w]) {v=w;break;}
		}
		for(int w:A) vst[w]=0;
		if(st[v].size()==1){
			ans=len;
			break;
		}
		if(M[{v,len}]){
			if(M[{v,len}]!=u){
				int w=A.back();
				st[v].erase(w);
			}
		}
		else {
			M[{v,len}]=u;
			q.push(u);
		}
	}
	while(ans%2==0) ans>>=1;
	printf("%d\n",ans);
	return 0;
}


#include<bits/stdc++.h>
#define pb push_back
#define x first
#define y second
using namespace std;
using pii=pair<int,int>;
using pi3=pair<pii,int>;
const int NN=272727;
set <int> st[NN];
pii p[NN];
int vst[NN],d[NN];
queue <int> q;
vector <int> A;
map <pii,int> M;

int main(){
#ifndef ONLINE_JUDGE
	freopen("in.in","r",stdin);
	freopen("out.out","w",stdout);
#endif
	int n;scanf("%d",&n);
	for(int i=1;i<n;i++){
		int u,v;
		scanf("%d%d",&u,&v);
		st[u].insert(v);
		st[v].insert(u);
	}
	int fl=0;
	for(int i=1;i<=n;i++){
		if(st[i].size()==1) q.push(i);
		if(st[i].size()>2) fl=1;
	}
	if(!fl){
		int ans=n-1;
		while(ans%2==0) ans>>=1;
		printf("%d\n",ans);
		return 0;
	}
	int ans=-1;
	int ch=0;
	for(int i=1;i<=n;i++) d[i]=1e9;
	while(!q.empty()){
		int u=q.front();
		q.pop();
		int v=u;
		int flag=0,len=0;
		A.clear();
		while(st[v].size()<=2){
			if(flag and st[v].size()==1) break;
			len++;
			flag=1;
			A.pb(v);
			vst[v]=1;
			for(int w:st[v]) if(!vst[w]) {v=w;break;}
		}
		for(int w:A) vst[w]=0;
		if(st[v].size()==1){
			ans=len;
			break;
		}
		if(M[{v,len}]!=u and M[{v,len}]){
			int w=A.back();
			st[v].erase(w);
			ch++;
		}
		else {
			M[{v,len}]=u;
			q.push(u);
		}
		if(ch==d[u]) break;
		d[u]=ch;
	}
	while(ans%2==0) ans>>=1;
	printf("%d\n",ans);
	return 0;
}


#include<bits/stdc++.h>
#define x first
#define y second
using namespace std;
using pii = pair<int, int>;
const int N = 2e5+10, inf = 3e5;
vector<int> adj[N];
pii mx[N][3];
int bad[N], p[N], up[N];
void add(int u, int d) {
	for(int i=0; i<3; i++) if(mx[u][i].x == d) {
		mx[u][i].y++;
		return;
	}
	if(mx[u][0].x < d) {
		mx[u][0] = {d, 1};
		sort(mx[u], mx[u]+3);
	}
}
void dfs(int u, int fa=0) {
	for(auto v:adj[u]) {
		if(v == fa) continue;
		p[v] = u;
		dfs(v, u);
		if(bad[v] || mx[v][1].x) bad[u]++;
		else add(u, mx[v][2].x+1);
	}
}
bool update(int v, int u) {
	if(bad[u]>1) return true;
	if(p[u] && !up[u]) return true;
	if(bad[u] == 1 && (!mx[v][1].x || bad[v])) return true;
	pii a = mx[u][0], b = mx[u][1], c = mx[u][2];
	if(!bad[u]) {
		if(mx[v][2].x+1 == a.x) a.y--;
		if(mx[v][2].x+1 == b.x) b.y--;
		if(mx[v][2].x+1 == c.x) c.y--;
	}
	if((int)(a.y>0) + (int)(b.y>0) + (int)(c.y>0) > 1) return true;
	if(p[u] && a.y && a.x != up[u]) return true;
	if(p[u] && b.y && b.x != up[u]) return true;
	if(p[u] && c.y && c.x != up[u]) return true;
	if(adj[u].size() == 1) up[v] = 1;
	else {
		if(p[u]) up[v] = up[u]+1;
		else if(a.y) up[v] = a.x+1;
		else if(b.y) up[v] = b.x+1;
		else if(c.y) up[v] = c.x+1;
		else {cerr<<"ERROR"<<endl;exit(0);}
//		add(v, up[v]);
	}
	return false;
}
void redfs(int u, int fa=0) {
	for(auto v:adj[u]) {
		if(v == fa) continue;
		if(update(v, u)) bad[v]++;
		redfs(v, u);
	}
}
int main() {
#ifndef ONLINE_JUDGE
	freopen("in.in", "r", stdin);
	freopen("out.out", "w", stdout);
#endif
	int n;
	scanf("%d", &n);
	for(int i=1; i<n; i++) {
		int u, v;
		scanf("%d %d", &u, &v);
		adj[u].push_back(v);
		adj[v].push_back(u);
	}
	dfs(1);
	redfs(1);
	int ans = inf;
	for(int i=1; i<=n; i++) {
		if(bad[i]) continue;
		if(p[i] && !up[i]) continue;
		set<int> st;
		for(auto j:adj[i]) {
			if(j == p[i]) st.insert(up[i]);
			else st.insert(mx[j][2].x+1);
			if(st.size()>2) break;
		}
		if(st.size()>2) continue;
		int t = *st.begin() + *--st.end();
		while(t%2 == 0) t/=2;
		ans = min(ans, t);
	}
	if(ans == inf) ans = -1;
	cout<<ans<<endl;
	return 0;
}


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

typedef long long INT;
typedef pair<int, int> pii;

vector<int> adj[NN];

int deep[NN][2], dep[NN];

vector<int> Deep[NN];

void dfs(int u, int fa = 0){
	for(auto v : adj[u]){
		if(v == fa) continue;
		dfs(v, u);
		Deep[u].push_back(deep[v][0] + 1);
		deep[u][1] = max(deep[u][1], deep[v][0] + 1);
		if(deep[u][0] < deep[u][1]) swap(deep[u][0], deep[u][1]);
	}
}

void rdfs(int u, int fa = 0){
	if(u != 1) Deep[u].push_back(dep[u]);
	for(auto v : adj[u]){
		if(v == fa) continue;
		dep[v] = dep[u] + 1;
		if(deep[v][0] + 1 == deep[u][0]) dep[v] = max(dep[v], deep[u][1] + 1);
		else dep[v] = max(dep[v], deep[u][0] + 1);
		rdfs(v, u);
	}
}

int main(){
#ifndef ONLINE_JUDGE
	freopen("in.in", "r", stdin);
	freopen("out.out", "w", stdout);
#endif
//	ios_base :: sync_with_stdio(0);cin.tie(0);
	int n;
	scanf("%d", &n);
	for(int i = 1; i < n; i++) {
		int u, v;
		scanf("%d%d", &u, &v);
		adj[u].push_back(v);
		adj[v].push_back(u);
	}

	dfs(1);
	rdfs(1);
//	for(int i = 1; i <= n; i++) cerr<<dep[i]<<endl;
	int check = 1;
	for(int i = 1; i <= n; i++) {
		sort(Deep[i].begin(), Deep[i].end());
		Deep[i].resize(unique(Deep[i].begin(), Deep[i].end()) - Deep[i].begin());
		if(Deep[i].size() > 2) {check = 0; break;}
	}
	if(check) {
		int ans = 0;
		for(int i = 1; i <= n; i++){
			int dis = 0;
			for(auto v : Deep[i]){
				dis += v;
			}
			while(dis % 2 == 0) dis /= 2;
			ans = max(ans, dis);
		}
		printf("%d\n", ans);
	}
	else puts("-1");

	return 0;
}


#include<bits/stdc++.h>
#define x first
#define y second
using namespace std;
using pii = pair<int, int>;
const int N = 2e5+10, inf = 3e5;
vector<int> adj[N];
pii mx[N][3];
int bad[N], p[N], up[N];
void add(int u, int d) {
	for(int i=0; i<3; i++) if(mx[u][i].x == d) {
		mx[u][i].y++;
		return;
	}
	if(mx[u][0].x < d) {
		mx[u][0] = {d, 1};
		sort(mx[u], mx[u]+3);
	}
}
void dfs(int u, int fa=0) {
	for(auto v:adj[u]) {
		if(v == fa) continue;
		p[v] = u;
		dfs(v, u);
		if(bad[v] || mx[v][1].x) bad[u]++;
		else add(u, mx[v][2].x+1);
	}
}
bool update(int v, int u) {
	if(bad[u]>1) return true;
	if(p[u] && !up[u]) return true;
	if(bad[u] == 1 && !mx[v][1].x) return true;
	pii a = mx[u][0], b = mx[u][1], c = mx[u][2];
	if(!bad[u]) {
		if(mx[v][2].x+1 == a.x) a.y--;
		if(mx[v][2].x+1 == b.x) b.y--;
		if(mx[v][2].x+1 == c.x) c.y--;
	}
	if((int)(a.y>0) + (int)(b.y>0) + (int)(c.y>0) > 1) return true;
	if(p[u] && a.y && a.x != up[u]) return true;
	if(p[u] && b.y && b.x != up[u]) return true;
	if(p[u] && c.y && c.x != up[u]) return true;
	if(adj[u].size() == 1) up[v] = 1;
	else {
		if(p[u]) up[v] = up[u]+1;
		else if(a.y) up[v] = a.x+1;
		else if(b.y) up[v] = b.x+1;
		else if(c.y) up[v] = c.x+1;
		else {cerr<<"ERROR"<<endl;exit(0);}
	}
	return false;
}
void redfs(int u, int fa=0) {
	for(auto v:adj[u]) {
		if(v == fa) continue;
		if(update(v, u)) bad[v]++;
		redfs(v, u);
	}
}
int main() {
#ifndef ONLINE_JUDGE
	freopen("in.in", "r", stdin);
	freopen("out.out", "w", stdout);
#endif
	int n;
	scanf("%d", &n);
	for(int i=1; i<n; i++) {
		int u, v;
		scanf("%d %d", &u, &v);
		adj[u].push_back(v);
		adj[v].push_back(u);
	}
	dfs(1);
	redfs(1);
	int ans = inf;
	for(int i=1; i<=n; i++) {
		if(bad[i]) continue;
		if(p[i] && !up[i]) continue;
		set<int> st;
		for(auto j:adj[i]) {
			if(j == p[i]) st.insert(up[i]);
			else st.insert(mx[j][2].x+1);
			if(st.size()>2) break;
		}
		if(st.size()>2) continue;
		int t = *st.begin() + *--st.end();
		while(t%2 == 0) t/=2;
		ans = min(ans, t);
	}
	if(ans == inf) ans = -1;
	cout<<ans<<endl;
	return 0;
}