#include<bits/stdc++.h>

using namespace std;
vector<int> v[200005],path[200005],l,r,res;
int diameter=0,root=1,f[200005],midPoint,visited[200005];

int dfs(int node,int parent)
{
    int temp=0,pointer,length=0;
    for(int i=0;i<v[node].size();i++)
    {
        pointer=v[node][i];

        if(pointer==parent) continue;

        temp=dfs(pointer,node);

        if(node==midPoint)
        {
            if(temp==-1)
            return -1;

            else
            res.push_back(temp);
        }

        else
        {
            if(length==0)
            length=temp;

            else if(length!=temp)
            return -1;
        }
    }

    return length+1;
}

void makePath1(int root)
{
    while(path[root].size()!=0)
    {
        l.push_back(root);
        root=path[root][0];
    }

    l.push_back(root);
}

void makePath2(int root)
{
    while(path[root].size()!=0)
    {
        r.push_back(root);
        root=path[root][0];
    }

    r.push_back(root);
}

void findRoot(int node,int parent)
{
    vector<pair<int,int> > fvalues;
    int i,pointer,sum=0;

    for(i=0;i<v[node].size();i++)
    {
        pointer=v[node][i];
        if(pointer==parent) continue;
        findRoot(pointer,node);
        fvalues.push_back(make_pair(f[pointer],pointer));
    }

    f[node]=1;

    if(!fvalues.empty())
    {
        sort(fvalues.begin(),fvalues.end());

        if(fvalues.size()>=2)
        {
            sum=fvalues[fvalues.size()-1].first+ fvalues[fvalues.size()-2].first;
            path[node].push_back(fvalues[fvalues.size()-1].second);
            path[node].push_back(fvalues[fvalues.size()-2].second);
            f[node]=1+fvalues[fvalues.size()-1].first;
        }

        else
        {
            sum=fvalues[fvalues.size()-1].first;
            path[node].push_back(fvalues[fvalues.size()-1].second);
            f[node]=1+fvalues[fvalues.size()-1].first;
        }
    }

    if(diameter!=max(diameter,1+sum))
    {
        root=node;
        diameter=1+sum;
    }
}

int main()
{
    int n,i,a,b,j,counter=0,sum=0;
    cin>>n;

    for(i=0;i<n-1;i++)
    {
        cin>>a>>b;
        v[a].push_back(b);
        v[b].push_back(a);
    }

    findRoot(1,1);

    if(path[root].size()==2)
    {
        makePath1(root);
        makePath2(path[root][1]);

        for(i=0;i<r.size();i++)
        l.insert(l.begin(),r[i]);
    }

    else
    makePath1(root);

    midPoint=l[l.size()/2];

    int temp=dfs(midPoint,midPoint);

    if(temp==-1)
    cout<<"-1\n";

    else
    {
        for(i=0;i<200006;i++)
        visited[i]=0;

        for(i=0;i<res.size();i++)
        visited[res[i]]=1;

        for(i=0;i<200006;i++)
        {
            if(visited[i]==1)
            counter++;
        }

        if(counter>2)
        cout<<"-1\n";

        else
        {
            for(i=0;i<200006;i++)
            {
                if(visited[i]==1)
                sum=sum+i;
            }

            while(sum%2==0)
            {
                sum=sum/2;
            }

            //cout<<sum<<endl;
        }

    }
}


#include<bits/stdc++.h>

using namespace std;
vector<int> v[200005],path[200005],l,r,res;
int diameter=0,root=1,f[200005],midPoint,visited[200005];

int dfs(int node,int parent)
{
    int temp=0,pointer,length=0;
    for(int i=0;i<v[node].size();i++)
    {
        pointer=v[node][i];

        if(pointer==parent) continue;

        temp=dfs(pointer,node);

        if(node==midPoint)
        {
            if(temp==-1)
            return -1;

            else
            res.push_back(temp);
        }

        else
        {
            if(temp==-1)
            return -1;

            else if(length==0)
            length=temp;

            else if(length!=temp)
            return -1;
        }
    }

    return length+1;
}

void makePath1(int root)
{
    while(path[root].size()!=0)
    {
        l.push_back(root);
        root=path[root][0];
    }

    l.push_back(root);
}

void makePath2(int root)
{
    while(path[root].size()!=0)
    {
        r.push_back(root);
        root=path[root][0];
    }

    r.push_back(root);
}

void findRoot(int node,int parent)
{
    vector<pair<int,int> > fvalues;
    int i,pointer,sum=0,max1=-1,max2=-1,index1=-1,index2=-1;

    for(i=0;i<v[node].size();i++)
    {
        pointer=v[node][i];
        if(pointer==parent) continue;
        findRoot(pointer,node);
        fvalues.push_back(make_pair(f[pointer],pointer));
    }

    f[node]=1;

    if(!fvalues.empty())
    {
        for(i=0;i<fvalues.size();i++)
        {
            if(max1!=max(max1,fvalues[i].first))
            {
                index1=i;
                max1=fvalues[i].first;
            }
        }

        fvalues[index1].first=-1;

        for(i=0;i<fvalues.size();i++)
        {
            if(max2!=max(max2,fvalues[i].first))
            {
                index2=i;
                max2=fvalues[i].first;
            }
        }

        if(max2!=-1)
        {
            sum=max1+max2;
            path[node].push_back(fvalues[index1].second);
            path[node].push_back(fvalues[index2].second);
            f[node]=1+max1;
        }

        else
        {
            sum=max1;
            path[node].push_back(fvalues[index1].second);
            f[node]=1+max1;
        }
    }

    if(diameter!=max(diameter,1+sum))
    {
        root=node;
        diameter=1+sum;
    }
}

int main()
{
    int n,i,a,b,j,counter=0,sum=0,temp;
    cin>>n;

    for(i=0;i<n-1;i++)
    {
        cin>>a>>b;
        v[a].push_back(b);
        v[b].push_back(a);
    }

    findRoot(1,1);

    if(path[root].size()==2)
    {
        makePath1(root);
        makePath2(path[root][1]);

        for(i=0;i<r.size();i++)
        l.insert(l.begin(),r[i]);
    }

    else
    makePath1(root);

    if(diameter%2==1)
    {
        midPoint=l[l.size()/2];
        temp=dfs(midPoint,midPoint);
    }

    else
    {
        midPoint=l[l.size()/2];
        temp=dfs(midPoint,midPoint);

        if(temp==-1)
        {
            midPoint=l[(l.size()/2)-1];
            temp=dfs(midPoint,midPoint);
        }
    }

    if(temp==-1)
    cout<<"-1\n";

    else
    {
        for(i=0;i<200006;i++)
        visited[i]=0;

        for(i=0;i<res.size();i++)
        visited[res[i]]=1;

        for(i=0;i<200006;i++)
        {
            if(visited[i]==1)
            counter++;
        }

        if(counter>2)
        cout<<"-1\n";

        else
        {
            for(i=0;i<200006;i++)
            {
                if(visited[i]==1)
                sum=sum+i;
            }

            while(sum%2==0)
            {
                sum=sum/2;
            }

            cout<<sum<<endl;
        }

    }
}


#include<bits/stdc++.h>

using namespace std;
vector<int> v[200005],path[200005],l,r,res;
int diameter=0,root=1,f[200005],midPoint,visited[200005];

int dfs(int node,int parent)
{
    int temp=0,pointer,length=0;
    for(int i=0;i<v[node].size();i++)
    {
        pointer=v[node][i];

        if(pointer==parent) continue;

        temp=dfs(pointer,node);

        if(node==midPoint)
        {
            if(temp==-1)
            return -1;

            else
            res.push_back(temp);
        }

        else
        {
            if(temp==-1)
            return -1;

            else if(length==0)
            length=temp;

            else if(length!=temp)
            return -1;
        }
    }

    return length+1;
}

void makePath1(int root)
{
    while(path[root].size()!=0)
    {
        l.push_back(root);
        root=path[root][0];
    }

    l.push_back(root);
}

void makePath2(int root)
{
    while(path[root].size()!=0)
    {
        r.push_back(root);
        root=path[root][0];
    }

    r.push_back(root);
}

void findRoot(int node,int parent)
{
    vector<pair<int,int> > fvalues;
    int i,pointer,sum=0,max1=-1,max2=-1,max_pointer1=-1,max_pointer2=-1;

    for(i=0;i<v[node].size();i++)
    {
        pointer=v[node][i];
        if(pointer==parent) continue;
        findRoot(pointer,node);
        fvalues.push_back(make_pair(f[pointer],pointer));

        if(fvalues.size()<2)
        {
            if(f[pointer]>max1)
            {
                max_pointer1=pointer;
                max1=f[pointer];
            }
        }

        else
        {
            if(f[pointer]>=max1)
            {
                max2=max1;
                max_pointer2=max_pointer1;

                max1=f[pointer];
                max_pointer1=pointer;
            }

            else if(f[pointer]>=max2 && f[pointer]<max1)
            {
                max2=f[pointer];
                max_pointer2=pointer;
            }
        }
    }

    f[node]=1;

    if(!fvalues.empty())
    {

        if(fvalues.size()>=2)
        {
            sum=max1+max2;
            path[node].push_back(max_pointer1);
            path[node].push_back(max_pointer2);
            f[node]=1+max1;
        }

        else
        {
            sum=max1;
            path[node].push_back(max_pointer1);
            f[node]=1+max1;
        }
    }

    if(diameter!=max(diameter,1+sum))
    {
        root=node;
        diameter=1+sum;
    }
}

int main()
{
    int n,i,a,b,j,counter=0,sum=0,temp;
    cin>>n;

    for(i=0;i<n-1;i++)
    {
        cin>>a>>b;
        v[a].push_back(b);
        v[b].push_back(a);
    }

    findRoot(1,1);

    if(path[root].size()==2)
    {
        makePath1(root);
        makePath2(path[root][1]);

        for(i=0;i<r.size();i++)
        l.insert(l.begin(),r[i]);
    }

    else
    makePath1(root);

    if(diameter%2==1)
    {
        midPoint=l[l.size()/2];
        temp=dfs(midPoint,midPoint);
    }

    else
    {
        midPoint=l[l.size()/2];
        temp=dfs(midPoint,midPoint);

        if(temp==-1)
        {
            midPoint=l[(l.size()/2)-1];
            temp=dfs(midPoint,midPoint);
        }
    }

    if(temp==-1)
    cout<<"-1\n";

    else
    {
        for(i=0;i<200006;i++)
        visited[i]=0;

        for(i=0;i<res.size();i++)
        visited[res[i]]=1;

        for(i=0;i<200006;i++)
        {
            if(visited[i]==1)
            counter++;
        }

        if(counter>2)
        cout<<"-1\n";

        else
        {
            for(i=0;i<200006;i++)
            {
                if(visited[i]==1)
                sum=sum+i;
            }

            while(sum%2==0)
            {
                sum=sum/2;
            }

            cout<<sum<<endl;
        }

    }
}


#include<bits/stdc++.h>

using namespace std;
vector<int> v[200005],res;
int de[200005],da[200005],db[200005],visited[200005],midPoint;

void dfs(int node,int parent,int ct,int* arr)
{
    arr[node]=ct;
    for(int i=0;i<v[node].size();i++)
    {
        if(v[node][i]==parent) continue;
        dfs(v[node][i],node,ct+1,arr);
    }
}

int dfsCheck(int node,int parent)
{
    int temp=0,pointer,length=0;
    for(int i=0;i<v[node].size();i++)
    {
        pointer=v[node][i];

        if(pointer==parent) continue;

        temp=dfsCheck(pointer,node);

        if(node==midPoint)
        {
            if(temp==-1)
            return -1;

            else
            res.push_back(temp);
        }

        else
        {
            if(temp==-1)
            return -1;

            else if(length==0)
            length=temp;

            else if(length!=0 && length!=temp)
            return -1;
        }
    }

    return length+1;
}

int main()
{
    int n,i,a,b,j,counter=0,sum=0,temp,maximum;
    cin>>n;

    for(i=0;i<n-1;i++)
    {
        cin>>a>>b;
        v[a].push_back(b);
        v[b].push_back(a);
    }

    dfs(1,1,0,de);

    for(i=1;i<n+1;i++)
    {
        if(de[i]>maximum)
        {
            maximum=de[i];
            a=i;
        }
    }

    maximum=0;
    dfs(a,a,0,da);

    for(i=0;i<n+1;i++)
    {
        if(da[i]>maximum)
        {
            maximum=da[i];
            b=i;
        }
    }

    dfs(b,b,0,db);

    for(i=0;i<n+1;i++)
    {
        if(da[i]+db[i]==da[b] && abs(da[i]-db[i])<=1)
        break;
    }

    midPoint=i;

    temp=dfsCheck(midPoint,midPoint);

    if(temp==-1)
    cout<<"-1\n";

    else
    {
        for(i=0;i<200006;i++)
        visited[i]=0;

        for(i=0;i<res.size();i++)
        visited[res[i]]=1;

        for(i=0;i<200006;i++)
        {
            if(visited[i]==1)
            counter++;
        }

        if(counter>2)
        cout<<"-1\n";

        else
        {
            for(i=0;i<200006;i++)
            {
                if(visited[i]==1)
                sum=sum+i;
            }

            while(sum%2==0)
            {
                sum=sum/2;
            }

            cout<<sum<<endl;
        }

    }
}


#include<bits/stdc++.h>

using namespace std;
vector<int> v[200005],path[200005],l,r,res;
int diameter=0,root=1,f[200005],midPoint,visited[200005];

int dfs(int node,int parent)
{
    int temp=0,pointer,length=0;
    for(int i=0;i<v[node].size();i++)
    {
        pointer=v[node][i];

        if(pointer==parent) continue;

        temp=dfs(pointer,node);

        if(node==midPoint)
        {
            if(temp==-1)
            return -1;

            else
            res.push_back(temp);
        }

        else
        {
            if(length==0)
            length=temp;

            else if(length!=temp)
            return -1;
        }
    }

    return length+1;
}

void makePath1(int root)
{
    while(path[root].size()!=0)
    {
        l.push_back(root);
        root=path[root][0];
    }

    l.push_back(root);
}

void makePath2(int root)
{
    while(path[root].size()!=0)
    {
        r.push_back(root);
        root=path[root][0];
    }

    r.push_back(root);
}

void findRoot(int node,int parent)
{
    vector<pair<int,int> > fvalues;
    int i,pointer,sum=0;

    for(i=0;i<v[node].size();i++)
    {
        pointer=v[node][i];
        if(pointer==parent) continue;
        findRoot(pointer,node);
        fvalues.push_back(make_pair(f[pointer],pointer));
    }

    f[node]=1;

    if(!fvalues.empty())
    {
        sort(fvalues.begin(),fvalues.end());

        if(fvalues.size()>=2)
        {
            sum=fvalues[fvalues.size()-1].first+ fvalues[fvalues.size()-2].first;
            path[node].push_back(fvalues[fvalues.size()-1].second);
            path[node].push_back(fvalues[fvalues.size()-2].second);
            f[node]=1+fvalues[fvalues.size()-1].first;
        }

        else
        {
            sum=fvalues[fvalues.size()-1].first;
            path[node].push_back(fvalues[fvalues.size()-1].second);
            f[node]=1+fvalues[fvalues.size()-1].first;
        }
    }

    if(diameter!=max(diameter,1+sum))
    {
        root=node;
        diameter=1+sum;
    }
}

int main()
{
    int n,i,a,b,j,counter=0,sum=0;
    cin>>n;

    for(i=0;i<n-1;i++)
    {
        cin>>a>>b;
        v[a].push_back(b);
        v[b].push_back(a);
    }

    findRoot(1,1);

    if(path[root].size()==2)
    {
        makePath1(root);
        makePath2(path[root][1]);

        for(i=0;i<r.size();i++)
        l.insert(l.begin(),r[i]);
    }

    else
    makePath1(root);

    midPoint=l[l.size()/2];

    int temp=dfs(midPoint,midPoint);

    if(temp==-1)
    cout<<"-1\n";

    else
    {
        for(i=0;i<200006;i++)
        visited[i]=0;

        for(i=0;i<res.size();i++)
        visited[res[i]]=1;

        for(i=0;i<200006;i++)
        {
            if(visited[i]==1)
            counter++;
        }

        if(counter>2)
        cout<<"-1\n";

        else
        {
            for(i=0;i<200006;i++)
            {
                if(visited[i]==1)
                sum=sum+i;
            }

            while(sum%2==0)
            {
                sum=sum/2;
            }

            cout<<sum<<endl;
        }

    }
}


#include<bits/stdc++.h>

using namespace std;
vector<int> v[200005],path[200005],l,r,res;
int diameter=0,root=1,f[200005],midPoint,visited[200005];

int dfs(int node,int parent)
{
    int temp=0,pointer,length=0;
    for(int i=0;i<v[node].size();i++)
    {
        pointer=v[node][i];

        if(pointer==parent) continue;

        temp=dfs(pointer,node);

        if(node==midPoint)
        {
            if(temp==-1)
            return -1;

            else
            res.push_back(temp);
        }

        else
        {
            if(temp==-1)
            return -1;

            else if(length==0)
            length=temp;

            else if(length!=temp)
            return -1;
        }
    }

    return length+1;
}

void makePath1(int root)
{
    while(path[root].size()!=0)
    {
        l.push_back(root);
        root=path[root][0];
    }

    l.push_back(root);
}

void makePath2(int root)
{
    while(path[root].size()!=0)
    {
        r.push_back(root);
        root=path[root][0];
    }

    r.push_back(root);
}

void findRoot(int node,int parent)
{
    vector<pair<int,int> > fvalues;
    int i,pointer,sum=0,max1=-1,max2=-1,max_pointer1=-1,max_pointer2=-1;

    for(i=0;i<v[node].size();i++)
    {
        pointer=v[node][i];
        if(pointer==parent) continue;
        findRoot(pointer,node);
        fvalues.push_back(make_pair(f[pointer],pointer));

        if(fvalues.size()<2)
        {
            if(f[pointer]>max1)
            {
                max_pointer1=pointer;
                max1=f[pointer];
            }
        }

        else
        {
            if(f[pointer]>max1)
            {
                max2=max1;
                max_pointer2=max_pointer1;

                max1=f[pointer];
                max_pointer1=pointer;
            }

            else if(f[pointer]>max2 && f[pointer]<max1)
            {
                max2=f[pointer];
                max_pointer2=pointer;
            }
        }
    }

    f[node]=1;

    if(!fvalues.empty())
    {

        if(fvalues.size()>=2)
        {
            sum=max1+max2;
            path[node].push_back(max_pointer1);
            path[node].push_back(max_pointer2);
            f[node]=1+max1;
        }

        else
        {
            sum=max1;
            path[node].push_back(max_pointer1);
            f[node]=1+max1;
        }
    }

    if(diameter!=max(diameter,1+sum))
    {
        root=node;
        diameter=1+sum;
    }
}

int main()
{
    int n,i,a,b,j,counter=0,sum=0,temp;
    cin>>n;

    for(i=0;i<n-1;i++)
    {
        cin>>a>>b;
        v[a].push_back(b);
        v[b].push_back(a);
    }

    findRoot(1,1);

    if(path[root].size()==2)
    {
        makePath1(root);
        makePath2(path[root][1]);

        for(i=0;i<r.size();i++)
        l.insert(l.begin(),r[i]);
    }

    else
    makePath1(root);

    if(diameter%2==1)
    {
        midPoint=l[l.size()/2];
        temp=dfs(midPoint,midPoint);
    }

    else
    {
        midPoint=l[l.size()/2];
        temp=dfs(midPoint,midPoint);

        if(temp==-1)
        {
            midPoint=l[(l.size()/2)-1];
            temp=dfs(midPoint,midPoint);
        }
    }

    if(temp==-1)
    cout<<"-1\n";

    else
    {
        for(i=0;i<200006;i++)
        visited[i]=0;

        for(i=0;i<res.size();i++)
        visited[res[i]]=1;

        for(i=0;i<200006;i++)
        {
            if(visited[i]==1)
            counter++;
        }

        if(counter>2)
        cout<<"-1\n";

        else
        {
            for(i=0;i<200006;i++)
            {
                if(visited[i]==1)
                sum=sum+i;
            }

            while(sum%2==0)
            {
                sum=sum/2;
            }

            cout<<sum<<endl;
        }

    }
}


#include<bits/stdc++.h>

using namespace std;
vector<int> v[200005],path[200005],l,r,res;
int diameter=0,root=1,f[200005],midPoint,visited[200005];

int dfs(int node,int parent)
{
    int temp=0,pointer,length=0;
    for(int i=0;i<v[node].size();i++)
    {
        pointer=v[node][i];

        if(pointer==parent) continue;

        temp=dfs(pointer,node);

        if(node==midPoint)
        {
            if(temp==-1)
            return -1;

            else
            res.push_back(temp);
        }

        else
        {
            if(length==0)
            length=temp;

            else if(length!=temp)
            return -1;
        }
    }

    return length+1;
}

void makePath1(int root)
{
    while(path[root].size()!=0)
    {
        l.push_back(root);
        root=path[root][0];
    }

    l.push_back(root);
}

void makePath2(int root)
{
    while(path[root].size()!=0)
    {
        r.push_back(root);
        root=path[root][0];
    }

    r.push_back(root);
}

void findRoot(int node,int parent)
{
    vector<pair<int,int> > fvalues;
    int i,pointer,sum=0;

    for(i=0;i<v[node].size();i++)
    {
        pointer=v[node][i];
        if(pointer==parent) continue;
        findRoot(pointer,node);
        fvalues.push_back(make_pair(f[pointer],pointer));
    }

    f[node]=1;

    if(!fvalues.empty())
    {
        sort(fvalues.begin(),fvalues.end());

        if(fvalues.size()>=2)
        {
            sum=fvalues[fvalues.size()-1].first+ fvalues[fvalues.size()-2].first;
            path[node].push_back(fvalues[fvalues.size()-1].second);
            path[node].push_back(fvalues[fvalues.size()-2].second);
            f[node]=1+fvalues[fvalues.size()-1].first;
        }

        else
        {
            sum=fvalues[fvalues.size()-1].first;
            path[node].push_back(fvalues[fvalues.size()-1].second);
            f[node]=1+fvalues[fvalues.size()-1].first;
        }
    }

    if(diameter!=max(diameter,1+sum))
    {
        root=node;
        diameter=1+sum;
    }
}

int main()
{
    int n,i,a,b,j,counter=0,sum=0;
    cin>>n;

    for(i=0;i<n-1;i++)
    {
        cin>>a>>b;
        v[a].pb(b);
        v[b].pb(a);
    }

    findRoot(1,1);

    if(path[root].size()==2)
    {
        makePath1(root);
        makePath2(path[root][1]);

        for(i=0;i<r.size();i++)
        l.insert(l.begin(),r[i]);
    }

    else
    makePath1(root);

    midPoint=l[l.size()/2];

    int temp=dfs(midPoint,midPoint);

    if(temp==-1)
    cout<<"-1\n";

    else
    {
        for(i=0;i<res.size();i++)
        visited[res[i]]=1;

        for(i=0;i<200006;i++)
        {
            if(visited[i]==1)
            counter++;
        }

        if(counter>2)
        cout<<"-1\n";

        else
        {
            for(i=0;i<200006;i++)
            {
                if(visited[i])
                sum=sum+i;
            }

            while(sum%2==0)
            {
                sum=sum/2;
            }

            cout<<sum<<endl;
        }

    }
}


#include<bits/stdc++.h>

using namespace std;
vector<int> v[200005],path[200005],l,r,res;
int diameter=0,root=1,f[200005],midPoint,visited[200005];

int dfs(int node,int parent)
{
    int temp=0,pointer,length=0;
    for(int i=0;i<v[node].size();i++)
    {
        pointer=v[node][i];

        if(pointer==parent) continue;

        temp=dfs(pointer,node);

        if(node==midPoint)
        {
            if(temp==-1)
            return -1;

            else
            res.push_back(temp);
        }

        else
        {
            if(length==0)
            length=temp;

            else if(length!=temp)
            return -1;
        }
    }

    return length+1;
}

void makePath1(int root)
{
    while(path[root].size()!=0)
    {
        l.push_back(root);
        root=path[root][0];
    }

    l.push_back(root);
}

void makePath2(int root)
{
    while(path[root].size()!=0)
    {
        r.push_back(root);
        root=path[root][0];
    }

    r.push_back(root);
}

void findRoot(int node,int parent)
{
    vector<pair<int,int> > fvalues;
    int i,pointer,sum=0;

    for(i=0;i<v[node].size();i++)
    {
        pointer=v[node][i];
        if(pointer==parent) continue;
        findRoot(pointer,node);
        fvalues.push_back(make_pair(f[pointer],pointer));
    }

    f[node]=1;

    if(!fvalues.empty())
    {
        sort(fvalues.begin(),fvalues.end());

        if(fvalues.size()>=2)
        {
            sum=fvalues[fvalues.size()-1].first+ fvalues[fvalues.size()-2].first;
            path[node].push_back(fvalues[fvalues.size()-1].second);
            path[node].push_back(fvalues[fvalues.size()-2].second);
            f[node]=1+fvalues[fvalues.size()-1].first;
        }

        else
        {
            sum=fvalues[fvalues.size()-1].first;
            path[node].push_back(fvalues[fvalues.size()-1].second);
            f[node]=1+fvalues[fvalues.size()-1].first;
        }
    }

    if(diameter!=max(diameter,1+sum))
    {
        root=node;
        diameter=1+sum;
    }
}

int main()
{
    int n,i,a,b,j,counter=0,sum=0;
    cin>>n;

    for(i=0;i<n-1;i++)
    {
        cin>>a>>b;
        v[a].push_back(b);
        v[b].push_back(a);
    }

    findRoot(1,1);

    if(path[root].size()==2)
    {
        makePath1(root);
        makePath2(path[root][1]);

        for(i=0;i<r.size();i++)
        l.insert(l.begin(),r[i]);
    }

    else
    makePath1(root);

    midPoint=l[l.size()/2];

    int temp=dfs(midPoint,midPoint);

    for(i=0;i<res.size();i++)
    cout<<res[i];

    if(temp==-1)
    cout<<"-1\n";

    else
    {
        for(i=0;i<res.size();i++)
        visited[res[i]]=1;

        for(i=0;i<200006;i++)
        {
            if(visited[i]==1)
            counter++;
        }

        cout<<counter<<endl;

        for(i=0;i<4;i++)
        cout<<visited[i];

        if(counter>2)
        cout<<"-1\n";

        else
        {
            for(i=0;i<200006;i++)
            {
                if(visited[i])
                sum=sum+i;
            }

            while(sum%2==0)
            {
                sum=sum/2;
            }

            //cout<<sum<<endl;
        }

    }
}


#include<bits/stdc++.h>
#define  pb push_back
#define ins insert
#define mp make_pair

using namespace std;
vector<int> v[200005],path[200005],l,r,res;
int diameter=0,root=1,f[200005],midPoint,visited[200005];

int dfs(int node,int parent)
{
    int temp=0,pointer,length=0;
    for(int i=0;i<v[node].size();i++)
    {
        pointer=v[node][i];

        if(pointer==parent) continue;

        temp=dfs(pointer,node);

        if(node==midPoint)
        {
            if(temp==-1)
            return -1;

            else
            res.pb(temp);
        }

        else
        {
            if(length==0)
            length=temp;

            else if(length!=temp)
            return -1;
        }
    }

    return length+1;
}

void makePath1(int root)
{
    while(path[root].size()!=0)
    {
        l.pb(root);
        root=path[root][0];
    }

    l.pb(root);
}

void makePath2(int root)
{
    while(path[root].size()!=0)
    {
        r.pb(root);
        root=path[root][0];
    }

    r.pb(root);
}

void findRoot(int node,int parent)
{
    vector<pair<int,int> > fvalues;
    int i,pointer,sum=0;

    for(i=0;i<v[node].size();i++)
    {
        pointer=v[node][i];
        if(pointer==parent) continue;
        findRoot(pointer,node);
        fvalues.pb(mp(f[pointer],pointer));
    }

    f[node]=1;

    if(!fvalues.empty())
    {
        sort(fvalues.begin(),fvalues.end());

        if(fvalues.size()>=2)
        {
            sum=fvalues[fvalues.size()-1].first+ fvalues[fvalues.size()-2].first;
            path[node].pb(fvalues[fvalues.size()-1].second);
            path[node].pb(fvalues[fvalues.size()-2].second);
            f[node]=1+fvalues[fvalues.size()-1].first;
        }

        else
        {
            sum=fvalues[fvalues.size()-1].first;
            path[node].pb(fvalues[fvalues.size()-1].second);
            f[node]=1+fvalues[fvalues.size()-1].first;
        }
    }

    if(diameter!=max(diameter,1+sum))
    {
        root=node;
        diameter=1+sum;
    }
}

int main()
{
    int n,i,a,b,j,counter=0,sum=0;
    cin>>n;

    for(i=0;i<n-1;i++)
    {
        cin>>a>>b;
        v[a].pb(b);
        v[b].pb(a);
    }

    findRoot(1,1);

    if(path[root].size()==2)
    {
        makePath1(root);
        makePath2(path[root][1]);

        for(i=0;i<r.size();i++)
        l.ins(l.begin(),r[i]);
    }

    else
    makePath1(root);

    midPoint=l[l.size()/2];

    int temp=dfs(midPoint,midPoint);

    if(temp==-1)
    cout<<"-1\n";

    else
    {
        for(i=0;i<res.size();i++)
        visited[res[i]]=1;

        for(i=0;i<200006;i++)
        {
            if(visited[i]==1)
            counter++;
        }

        if(counter>2)
        cout<<"-1\n";

        else
        {
            for(i=0;i<200006;i++)
            {
                if(visited[i])
                sum=sum+i;
            }
        }

        cout<<sum<<endl;
    }
}


#include<bits/stdc++.h>

using namespace std;
vector<int> v[200005],path[200005],l,r,res;
int diameter=0,root=1,f[200005],midPoint,visited[200005];

int dfs(int node,int parent)
{
    int temp=0,pointer,length=0;
    for(int i=0;i<v[node].size();i++)
    {
        pointer=v[node][i];

        if(pointer==parent) continue;

        temp=dfs(pointer,node);

        if(node==midPoint)
        {
            if(temp==-1)
            return -1;

            else
            res.push_back(temp);
        }

        else
        {
            if(length==0)
            length=temp;

            else if(length!=temp)
            return -1;
        }
    }

    return length+1;
}

void makePath1(int root)
{
    while(path[root].size()!=0)
    {
        l.push_back(root);
        root=path[root][0];
    }

    l.push_back(root);
}

void makePath2(int root)
{
    while(path[root].size()!=0)
    {
        r.push_back(root);
        root=path[root][0];
    }

    r.push_back(root);
}

void findRoot(int node,int parent)
{
    vector<pair<int,int> > fvalues;
    int i,pointer,sum=0;

    for(i=0;i<v[node].size();i++)
    {
        pointer=v[node][i];
        if(pointer==parent) continue;
        findRoot(pointer,node);
        fvalues.push_back(make_pair(f[pointer],pointer));
    }

    f[node]=1;

    if(!fvalues.empty())
    {
        sort(fvalues.begin(),fvalues.end());

        if(fvalues.size()>=2)
        {
            sum=fvalues[fvalues.size()-1].first+ fvalues[fvalues.size()-2].first;
            path[node].push_back(fvalues[fvalues.size()-1].second);
            path[node].push_back(fvalues[fvalues.size()-2].second);
            f[node]=1+fvalues[fvalues.size()-1].first;
        }

        else
        {
            sum=fvalues[fvalues.size()-1].first;
            path[node].push_back(fvalues[fvalues.size()-1].second);
            f[node]=1+fvalues[fvalues.size()-1].first;
        }
    }

    if(diameter!=max(diameter,1+sum))
    {
        root=node;
        diameter=1+sum;
    }
}

int main()
{
    int n,i,a,b,j,counter=0,sum=0;
    cin>>n;

    for(i=0;i<n-1;i++)
    {
        cin>>a>>b;
        v[a].push_back(b);
        v[b].push_back(a);
    }

    findRoot(1,1);

    if(path[root].size()==2)
    {
        makePath1(root);
        makePath2(path[root][1]);

        for(i=0;i<r.size();i++)
        l.insert(l.begin(),r[i]);
    }

    else
    makePath1(root);

    midPoint=l[l.size()/2];

    int temp=dfs(midPoint,midPoint);

    if(temp==-1)
    cout<<"-1\n";

    else
    {
        for(i=0;i<res.size();i++)
        visited[res[i]]=1;

        for(i=0;i<200006;i++)
        {
            if(visited[i]==1)
            counter++;
        }

        if(counter>2)
        cout<<"-1\n";

        else
        {
            for(i=0;i<200006;i++)
            {
                if(visited[i])
                sum=sum+i;
            }

            while(sum%2==0)
            {
                sum=sum/2;
            }

            cout<<sum<<endl;
        }

    }
}


#include<bits/stdc++.h>

using namespace std;
vector<int> v[200005],path[200005],l,r,res;
int diameter=0,root=1,f[200005],midPoint,visited[200005];

int dfs(int node,int parent)
{
    int temp=0,pointer,length=0;
    for(int i=0;i<v[node].size();i++)
    {
        pointer=v[node][i];

        if(pointer==parent) continue;

        temp=dfs(pointer,node);

        if(node==midPoint)
        {
            if(temp==-1)
            return -1;

            else
            res.push_back(temp);
        }

        else
        {
            if(temp==-1)
            return -1;

            else if(length==0)
            length=temp;

            else if(length!=temp)
            return -1;
        }
    }

    return length+1;
}

void makePath1(int root)
{
    while(path[root].size()!=0)
    {
        l.push_back(root);
        root=path[root][0];
    }

    l.push_back(root);
}

void makePath2(int root)
{
    while(path[root].size()!=0)
    {
        r.push_back(root);
        root=path[root][0];
    }

    r.push_back(root);
}

void findRoot(int node,int parent)
{
    vector<pair<int,int> > fvalues;
    int i,pointer,sum=0,max1=-1,max2=-1,index1=-1,index2=-1;

    for(i=0;i<v[node].size();i++)
    {
        pointer=v[node][i];
        if(pointer==parent) continue;
        findRoot(pointer,node);
        fvalues.push_back(make_pair(f[pointer],pointer));

        if(fvalues.size()<2)
        {
            if(max1!=max(max1,f[pointer]))
            {
                index1=fvalues.size()-1;
                max1=f[pointer];
            }
        }

        else
        {
            if(f[pointer]>max1)
            {
                max2=max1;
                index2=index1;

                max1=f[pointer];
                index1=fvalues.size()-1;
            }

            else if(f[pointer]>max2 && f[pointer]<max1)
            {
                max2=f[pointer];
                index2=fvalues.size()-1;
            }
        }
    }

    f[node]=1;

    if(!fvalues.empty())
    {

        if(max2!=-1)
        {
            sum=max1+max2;
            path[node].push_back(fvalues[index1].second);
            path[node].push_back(fvalues[index2].second);
            f[node]=1+max1;
        }

        else
        {
            sum=max1;
            path[node].push_back(fvalues[index1].second);
            f[node]=1+max1;
        }
    }

    if(diameter!=max(diameter,1+sum))
    {
        root=node;
        diameter=1+sum;
    }
}

int main()
{
    int n,i,a,b,j,counter=0,sum=0,temp;
    cin>>n;

    for(i=0;i<n-1;i++)
    {
        cin>>a>>b;
        v[a].push_back(b);
        v[b].push_back(a);
    }

    findRoot(1,1);

    if(path[root].size()==2)
    {
        makePath1(root);
        makePath2(path[root][1]);

        for(i=0;i<r.size();i++)
        l.insert(l.begin(),r[i]);
    }

    else
    makePath1(root);

    if(diameter%2==1)
    {
        midPoint=l[l.size()/2];
        temp=dfs(midPoint,midPoint);
    }

    else
    {
        midPoint=l[l.size()/2];
        temp=dfs(midPoint,midPoint);

        if(temp==-1)
        {
            midPoint=l[(l.size()/2)-1];
            temp=dfs(midPoint,midPoint);
        }
    }

    if(temp==-1)
    cout<<"-1\n";

    else
    {
        for(i=0;i<200006;i++)
        visited[i]=0;

        for(i=0;i<res.size();i++)
        visited[res[i]]=1;

        for(i=0;i<200006;i++)
        {
            if(visited[i]==1)
            counter++;
        }

        if(counter>2)
        cout<<"-1\n";

        else
        {
            for(i=0;i<200006;i++)
            {
                if(visited[i]==1)
                sum=sum+i;
            }

            while(sum%2==0)
            {
                sum=sum/2;
            }

            cout<<sum<<endl;
        }

    }
}


#include<bits/stdc++.h>

using namespace std;
vector<int> v[200005],path[200005],l,r,res;
int diameter=0,root=1,f[200005],midPoint,visited[200005];

int dfs(int node,int parent)
{
    int temp=0,pointer,length=0;
    for(int i=0;i<v[node].size();i++)
    {
        pointer=v[node][i];

        if(pointer==parent) continue;

        temp=dfs(pointer,node);

        if(node==midPoint)
        {
            if(temp==-1)
            return -1;

            else
            res.push_back(temp);
        }

        else
        {
            if(temp==-1)
            return -1;

            else if(length==0)
            length=temp;

            else if(length!=0 && length!=temp)
            return -1;
        }
    }

    return length+1;
}

void makePath1(int root)
{
    while(path[root].size()!=0)
    {
        l.push_back(root);
        root=path[root][0];
    }

    l.push_back(root);
}

void makePath2(int root)
{
    while(path[root].size()!=0)
    {
        r.push_back(root);
        root=path[root][0];
    }

    r.push_back(root);
}

void findRoot(int node,int parent)
{
    vector<pair<int,int> > fvalues;
    int i,pointer,sum=0,max1=-1,max2=-1,max_pointer1=-1,max_pointer2=-1;

    for(i=0;i<v[node].size();i++)
    {
        pointer=v[node][i];
        if(pointer==parent) continue;
        findRoot(pointer,node);
        fvalues.push_back(make_pair(f[pointer],pointer));

        if(fvalues.size()<2)
        {
            if(f[pointer]>max1)
            {
                max_pointer1=pointer;
                max1=f[pointer];
            }
        }

        else
        {
            if(f[pointer]>=max1)
            {
                max2=max1;
                max_pointer2=max_pointer1;

                max1=f[pointer];
                max_pointer1=pointer;
            }

            else if(f[pointer]>=max2 && f[pointer]<max1)
            {
                max2=f[pointer];
                max_pointer2=pointer;
            }
        }
    }

    f[node]=1;

    if(!fvalues.empty())
    {

        if(fvalues.size()>=2)
        {
            sum=max1+max2;
            path[node].push_back(max_pointer1);
            path[node].push_back(max_pointer2);
            f[node]=1+max1;
        }

        else
        {
            sum=max1;
            path[node].push_back(max_pointer1);
            f[node]=1+max1;
        }
    }

    if(diameter!=max(diameter,1+sum))
    {
        root=node;
        diameter=1+sum;
    }
}

int main()
{
    int n,i,a,b,j,counter=0,sum=0,temp;
    cin>>n;

    for(i=0;i<n-1;i++)
    {
        cin>>a>>b;
        v[a].push_back(b);
        v[b].push_back(a);
    }

    findRoot(1,1);

    if(path[root].size()==2)
    {
        makePath1(root);
        makePath2(path[root][1]);

        for(i=0;i<r.size();i++)
        l.insert(l.begin(),r[i]);
    }

    else
    makePath1(root);

    //if(diameter%2==1)
    {
        midPoint=l[l.size()/2];
        temp=dfs(midPoint,midPoint);
    }

    /*else
    {
        midPoint=l[l.size()/2];
        temp=dfs(midPoint,midPoint);

        if(temp==-1)
        {
            midPoint=l[(l.size()/2)-1];
            temp=dfs(midPoint,midPoint);
        }
    }*/

    if(temp==-1)
    cout<<"-1\n";

    else
    {
        for(i=0;i<200006;i++)
        visited[i]=0;

        for(i=0;i<res.size();i++)
        visited[res[i]]=1;

        for(i=0;i<200006;i++)
        {
            if(visited[i]==1)
            counter++;
        }

        if(counter>2)
        cout<<"-1\n";

        else
        {
            for(i=0;i<200006;i++)
            {
                if(visited[i]==1)
                sum=sum+i;
            }

            while(sum%2==0)
            {
                sum=sum/2;
            }

            cout<<sum<<endl;
        }

    }
}


#include<bits/stdc++.h>

using namespace std;
vector<int> v[200005],path[200005],l,r,res;
int diameter=0,root=1,f[200005],midPoint,visited[200005];

int dfs(int node,int parent)
{
    int temp=0,pointer,length=0;
    for(int i=0;i<v[node].size();i++)
    {
        pointer=v[node][i];

        if(pointer==parent) continue;

        temp=dfs(pointer,node);

        if(node==midPoint)
        {
            if(temp==-1)
            return -1;

            else
            res.push_back(temp);
        }

        else
        {
            if(length==0)
            length=temp;

            else if(length!=temp)
            return -1;
        }
    }

    return length+1;
}

void makePath1(int root)
{
    while(path[root].size()!=0)
    {
        l.push_back(root);
        root=path[root][0];
    }

    l.push_back(root);
}

void makePath2(int root)
{
    while(path[root].size()!=0)
    {
        r.push_back(root);
        root=path[root][0];
    }

    r.push_back(root);
}

void findRoot(int node,int parent)
{
    vector<pair<int,int> > fvalues;
    int i,pointer,sum=0;

    for(i=0;i<v[node].size();i++)
    {
        pointer=v[node][i];
        if(pointer==parent) continue;
        findRoot(pointer,node);
        fvalues.push_back(make_pair(f[pointer],pointer));
    }

    f[node]=1;

    if(!fvalues.empty())
    {
        sort(fvalues.begin(),fvalues.end());

        if(fvalues.size()>=2)
        {
            sum=fvalues[fvalues.size()-1].first+ fvalues[fvalues.size()-2].first;
            path[node].push_back(fvalues[fvalues.size()-1].second);
            path[node].push_back(fvalues[fvalues.size()-2].second);
            f[node]=1+fvalues[fvalues.size()-1].first;
        }

        else
        {
            sum=fvalues[fvalues.size()-1].first;
            path[node].push_back(fvalues[fvalues.size()-1].second);
            f[node]=1+fvalues[fvalues.size()-1].first;
        }
    }

    if(diameter!=max(diameter,1+sum))
    {
        root=node;
        diameter=1+sum;
    }
}

int main()
{
    int n,i,a,b,j,counter=0,sum=0,temp;
    cin>>n;

    for(i=0;i<n-1;i++)
    {
        cin>>a>>b;
        v[a].push_back(b);
        v[b].push_back(a);
    }

    findRoot(1,1);

    if(path[root].size()==2)
    {
        makePath1(root);
        makePath2(path[root][1]);

        for(i=0;i<r.size();i++)
        l.insert(l.begin(),r[i]);
    }

    else
    makePath1(root);

    if(diameter%2==1)
    {
        midPoint=l[l.size()/2];
        temp=dfs(midPoint,midPoint);
    }

    else
    {
        midPoint=l[l.size()/2];
        temp=dfs(midPoint,midPoint);

        if(temp==-1)
        {
            midPoint=l[(l.size()/2)-1];
            temp=dfs(midPoint,midPoint);
        }
    }

    if(temp==-1)
    cout<<"-1\n";

    else
    {
        for(i=0;i<200006;i++)
        visited[i]=0;

        for(i=0;i<res.size();i++)
        visited[res[i]]=1;

        for(i=0;i<200006;i++)
        {
            if(visited[i]==1)
            counter++;
        }

        if(counter>2)
        cout<<"-1\n";

        else
        {
            for(i=0;i<200006;i++)
            {
                if(visited[i]==1)
                sum=sum+i;
            }

            while(sum%2==0)
            {
                sum=sum/2;
            }

            cout<<sum<<endl;
        }

    }
}


#include<bits/stdc++.h>

using namespace std;
vector<int> v[200005],path[200005],l,r,res;
int diameter=0,root=1,f[200005],midPoint,visited[200005];

int dfs(int node,int parent)
{
    int temp=0,pointer,length=0;
    for(int i=0;i<v[node].size();i++)
    {
        pointer=v[node][i];

        if(pointer==parent) continue;

        temp=dfs(pointer,node);

        if(node==midPoint)
        {
            if(temp==-1)
            return -1;

            else
            res.push_back(temp);
        }

        else
        {
            if(temp==-1)
            return -1;

            else if(length==0)
            length=temp;

            else if(length!=temp)
            return -1;
        }
    }

    return length+1;
}

void makePath1(int root)
{
    while(path[root].size()!=0)
    {
        l.push_back(root);
        root=path[root][0];
    }

    l.push_back(root);
}

void makePath2(int root)
{
    while(path[root].size()!=0)
    {
        r.push_back(root);
        root=path[root][0];
    }

    r.push_back(root);
}

void findRoot(int node,int parent)
{
    vector<pair<int,int> > fvalues;
    int i,pointer,sum=0,max1=-1,max2=-1,max_pointer1=-1,max_pointer2=-1;

    for(i=0;i<v[node].size();i++)
    {
        pointer=v[node][i];
        if(pointer==parent) continue;
        findRoot(pointer,node);
        fvalues.push_back(make_pair(f[pointer],pointer));

        if(fvalues.size()<2)
        {
            if(max1!=max(max1,f[pointer]))
            {
                max_pointer1=pointer;
                max1=f[pointer];
            }
        }

        else
        {
            if(f[pointer]>max1)
            {
                max2=max1;
                max_pointer2=max_pointer1;

                max1=f[pointer];
                max_pointer1=pointer;
            }

            else if(f[pointer]>max2 && f[pointer]<max1)
            {
                max2=f[pointer];
                max_pointer2=pointer;
            }
        }
    }

    f[node]=1;

    if(!fvalues.empty())
    {

        if(max2!=-1)
        {
            sum=max1+max2;
            path[node].push_back(max_pointer1);
            path[node].push_back(max_pointer2);
            f[node]=1+max1;
        }

        else
        {
            sum=max1;
            path[node].push_back(max_pointer1);
            f[node]=1+max1;
        }
    }

    if(diameter!=max(diameter,1+sum))
    {
        root=node;
        diameter=1+sum;
    }
}

int main()
{
    int n,i,a,b,j,counter=0,sum=0,temp;
    cin>>n;

    for(i=0;i<n-1;i++)
    {
        cin>>a>>b;
        v[a].push_back(b);
        v[b].push_back(a);
    }

    findRoot(1,1);

    if(path[root].size()==2)
    {
        makePath1(root);
        makePath2(path[root][1]);

        for(i=0;i<r.size();i++)
        l.insert(l.begin(),r[i]);
    }

    else
    makePath1(root);

    if(diameter%2==1)
    {
        midPoint=l[l.size()/2];
        temp=dfs(midPoint,midPoint);
    }

    else
    {
        midPoint=l[l.size()/2];
        temp=dfs(midPoint,midPoint);

        if(temp==-1)
        {
            midPoint=l[(l.size()/2)-1];
            temp=dfs(midPoint,midPoint);
        }
    }

    if(temp==-1)
    cout<<"-1\n";

    else
    {
        for(i=0;i<200006;i++)
        visited[i]=0;

        for(i=0;i<res.size();i++)
        visited[res[i]]=1;

        for(i=0;i<200006;i++)
        {
            if(visited[i]==1)
            counter++;
        }

        if(counter>2)
        cout<<"-1\n";

        else
        {
            for(i=0;i<200006;i++)
            {
                if(visited[i]==1)
                sum=sum+i;
            }

            while(sum%2==0)
            {
                sum=sum/2;
            }

            cout<<sum<<endl;
        }

    }
}


#include<bits/stdc++.h>

using namespace std;

int length[200005],flag=0,visited[200005],node;
vector<pair<int,int> > v[200005];
vector<int> res;

int dfs(int root)
{
    visited[root]=1;
    int i,temp=0,length_root=0;

        for(i=0;i<length[root];i++)
        {
            if(!visited[v[root][i].second])
            {
                temp=dfs(v[root][i].second);

                if(root!=node)
                {
                    if(temp==-1)
                    return -1;

                    else if(length_root==0)
                    length_root=temp;

                    else if(length_root!=0 && temp!=length_root)
                    return -1;

                }

                else
                {
                    res.push_back(temp);

                    if(temp==-1)
                    return -1;
                }

            }

        }
         return length_root+1;

}

int main()
{
    int n,i,a,b,j,counter=0,sum=0;
    cin>>n;

    for(i=1;i<=n-1;i++)
    {
        cin>>a>>b;

        length[a]++;
        length[b]++;

        v[a].push_back(make_pair(a,b));
        v[b].push_back(make_pair(b,a));
    }

    for(i=1;i<n+1;i++)
    {
        res.clear();

        for(j=0;j<200006;j++)
        visited[j]=0;

        node=i;
        int temp=dfs(i);

        if(temp!=-1)
        {
            flag=1;
            break;
        }
    }

    if(flag==0)
    cout<<"-1\n";

    else
    {
       for(i=0;i<200006;i++)
       visited[i]=0;

       for(i=0;i<res.size();i++)
       {
            if(!visited[res[i]])
            visited[res[i]]=1;
       }

       for(i=0;i<200006;i++)
       {
           if(visited[i]==1)
           counter++;
       }

       if(counter>2)
       cout<<"-1\n";

       else
       {
           for(i=0;i<200006;i++)
           {
               if(visited[i]==1)
               sum=sum+i;
           }

           if(sum%2==0)
           cout<<sum/2;

           else
           cout<<sum;
       }
    }

}


#include<bits/stdc++.h>

using namespace std;

int length[200005],flag=0,visited[200005],node;
vector<pair<int,int> > v[200005];
vector<int> res;

int dfs(int root)
{
    visited[root]=1;
    int i,temp=0,length_root=0;

        for(i=0;i<length[root];i++)
        {
            if(!visited[v[root][i].second])
            {
                temp=dfs(v[root][i].second);

                if(root!=node)
                {
                    if(temp==-1)
                    return -1;

                    else if(length_root==0)
                    length_root=temp;

                    else if(length_root!=0 && temp!=length_root)
                    return -1;

                }

                else
                {
                    res.push_back(temp);

                    if(temp==-1)
                    return -1;
                }

            }

        }
         return length_root+1;

}

int main()
{
    int n,i,a,b,j,counter=0,sum=0;
    cin>>n;

    for(i=1;i<=n-1;i++)
    {
        cin>>a>>b;

        length[a]++;
        length[b]++;

        v[a].push_back(make_pair(a,b));
        v[b].push_back(make_pair(b,a));
    }

    for(i=1;i<n+1;i++)
    {
        res.clear();

        for(j=0;j<200006;j++)
        visited[j]=0;

        node=i;
        int temp=dfs(i);

        if(temp!=-1)
        {
            flag=1;
            break;
        }
    }

    if(flag==0)
    cout<<"-1\n";

    else
    {
       for(i=0;i<200006;i++)
       visited[i]=0;

       for(i=0;i<res.size();i++)
       {
            if(!visited[res[i]])
            visited[res[i]]=1;
       }

       for(i=0;i<200006;i++)
       {
           if(visited[i]==1)
           counter++;
       }

       if(counter>2)
       cout<<"-1\n";

       else
       {
           for(i=0;i<200006;i++)
           {
               if(visited[i]==1)
               sum=sum+i;
           }

            while(1)
            {
                if(sum%2!=0)
                break;

                else
                sum=sum/2;
            }

            cout<<sum;
       }
    }

}


#include<bits/stdc++.h>

using namespace std;
vector<int> v[200005],path[200005],l,r,res;
int diameter=0,root=1,f[200005],midPoint,visited[200005];

int dfs(int node,int parent)
{
    int temp=0,pointer,length=0;
    for(int i=0;i<v[node].size();i++)
    {
        pointer=v[node][i];

        if(pointer==parent) continue;

        temp=dfs(pointer,node);

        if(node==midPoint)
        {
            if(temp==-1)
            return -1;

            else
            res.push_back(temp);
        }

        else
        {
            if(temp==-1)
            return -1;

            else if(length==0)
            length=temp;

            else if(length!=0 && length!=temp)
            return -1;
        }
    }

    return length+1;
}

void makePath1(int root)
{
    while(path[root].size()!=0)
    {
        l.push_back(root);
        root=path[root][0];
    }

    l.push_back(root);
}

void makePath2(int root)
{
    while(path[root].size()!=0)
    {
        r.push_back(root);
        root=path[root][0];
    }

    r.push_back(root);
}

void findRoot(int node,int parent)
{
    vector<pair<int,int> > fvalues;
    int i,pointer,sum=0,max1=-1,max2=-1,max_pointer1=-1,max_pointer2=-1;

    for(i=0;i<v[node].size();i++)
    {
        pointer=v[node][i];
        if(pointer==parent) continue;
        findRoot(pointer,node);
        fvalues.push_back(make_pair(f[pointer],pointer));

        if(fvalues.size()<2)
        {
            if(f[pointer]>max1)
            {
                max_pointer1=pointer;
                max1=f[pointer];
            }
        }

        else
        {
            if(f[pointer]>=max1)
            {
                max2=max1;
                max_pointer2=max_pointer1;

                max1=f[pointer];
                max_pointer1=pointer;
            }

            else if(f[pointer]>=max2 && f[pointer]<max1)
            {
                max2=f[pointer];
                max_pointer2=pointer;
            }
        }
    }

    f[node]=1;

    if(!fvalues.empty())
    {

        if(fvalues.size()>=2)
        {
            sum=max1+max2;
            path[node].push_back(max_pointer1);
            path[node].push_back(max_pointer2);
            f[node]=1+max1;
        }

        else
        {
            sum=max1;
            path[node].push_back(max_pointer1);
            f[node]=1+max1;
        }
    }

    if(diameter!=max(diameter,1+sum))
    {
        root=node;
        diameter=1+sum;
    }
}

int main()
{
    int n,i,a,b,j,counter=0,sum=0,temp;
    cin>>n;

    for(i=0;i<n-1;i++)
    {
        cin>>a>>b;
        v[a].push_back(b);
        v[b].push_back(a);
    }

    findRoot(1,1);

    if(path[root].size()==2)
    {
        makePath1(root);
        makePath2(path[root][1]);

        for(i=0;i<r.size();i++)
        l.insert(l.begin(),r[i]);
    }

    else
    makePath1(root);

    if(diameter%2==1)
    {
        midPoint=l[l.size()/2];
        temp=dfs(midPoint,midPoint);
    }

    else
    {
        midPoint=l[l.size()/2];
        temp=dfs(midPoint,midPoint);

        if(temp==-1)
        {
            midPoint=l[(l.size()/2)-1];
            temp=dfs(midPoint,midPoint);
        }
    }

    if(temp==-1)
    cout<<"-1\n";

    else
    {
        for(i=0;i<200006;i++)
        visited[i]=0;

        for(i=0;i<res.size();i++)
        visited[res[i]]=1;

        for(i=0;i<200006;i++)
        {
            if(visited[i]==1)
            counter++;
        }

        if(counter>2)
        cout<<"-1\n";

        else
        {
            for(i=0;i<200006;i++)
            {
                if(visited[i]==1)
                sum=sum+i;
            }

            while(sum%2==0)
            {
                sum=sum/2;
            }

            cout<<sum<<endl;
        }

    }
}


#include<bits/stdc++.h>

using namespace std;

int length[200005],flag=0,visited[200005],node;
vector<pair<int,int> > v[200005];
vector<int> res;

int dfs(int root)
{
    visited[root]=1;
    int i,temp=0,length_root=0;

        for(i=0;i<length[root];i++)
        {
            if(!visited[v[root][i].second])
            {
                temp=dfs(v[root][i].second);

                if(root!=node)
                {
                    if(temp==-1)
                    return -1;

                    else if(length_root==0)
                    length_root=temp;

                    else if(length_root!=0 && temp!=length_root)
                    return -1;

                }

                else
                {
                    res.push_back(temp);

                    if(temp==-1)
                    return -1;
                }

            }

        }
         return length_root+1;

}

int main()
{
    int n,i,a,b,j,counter=0,sum=0;
    cin>>n;

    for(i=1;i<=n-1;i++)
    {
        cin>>a>>b;

        length[a]++;
        length[b]++;

        v[a].push_back(make_pair(a,b));
        v[b].push_back(make_pair(b,a));
    }

    for(i=1;i<n+1;i++)
    {
        res.clear();

        for(j=0;j<200006;j++)
        visited[j]=0;

        node=i;
        int temp=dfs(i);

        if(temp!=-1)
        {
            flag=1;
            break;
        }
    }

    if(flag==0)
    cout<<"-1\n";

    else
    {
       for(i=0;i<200006;i++)
       visited[i]=0;

       for(i=0;i<res.size();i++)
       {
            if(!visited[res[i]])
            visited[res[i]]=1;
       }

       for(i=0;i<200006;i++)
       {
           if(visited[i]==1)
           counter++;
       }

       if(counter>2)
       cout<<"-1\n";

       else
       {
           for(i=0;i<200006;i++)
           {
               if(visited[i]==1)
               sum=sum+i;
           }

           cout<<sum<<endl;
       }
    }

}


#include<bits/stdc++.h>

using namespace std;
vector<int> v[200005],path[200005],l,r,res;
int diameter=0,root=1,f[200005],midPoint,visited[200005];

int dfs(int node,int parent)
{
    int temp=0,pointer,length=0;
    for(int i=0;i<v[node].size();i++)
    {
        pointer=v[node][i];

        if(pointer==parent) continue;

        temp=dfs(pointer,node);

        if(node==midPoint)
        {
            if(temp==-1)
            return -1;

            else
            res.push_back(temp);
        }

        else
        {
            if(temp==-1)
            return -1;

            else if(length==0)
            length=temp;

            else if(length!=temp)
            return -1;
        }
    }

    return length+1;
}

void makePath1(int root)
{
    while(path[root].size()!=0)
    {
        l.push_back(root);
        root=path[root][0];
    }

    l.push_back(root);
}

void makePath2(int root)
{
    while(path[root].size()!=0)
    {
        r.push_back(root);
        root=path[root][0];
    }

    r.push_back(root);
}

void findRoot(int node,int parent)
{
    vector<pair<int,int> > fvalues;
    int i,pointer,sum=0;

    for(i=0;i<v[node].size();i++)
    {
        pointer=v[node][i];
        if(pointer==parent) continue;
        findRoot(pointer,node);
        fvalues.push_back(make_pair(f[pointer],pointer));
    }

    f[node]=1;

    if(!fvalues.empty())
    {
        sort(fvalues.begin(),fvalues.end());

        if(fvalues.size()>=2)
        {
            sum=fvalues[fvalues.size()-1].first+ fvalues[fvalues.size()-2].first;
            path[node].push_back(fvalues[fvalues.size()-1].second);
            path[node].push_back(fvalues[fvalues.size()-2].second);
            f[node]=1+fvalues[fvalues.size()-1].first;
        }

        else
        {
            sum=fvalues[fvalues.size()-1].first;
            path[node].push_back(fvalues[fvalues.size()-1].second);
            f[node]=1+fvalues[fvalues.size()-1].first;
        }
    }

    if(diameter!=max(diameter,1+sum))
    {
        root=node;
        diameter=1+sum;
    }
}

int main()
{
    int n,i,a,b,j,counter=0,sum=0,temp;
    cin>>n;

    for(i=0;i<n-1;i++)
    {
        cin>>a>>b;
        v[a].push_back(b);
        v[b].push_back(a);
    }

    findRoot(1,1);

    if(path[root].size()==2)
    {
        makePath1(root);
        makePath2(path[root][1]);

        for(i=0;i<r.size();i++)
        l.insert(l.begin(),r[i]);
    }

    else
    makePath1(root);

    if(diameter%2==1)
    {
        midPoint=l[l.size()/2];
        temp=dfs(midPoint,midPoint);
    }

    else
    {
        midPoint=l[l.size()/2];
        temp=dfs(midPoint,midPoint);

        if(temp==-1)
        {
            midPoint=l[(l.size()/2)-1];
            temp=dfs(midPoint,midPoint);
        }
    }

    if(temp==-1)
    cout<<"-1\n";

    else
    {
        for(i=0;i<200006;i++)
        visited[i]=0;

        for(i=0;i<res.size();i++)
        visited[res[i]]=1;

        for(i=0;i<200006;i++)
        {
            if(visited[i]==1)
            counter++;
        }

        if(counter>2)
        cout<<"-1\n";

        else
        {
            for(i=0;i<200006;i++)
            {
                if(visited[i]==1)
                sum=sum+i;
            }

            while(sum%2==0)
            {
                sum=sum/2;
            }

            cout<<sum<<endl;
        }

    }
}


#include<bits/stdc++.h>

using namespace std;
vector<int> v[200005],path[200005],l,r,res;
int diameter=0,root=1,f[200005],midPoint,visited[200005];

int dfs(int node,int parent)
{
    int temp=0,pointer,length=0;
    for(int i=0;i<v[node].size();i++)
    {
        pointer=v[node][i];

        if(pointer==parent) continue;

        temp=dfs(pointer,node);

        if(node==midPoint)
        {
            if(temp==-1)
            return -1;

            else
            res.push_back(temp);
        }

        else
        {
            if(length==0)
            length=temp;

            else if(length!=temp)
            return -1;
        }
    }

    return length+1;
}

void makePath1(int root)
{
    while(path[root].size()!=0)
    {
        l.push_back(root);
        root=path[root][0];
    }

    l.push_back(root);
}

void makePath2(int root)
{
    while(path[root].size()!=0)
    {
        r.push_back(root);
        root=path[root][0];
    }

    r.push_back(root);
}

void findRoot(int node,int parent)
{
    vector<pair<int,int> > fvalues;
    int i,pointer,sum=0;

    for(i=0;i<v[node].size();i++)
    {
        pointer=v[node][i];
        if(pointer==parent) continue;
        findRoot(pointer,node);
        fvalues.push_back(make_pair(f[pointer],pointer));
    }

    f[node]=1;

    if(!fvalues.empty())
    {
        sort(fvalues.begin(),fvalues.end());

        if(fvalues.size()>=2)
        {
            sum=fvalues[fvalues.size()-1].first+ fvalues[fvalues.size()-2].first;
            path[node].push_back(fvalues[fvalues.size()-1].second);
            path[node].push_back(fvalues[fvalues.size()-2].second);
            f[node]=1+fvalues[fvalues.size()-1].first;
        }

        else
        {
            sum=fvalues[fvalues.size()-1].first;
            path[node].push_back(fvalues[fvalues.size()-1].second);
            f[node]=1+fvalues[fvalues.size()-1].first;
        }
    }

    if(diameter!=max(diameter,1+sum))
    {
        root=node;
        diameter=1+sum;
    }
}

int main()
{
    int n,i,a,b,j,counter=0,sum=0;
    cin>>n;

    for(i=0;i<n-1;i++)
    {
        cin>>a>>b;
        v[a].push_back(b);
        v[b].push_back(a);
    }

    findRoot(1,1);

    if(path[root].size()==2)
    {
        makePath1(root);
        makePath2(path[root][1]);

        for(i=0;i<r.size();i++)
        l.insert(l.begin(),r[i]);
    }

    else
    makePath1(root);

    midPoint=l[l.size()/2];

    int temp=dfs(midPoint,midPoint);

    for(i=0;i<res.size();i++)
    cout<<res[i];

    /*if(temp==-1)
    cout<<"-1\n";

    else
    {
        for(i=0;i<res.size();i++)
        visited[res[i]]=1;

        for(i=0;i<200006;i++)
        {
            if(visited[i]==1)
            counter++;
        }

        if(counter>2)
        cout<<"-1\n";

        else
        {
            for(i=0;i<200006;i++)
            {
                if(visited[i])
                sum=sum+i;
            }

            while(sum%2==0)
            {
                sum=sum/2;
            }

            cout<<sum<<endl;
        }

    }*/
}


// ~/BAU/ACM-ICPC/Teams/A++/BlackBurn95
// ~/sudo apt-get Accpeted

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <memory.h>
#include <limits.h>
#include <math.h>
#include <string.h>
#include <string>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;

const int N = 200002;
int n, dpth[N], root, mx, last;
bool flag;
vector<vector<int> > g;

void dfs1(int u, int p) {
	if (dpth[u] > mx) {
		mx = dpth[u];
		last = u;
	}

	for (int i = 0; i < g[u].size(); i++)
		if (g[u][i] != p) {
			dpth[g[u][i]] = dpth[u] + 1;
			dfs1(g[u][i], u);
		}
}

set<int> dfs(int u, int p) {
	set<int> d;

	bool isLeaf = true;
	for (int i = 0; i < g[u].size(); i++)
		if (g[u][i] != p) {
			isLeaf = false;

			dpth[g[u][i]] = dpth[u] + 1;

			set<int> r = dfs(g[u][i], u);

			if (r.size()>1) {
				flag = false;
				break;
			}

			for (set<int>::iterator it = r.begin(); it != r.end(); it++)
				d.insert(*it);
		}

	if (isLeaf) d.insert(dpth[u]);

	if (d.size()>2 || (d.size() == 2 && p != -1))
		flag = false;

	return d;
}

int main() {
	std::ios::sync_with_stdio(false);

#ifdef LOCAL
	freopen("input.in", "r", stdin);
#endif

	scanf("%d", &n);
	g.resize(n);
	for (int i = 1, a, b; i < n; i++) {
		scanf("%d%d", &a, &b);
		a--, b--;

		g[a].push_back(b);
		g[b].push_back(a);
	}

	mx = last = -1;
	dpth[0] = 0;
	dfs1(0, -1);
	mx = -1;
	dpth[last] = 0;
	dfs1(last, -1);
	root = 0;
	for (int i = 0; i < n; i++)
		if (dpth[i] == mx / 2) {
			root = i;
			break;
		}

	dpth[root] = 0;
	flag = true;

	set<int> s = dfs(root, -1);

	if (flag) {
		int res = 0;
		for (set<int>::iterator it = s.begin(); it != s.end(); it++)
			res += *it;
		while (!(res & 1))
			res >>= 1;
		printf("%d\n", res);
	}
	else puts("-1");

	return 0;
}


// ~/BAU/ACM-ICPC/Teams/A++/BlackBurn95

import java.io.*;
import java.util.*;
import java.math.*;
import static java.lang.Math.*;
import static java.lang.Integer.parseInt;
import static java.lang.Long.parseLong;
import static java.lang.Double.parseDouble;
import static java.lang.String.*;

public class Main {

    static int n,root,last,mx;
    static List<Integer> [] g;
    static int [] p;
    static boolean flag;

    public static void main(String[] args) throws IOException {
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        StringBuilder out = new StringBuilder();
        StringTokenizer tk;

        n = parseInt(in.readLine());

        g = new List[n];
        for(int i=0; i<n; i++)
            g[i] = new ArrayList();
        p = new int[n];
        for(int i=1,a,b; i<n; i++) {
            tk = new StringTokenizer(in.readLine());
            a = parseInt(tk.nextToken())-1;
            b = parseInt(tk.nextToken())-1;

            g[a].add(b);
            g[b].add(a);
        }

        mx = -1;
        dfs1(0,-1,0);
        mx = -1;
        dfs1(last,-1,0);

        root = last;
        for(int i=0; i<mx/2; i++)
            root = p[root];

        flag = true;
        Set<Integer> res = dfs(root,-1,0);

        if(!flag) {
            System.out.println("-1");
            return;
        }

        int ans = 0;
        for(int x : res)
            ans += x;

        while(ans%2==0)
            ans /= 2;

        System.out.println(ans);
    }

    static void dfs1(int u,int p1,int dpth) {
        p[u] = p1;

        if(dpth > mx) {
            mx = dpth;
            last = u;
        }

        for(int v : g[u]) {
            if(v!=p1) {
                dfs1(v,u,dpth+1);
            }
        }
    }

    static Set<Integer> dfs(int u,int p1,int dpth) {
        Set<Integer> d = new HashSet();
        if(!flag)
            return d;

        boolean isLeaf = true;
        for(int v : g[u]) {
            if(v!=p1) {
                isLeaf = false;
                Set<Integer> r = dfs(v,u,dpth+1);

                if(r.size()>1) {
                    flag = false;
                    return r;
                }

                d.addAll(r);
            }
        }

        if(isLeaf) d.add(dpth);

        if(d.size()>2 || (d.size()==2 && p1!=-1))
            flag = false;

        return d;
    }
}


// ~/BAU/ACM-ICPC/Teams/A++/BlackBurn95
// ~/sudo apt-get Accpeted

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <memory.h>
#include <limits.h>
#include <math.h>
#include <string.h>
#include <string>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;

const int N = 200002;
int n, dpth[N], root, mx, last;
bool flag;
vector<vector<int> > g;

void dfs1(int u, int p) {
	if (dpth[u] > mx) {
		mx = dpth[u];
		last = u;
	}

	for (int i = 0; i < g[u].size(); i++)
		if (g[u][i] != p) {
			dpth[g[u][i]] = dpth[u] + 1;
			dfs1(g[u][i], u);
		}
}

set<int> dfs(int u, int p) {
	set<int> d;

	bool isLeaf = true;
	for (int i = 0; i < g[u].size(); i++)
		if (g[u][i] != p) {
			isLeaf = false;

			dpth[g[u][i]] = dpth[u] + 1;

			set<int> r = dfs(g[u][i], u);

			if (r.size()>1) {
				flag = false;
				break;
			}

			for (set<int>::iterator it = r.begin(); it != r.end(); it++)
				d.insert(*it);
		}

	if (isLeaf) d.insert(dpth[u]);

	if (d.size()>2 || (d.size() == 2 && p != -1))
		flag = false;

	return d;
}

int main() {
	std::ios::sync_with_stdio(false);

#ifdef LOCAL
	freopen("input.in", "r", stdin);
#endif

	scanf("%d", &n);
	g.resize(n);
	for (int i = 1, a, b; i < n; i++) {
		scanf("%d%d", &a, &b);
		a--, b--;

		g[a].push_back(b);
		g[b].push_back(a);
	}

	mx = last = -1;
	dpth[0] = 0;
	dfs1(0, -1);
	mx = -1;
	dpth[last] = 0;
	dfs1(last, -1);
	root = 0;
	for (int i = 0; i < n; i++)
		if (dpth[i] == mx / 2) {
			root = i;
			break;
		}

	dpth[root] = 0;
	flag = true;

	set<int> s = dfs(root, -1);

	if (flag) {
		int res = 0;
		for (set<int>::iterator it = s.begin(); it != s.end(); it++)
			res += *it;
		printf("%d\n", res);
	}
	else puts("-1");

	return 0;
}


// ~/BAU/ACM-ICPC/Teams/A++/BlackBurn95
// ~/sudo apt-get Accpeted

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <memory.h>
#include <limits.h>
#include <math.h>
#include <string.h>
#include <string>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;

const int N = 200002;
int n, dpth[N], root, mx, last;
bool flag;
vector<vector<int> > g;

void dfs1(int u, int p) {
	if (dpth[u] > mx) {
		mx = dpth[u];
		last = u;
	}

	for (int i = 0; i < g[u].size(); i++)
		if (g[u][i] != p) {
			dpth[g[u][i]] = dpth[u] + 1;
			dfs1(g[u][i], u);
		}
}

set<int> dfs(int u, int p) {
	set<int> d;

	bool isLeaf = true;
	for (int i = 0; i < g[u].size(); i++)
		if (g[u][i] != p) {
			isLeaf = false;

			dpth[g[u][i]] = dpth[u] + 1;

			set<int> r = dfs(g[u][i], u);
			/*
			if (r.size()>1) {
				flag = false;
				break;
			}
			*/
			for (set<int>::iterator it = r.begin(); it != r.end(); it++)
				d.insert(*it);
		}

	if (isLeaf) d.insert(dpth[u]);

	if (d.size()>2 || (d.size() == 2 && p != -1))
		flag = false;

	return d;
}

int main() {
	std::ios::sync_with_stdio(false);

#ifdef LOCAL
	freopen("input.in", "r", stdin);
#endif

	scanf("%d", &n);
	g.resize(n);
	for (int i = 1, a, b; i < n; i++) {
		scanf("%d%d", &a, &b);
		a--, b--;

		g[a].push_back(b);
		g[b].push_back(a);
	}

	mx = last = -1;
	dpth[0] = 0;
	dfs1(0, -1);
	mx = -1;
	dpth[last] = 0;
	dfs1(last, -1);
	root = 0;
	for (int i = 0; i < n; i++)
		if (dpth[i] == mx / 2) {
			root = i;
			break;
		}

	dpth[root] = 0;
	flag = true;

	set<int> s = dfs(root, -1);

	if (flag) {
		int res = 0;
		for (set<int>::iterator it = s.begin(); it != s.end(); it++)
			res += *it;
		while (!(res & 1))
			res >>= 1;
		printf("%d\n", res);
	}
	else puts("-1");

	return 0;
}


// ~/BAU/ACM-ICPC/Teams/A++/BlackBurn95
// ~/sudo apt-get Accpeted

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <memory.h>
#include <limits.h>
#include <math.h>
#include <string.h>
#include <string>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;

const int N = 200002;
int n, dpth[N], p1[N], root, mx, last;
bool flag;
vector<vector<int> > g;

void dfs1(int u, int p) {
	p1[u] = p;
	if (dpth[u] > mx) {
		mx = dpth[u];
		last = u;
	}

	for (int i = 0; i < g[u].size(); i++)
		if (g[u][i] != p) {
			dpth[g[u][i]] = dpth[u] + 1;
			dfs1(g[u][i], u);
		}
}

set<int> dfs(int u, int p) {
	set<int> d;

	bool isLeaf = true;
	for (int i = 0; i < g[u].size(); i++)
		if (g[u][i] != p) {
			isLeaf = false;

			dpth[g[u][i]] = dpth[u] + 1;

			set<int> r = dfs(g[u][i], u);

			if (r.size()>1) {
				flag = false;
				break;
			}

			for (set<int>::iterator it = r.begin(); it != r.end(); it++)
				d.insert(*it);
		}

	if (isLeaf) d.insert(dpth[u]);

	if (d.size()>2 || (d.size() == 2 && p != -1))
		flag = false;

	return d;
}

int main() {
	std::ios::sync_with_stdio(false);

#ifdef LOCAL
	freopen("input.in", "r", stdin);
#endif

	scanf("%d", &n);
	g.resize(n);
	for (int i = 1, a, b; i < n; i++) {
		scanf("%d%d", &a, &b);
		a--, b--;

		g[a].push_back(b);
		g[b].push_back(a);
	}

	mx = last = -1;
	dpth[0] = 0;
	dfs1(0, -1);
	mx = -1;
	dpth[last] = 0;
	dfs1(last, -1);
	root = last;
	for (int i = 0; i < mx / 2; i++)
		root = p1[root];

	dpth[root] = 0;
	flag = true;

	set<int> s = dfs(root, -1);

	if (flag) {
		int res = 0;
		for (set<int>::iterator it = s.begin(); it != s.end(); it++)
			res += *it;
		while (!(res & 1))
			res >>= 1;
		printf("%d\n", res);
	}
	else puts("-1");

	return 0;
}


// ~/BAU/ACM-ICPC/Teams/A++/BlackBurn95
// ~/sudo apt-get Accpeted

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <memory.h>
#include <limits.h>
#include <math.h>
#include <string.h>
#include <string>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;

const int N = 200002;
int n, dpth[N], root, mx, last;
bool flag;
vector<vector<int> > g;

void dfs1(int u, int p) {
	if (dpth[u] > mx) {
		mx = dpth[u];
		last = u;
	}

	for (int i = 0; i < g[u].size(); i++)
		if (g[u][i] != p) {
			dpth[g[u][i]] = dpth[u] + 1;
			dfs1(g[u][i], u);
		}
}

set<int> dfs(int u, int p) {
	set<int> d;

	bool isLeaf = true;
	for (int i = 0; i < g[u].size(); i++)
		if (g[u][i] != p) {
			isLeaf = false;

			dpth[g[u][i]] = dpth[u] + 1;

			set<int> r = dfs(g[u][i], u);

			if (r.size()>1) {
				flag = false;
				break;
			}

			for (set<int>::iterator it = r.begin(); it != r.end(); it++)
				d.insert(*it);
		}

	if (isLeaf) d.insert(dpth[u]);

	if (d.size()>2 || (d.size() == 2 && p != -1))
		flag = false;

	return d;
}

int main() {
	std::ios::sync_with_stdio(false);

#ifdef LOCAL
	freopen("input.in", "r", stdin);
#endif

	scanf("%d", &n);
	g.resize(n);
	for (int i = 1, a, b; i < n; i++) {
		scanf("%d%d", &a, &b);
		a--, b--;

		g[a].push_back(b);
		g[b].push_back(a);
	}

	mx = last = -1;
	dpth[0] = 0;
	dfs1(0, -1);
	mx = -1;
	dpth[last] = 0;
	dfs1(last, -1);
	root = 0;
	for (int i = 0; i < n; i++)
		if (dpth[i] == mx / 2) {
			root = i;
			break;
		}

	dpth[root] = 0;
	flag = true;

	set<int> s = dfs(root, -1);

	if (flag) {
		int res = 0;
		for (set<int>::iterator it = s.begin(); it != s.end(); it++)
			res += *it;
		while (!(res & 1))
			res <<= 1;
		printf("%d\n", res);
	}
	else puts("-1");

	return 0;
}


#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;
    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;
                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<=4 ; ++ i)
    {
        if(i == 2) rt = rt2 ;
        if(i == 3) rt = rt3 ;
        if(i == 4) rt = rt4;
        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;
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)
    {
        cout<<-1<<endl;
        return 0;
    }
    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)
    {
        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( j , 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 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);
    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<<rt<<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 take[210000];
int fa[210000];
int deep[210000];
int rt;
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)
{

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

        add_edge(u , v);
        add_edge(v , u);
    }
    dp[0] = 0;
    dfs2(1 , 0);
    rt = tar ;
    dp[0 ] = 0;
    Max = 0;
    dfs2(rt,0);
//    printf("%d %d\n",rt,tar);
    int ret [5];
    ret[1] = rt ;
    ret[2] = tar ;
    int m = 0;
    if(Max%2 == 0)
    {
    int res = 0;;
    int real_ans = INF;
    for(int i = 1; i<=2; ++ i)
    {

       // cout<<rt<<" "<<i<<endl;
        judge = 1;
        ans = 0;
        dfs1(ret[i]  , -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;
    }
    else
    {
        judge = 1;
        for(int i = tar; i != 0 ; i = fa[i])
            if(dp[i] == (Max+1)/2)
            {
                dp[0] = 0;

                for(int j = head[i] ; j!= -1 ; j = edge[j].next)
                {
                    //cout<<edge[j].to<<endl;
                    deep[++m] = dfs1( edge[j].to , i );
                    if(!judge) break;
                }

            //    cout<<rt5<<endl;
                break ;
            }
        int sum = 1 , cnt = 0 ;
        for(int i = 1; i<= m ; ++ i)
        {
         //   cout<<deep[i]<<endl;
            if(!take[deep[i]])
            {
                take[deep[i]] = 1;
                cnt ++ ;
                sum += deep[i];
            }
            if(cnt >3)
            {
                judge = 0;
                break;
            }
        }
     //   cout<<cnt<<endl;
        if(!judge)cout<<-1<<endl;
        else
        {

             for(;;)
            {
                if(!(sum & 1 ))
                break;
                sum = (sum -1)/2 + 1;
            }
        cout<<sum - 1<<endl;
        }
    }
//cout<<rt2<<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;;
    for(int i = 1; i<=n ; ++ i)
    {
        judge = 1;
        dfs1(i  , -1);
        if(judge )
        {
            res = i;
            break ;
        }

    }
//    cout<<res<<endl;
    if(judge )
    cout<<ans-1<<endl;
    else cout<<-1<<endl;
}