#include <bits/stdc++.h>

using namespace std;

typedef long long lli;
const int MX = 200005;
typedef pair<int,int> pii;

vector<set<int> > adj(MX) ;
vector<int> vis(MX) , viz(MX) , sols ;
int all,n,far,L , s , e,k;

void dfs1(int x ,int lvl )
{
    if(vis[x])return;
    vis[x]=1;
    for(auto u : adj[x])
        dfs1(u,lvl+1);
    if(lvl>L)
    {
        L=lvl;
        far=x;
    }
    vis[x]=0;
}

bool dfs2(int x,int lvl)
{
    if(vis[x])
        return 0;
    if(x==e)return 1;
    vis[x]=1;
    for(auto u :adj[x])
    {
        if(!vis[u])
        {
            if(dfs2(u,lvl+1))
            {
                if(L%2)
                {
                    if(lvl==L/2 ||lvl==L/2+1)
                        sols.push_back(x);
                }
                else
                {
                    if(lvl==L/2)
                        sols.push_back(x);
                }
                vis[x]=0;
                return 1;
            }
        }
    }
    return 0;
    vis[x]=0;
}
bool is = 1;
int tr(int x,int lvl)
{
    if(viz[x])return 0;
    viz[x]=1;
    set<int> val;
    for(auto u : adj[x])
    {
        if(!viz[u])
        {
            vis[x]=1;
            L=0;
            dfs1(u,1);
            val.insert(L);
            vis[x]=0;
            tr(u,lvl);
        }

    }
    if(x==k)
    {
        if(val.size()>2)
            is = 0 ;
        for(auto u : val)
            all+=u;
    }
    else
    {
        if(val.size()>1)
            is=0;
    }
    viz[x]=0;
}

int main(){
   // ios_base::sync_with_stdio(0);
    //cin.tie(0);cout.tie(0);
    scanf("%d",&n);
    for(int i=0;i<n-1;i++)
    {
        int l , r;
        scanf("%d%d",&l,&r);
        adj[l].insert(r);
        adj[r].insert(l);
    }
    dfs1(1,0);s=far;
    L=0;
    dfs1(far,0);e = far;
        dfs2(s,0);
    for(int i=0;i<MX;i++)
        vis[i]=0;
    for(auto u : sols)
    {
        is=1;
        k=u;
        tr(u,0);
        if(is)
            break;
    }
    while(all%2==0&&all)
        all/=2;
    if(is)printf("%d",all);
    else printf("-1");
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long lli;
const int MX = 200005;
typedef pair<int,int> pii;

vector<set<int> > adj(MX) ;
vector<int> vis(MX) , viz(MX) , vos(MX) , hei(MX) , sols ;
int all,n,far,L , s , e,k;

void dfs1(int x ,int lvl )
{
    if(vis[x])return;
    vis[x]=1;
    for(auto u : adj[x])
        dfs1(u,lvl+1);
    if(lvl>L)
    {
        L=lvl;
        far=x;
    }
    vis[x]=0;
}

bool dfs2(int x,int lvl)
{
    if(vis[x])
        return 0;
    if(x==e)return 1;
    vis[x]=1;
    for(auto u :adj[x])
    {
        if(!vis[u])
        {
            if(dfs2(u,lvl+1))
            {
                if(L%2)
                {
                    if(lvl==L/2 ||lvl==L/2+1)
                        sols.push_back(x);
                }
                else
                {
                    if(lvl==L/2)
                        sols.push_back(x);
                }
                vis[x]=0;
                return 1;
            }
        }
    }
    return 0;
    vis[x]=0;
}
int dfs3(int x , int lvl)
{
    if(vos[x])
        return 0;
    vos[x]=1;
    int X = 0;
    for(auto u : adj[x])
        if(!vos[u])
            X =max(X , dfs3(u,lvl) +1 );
    hei[x] = X;
    vos[x]=0;
    return X;
}
bool is = 1;
int tr(int x,int lvl)
{
    if(viz[x])return 0;
    viz[x]=1;
    set<int> val;
    for(auto u : adj[x])
    {
        if(!viz[u])
        {
            tr(u,lvl);
            val.insert(hei[u]);
            if(x==k)
            {
                if(val.size()>2)
                {
                    is = 0;
                    viz[x]=0;
                    return 0;
                }
            }
            else
            {
                if(val.size()>1)
                {
                    is=0;
                    viz[x]=0;
                    return 0;
                }
            }
        }
    }
    if(x==k)
        for(auto u : val)
            all+=u+1;
    viz[x]=0;
}

int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);cout.tie(0);
    scanf("%d",&n);
    for(int i=0;i<n-1;i++)
    {
        int l , r;
        scanf("%d%d",&l,&r);
        adj[l].insert(r);
        adj[r].insert(l);
    }
    dfs1(1,0);s=far;
    L=0;
    dfs1(far,0);e = far;
        dfs2(s,0);
    for(int i=0;i<MX;i++)
        vis[i]=0;
    for(auto u : sols)
    {
        is=1;
        k=u;
        dfs3(k,0);
        tr(u,0);
        if(is)
            break;
    }
    while(all%2==0&&all)
        all/=2;
    if(is)printf("%d",all);
    else printf("-1");
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long lli;
const int MX = 200005;
typedef pair<int,int> pii;

vector<vector<int> > adj(MX);
vector<set<int> > met(MX);
vector<int> in(MX) , vis(MX);
queue<pii> st;
int all,l,n;

void topo()
{
    while(!st.empty())
    {
        int tp = st.front().first;
        int lvl = st.front().second;
        vis[tp]=1;
        l=tp;
        st.pop();
        for(auto u : adj[tp])
        {
            if(!vis[u])
            {
                in[u]--;
                if(in[u]==1&&met[u].size()==0)
                        st.push({u,lvl+1});
                else
                {
                    if(met[u].find(lvl)!=met[u].end())
                        all-=lvl;
                    else
                        met[u].insert(lvl);
                }
            }
        }
    }
}

int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);cout.tie(0);
    scanf("%d",&n);
    all=n-1;
    for(int i=0;i<n-1;i++)
    {
        int l , r;
        scanf("%d%d",&l,&r);
        adj[l].push_back(r);
        adj[r].push_back(l);
        in[r]++;
        in[l]++;
    }
    for(int i=1;i<=n;i++)
    {
        if(in[i]==1)
            st.push({i,1});
    }
    topo();
    for(int i=1;i<=n;i++)
        if(met[i].size()>2)
        {
            printf("-1");
            return 0;
        }
    while(all%2==0)
        all/=2;
    printf("%d",all);
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long lli;
const int MX = 200005;
typedef pair<int,int> pii;

vector<vector<int> > adj(MX);
vector<set<int> > met(MX);
vector<int> in(MX) , vis(MX);
queue<pii> st;
int all,l,n;

void topo()
{
    while(!st.empty())
    {
        int tp = st.front().first;
        int lvl = st.front().second;
        vis[tp]=1;
        l=tp;
        st.pop();
        for(auto u : adj[tp])
        {
            if(!vis[u])
            {
                in[u]--;
                if(in[u]==1&&met[u].size()==0)
                        st.push({u,lvl+1});
                else
                {
                    if(met[u].find(lvl)!=met[u].end())
                    {
                        all-=lvl;
                        met[u].erase(lvl);
                    }
                    else
                        met[u].insert(lvl);
                }
            }
        }
    }
}

int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);cout.tie(0);
    scanf("%d",&n);
    all=n-1;
    for(int i=0;i<n-1;i++)
    {
        int l , r;
        scanf("%d%d",&l,&r);
        adj[l].push_back(r);
        adj[r].push_back(l);
        in[r]++;
        in[l]++;
    }
    for(int i=1;i<=n;i++)
    {
        if(in[i]==1)
            st.push({i,1});
    }
    topo();
    for(int i=1;i<=n;i++)
        if(met[i].size()>2)
    {
            printf("-1");
            return 0;
    }
    printf("%d",all);
    return 0;
}


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

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;

#define pb                    push_back
#define ll                    long long
#define ld                    long double
#define sz(x)                 (ll)x.size()
#define vll                   vector<long long>

#define infi                  INT_MAX
#define infl                  LONG_LONG_MAX
#define infd                  LDBL_MAX
#define f(i,a,b)              for(ll i=a;i<b;i++)
#define fi(i,a,b)             for(ll i=(b-1);i>=a;i--)
#define F                     first
#define S                     second
#define G(a,b)                get<a>(b)
#define MP                    make_pair
#define MT                    make_tuple
#define pll                   pair<ll,ll>
#define endl                  "\n"
#define ALL(v)                v.begin(),v.end()
#define nl                    cout<<"\n";
#define pr(x)                 cout<<x;
#define pr1(x)                cout<<x<<" ";
#define pr2(x,y)              cout<<x<<" "<<y;
#define pr3(x,y,z)            cout<<x<<" "<<y<<" "<<z;
#define pr4(x,y,z,w)          cout<<x<<" "<<y<<" "<<z<<" "<<w;
#define deb                   cout<<"***************************************************************************************\n";
#define moshi                 cout<<"moshi moshi ! \n";
#define hi                    cout<<"hi ! \n";
#define bye                   cout<<"bye bye ! \n";
#define kawai                 cout<<"O kawai koto ! \n";
#define o_set                 tree<ll, null_type,less<ll>, rb_tree_tag,tree_order_statistics_node_update>
#define number_of_set_bits  __builtin_popcountll
#define eb                    emplace_back
const ld PI=3.1415926535897932384626433;

auto clk=clock();
mt19937_64 rng(chrono::high_resolution_clock::now().time_since_epoch().count());

#define TRACE
#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
    template <typename Arg1>
    void __f(const char* name, Arg1&& arg1){
        cout << name << " : " << arg1 <<"\n";
        //use cerr if u want to display at the bottom
    }
    template <typename Arg1, typename... Args>
    void __f(const char* names, Arg1&& arg1, Args&&... args){
        const char* comma = strchr(names + 1, ','); cout.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...);
    }
#else
#define trace(...)
#endif

//**********************************************************  Code begins  ***********************************************

const ll N=2e5+5;
ll n,root;
vll adj[N];

ll dfs(ll v,ll pv)
{
  set<ll> vec;
  for(auto i:adj[v])
  {
    if(i==pv) continue;
    vec.insert(dfs(i,v));
  }

  if(!sz(vec)) return 0;
  else if(sz(vec)==1) return 1+(*vec.begin());
  if(!root) root=v;
  return 1+(*vec.begin());
}

ll dfs2(ll v,ll pv)
{
  set<ll> vec;
  for(auto i:adj[v])
  {
    if(i==pv) continue;
    vec.insert(dfs2(i,v));
  }

  if(!sz(vec)) return 0;
  else if(sz(vec)==1) return 1+(*vec.begin());
  if(v==root && sz(vec)==2)
  {
    ll res=0;
    for(auto i:vec) res+=i;
    return res+2;
  }
  cout<<"-1\n";
  exit(0);
  return 1+(*vec.begin());
}

void solve()
{
  cin>>n;
  f(i,1,n)
  {
    ll u,v;
    cin>>u>>v;
    adj[u].pb(v);
    adj[v].pb(u);
  }
  ll ans=dfs(1,0);
  if(!root)
  {
    cout<<ans<<"\n";
    return;
  }
  ans=dfs2(root,0);
  cout<<ans<<"\n";
}

int main()
{
    ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0);
    srand(chrono::high_resolution_clock::now().time_since_epoch().count());
    cout<<fixed<<setprecision(8);

    ll test_cases=1;
    //cin>>test_cases;

    f(i,1,test_cases+1)  solve();

    #ifdef rd
    cout<<endl<<endl<<endl<<endl<<"Time elapsed: "<<(double)(clock()-clk)/CLOCKS_PER_SEC<<endl;
    #endif

    return 0;
}


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

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;

#define pb                    push_back
#define ll                    long long
#define ld                    long double
#define sz(x)                 (ll)x.size()
#define vll                   vector<long long>

#define infi                  INT_MAX
#define infl                  LONG_LONG_MAX
#define infd                  LDBL_MAX
#define f(i,a,b)              for(ll i=a;i<b;i++)
#define fi(i,a,b)             for(ll i=(b-1);i>=a;i--)
#define F                     first
#define S                     second
#define G(a,b)                get<a>(b)
#define MP                    make_pair
#define MT                    make_tuple
#define pll                   pair<ll,ll>
#define endl                  "\n"
#define ALL(v)                v.begin(),v.end()
#define nl                    cout<<"\n";
#define pr(x)                 cout<<x;
#define pr1(x)                cout<<x<<" ";
#define pr2(x,y)              cout<<x<<" "<<y;
#define pr3(x,y,z)            cout<<x<<" "<<y<<" "<<z;
#define pr4(x,y,z,w)          cout<<x<<" "<<y<<" "<<z<<" "<<w;
#define deb                   cout<<"***************************************************************************************\n";
#define moshi                 cout<<"moshi moshi ! \n";
#define hi                    cout<<"hi ! \n";
#define bye                   cout<<"bye bye ! \n";
#define kawai                 cout<<"O kawai koto ! \n";
#define o_set                 tree<ll, null_type,less<ll>, rb_tree_tag,tree_order_statistics_node_update>
#define number_of_set_bits  __builtin_popcountll
#define eb                    emplace_back
const ld PI=3.1415926535897932384626433;

auto clk=clock();
mt19937_64 rng(chrono::high_resolution_clock::now().time_since_epoch().count());

#define TRACE
#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
    template <typename Arg1>
    void __f(const char* name, Arg1&& arg1){
        cout << name << " : " << arg1 <<"\n";
        //use cerr if u want to display at the bottom
    }
    template <typename Arg1, typename... Args>
    void __f(const char* names, Arg1&& arg1, Args&&... args){
        const char* comma = strchr(names + 1, ','); cout.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...);
    }
#else
#define trace(...)
#endif

//**********************************************************  Code begins  ***********************************************

const ll N=2e5+5;
ll n,root;
vll adj[N];

ll dfs(ll v,ll pv)
{
  set<ll> vec;
  for(auto i:adj[v])
  {
    if(i==pv) continue;
    vec.insert(dfs(i,v));
  }

  if(!sz(vec)) return 0;
  else if(sz(vec)==1) return 1+(*vec.begin());
  if(!root) root=v;
  return 1+(*vec.begin());
}

ll dfs2(ll v,ll pv)
{
  set<ll> vec;
  for(auto i:adj[v])
  {
    if(i==pv) continue;
    vec.insert(dfs2(i,v));
  }

  if(!sz(vec)) return 0;
  else if(sz(vec)==1) return 1+(*vec.begin());
  if(v==root && sz(vec)==2)
  {
    ll res=0;
    for(auto i:vec) res+=i;
    return res+2;
  }
  cout<<"-1\n";
  exit(0);
  return 1+(*vec.begin());
}

void solve()
{
  cin>>n;
  f(i,1,n)
  {
    ll u,v;
    cin>>u>>v;
    adj[u].pb(v);
    adj[v].pb(u);
  }
  ll ans=dfs(1,0);
  if(!root)
  {
    while(ans%2==0) ans/=2;
    cout<<ans<<"\n";
    return;
  }
  ans=dfs2(root,0);
  while(ans%2==0) ans/=2;
  cout<<ans<<"\n";
}

int main()
{
    ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0);
    srand(chrono::high_resolution_clock::now().time_since_epoch().count());
    cout<<fixed<<setprecision(8);

    ll test_cases=1;
    //cin>>test_cases;

    f(i,1,test_cases+1)  solve();

    #ifdef rd
    cout<<endl<<endl<<endl<<endl<<"Time elapsed: "<<(double)(clock()-clk)/CLOCKS_PER_SEC<<endl;
    #endif

    return 0;
}


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

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;

#define ll long long
#define db double
#define el "\n"
#define ld long double
#define rep(i,n) for(ll i=0;i<n;i++)
#define rev(i,n) for(ll i=n;i>=0;i--)
#define rep_a(i,a,n) for(ll i=a;i<n;i++)
#define tr(ds, it) for(auto it=ds.begin(); it!=ds.end(); it++)
#define rtr(ds, it) for(auto it=ds.rbegin(); it!=ds.rend(); it--)
#define all(ds) ds.begin(), ds.end()
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
typedef vector< long long > vi;
typedef pair<long long, long long> ii;
typedef priority_queue <ii, vector<ii>, greater<ii>> pq;
#define o_set tree<ll, null_type,less<ll>, rb_tree_tag,tree_order_statistics_node_update>
#define graph vector<set<ll> >

const ll mod = 1000000007;
ll po(ll x, ll n ){
    ll ans=1;
     while(n>0){
         if(n&1) ans=(ans*x)%mod;
         x=(x*x)%mod;
         n/=2;
     }
     return ans;
}

graph g;
pq q;

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    #ifndef ONLINE_JUDGE
    freopen("input.txt", "r" , stdin);
    freopen("output.txt", "w" , stdout);
    #endif
    int T=1;
    //cin >> T;
    while(T--){
        ll n,x,y;
        cin>>n;
        g.assign(n+1,set<ll>());
        vi v(n+1,0);
        rep(i,n-1){
            cin>>x>>y;
            g[x].insert(y);
            g[y].insert(x);
        }
        map<ll,ll> m;
        ll s1=0,a1=0,ans=0,s=0,c;
        rep_a(i,1,n+1){
            if(g[i].size()==1) q.push(mp(0ll,i)), v[i]=1, m.insert(mp(i,0ll));
        }
        //tr(m,h) cout<<h->ff<<" "<<h->ss<<el;
            while(!q.empty()){
                ii z=q.top();
                q.pop();
                c=z.ss;
                s=z.ff;
                //cout<<c<<" "<<s<<el;
                if(g[c].size()==0){
                    ans=m[c]+s1;
                    break;
                }
                if(c==a1) continue;
                while(g[c].size()==1){
                    y=*g[c].begin();
                    //cout<<y<<el;
                    s++;
                    if(v[y]){
                        if(m[y]!=s && a1==0){
                            a1=y;
                            s1=s;
                        }
                        else if(m[y]==s){
                        }
                        else{
                            if(y==a1 && s==s1){
                            }
                            else{
                                cout<<-1<<el;
                                return 0;
                            }
                        }
                        g[y].erase(c);
                        if(g[y].size()==1) q.push(mp(m[y],y));
                        else if(g[y].size()==0) ans=m[y]+s1;
                        break;
                    }
                    else{
                        v[y]=1;
                        g[y].erase(c);
                        if(g[y].size()!=1){
                            m.insert(mp(y,s));
                            //q.push(mp(s,y));
                            break;
                        }
                        else c=y;
                    }
                }
                if(ans>0) break;

            }
        //if(ans==0) ans=m[c]+s1;
        while(ans%2==0) ans/=2;
        cout<<ans<<el;

    }

    return 0;
}


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

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;

#define ll long long
#define db double
#define el "\n"
#define ld long double
#define rep(i,n) for(ll i=0;i<n;i++)
#define rev(i,n) for(ll i=n;i>=0;i--)
#define rep_a(i,a,n) for(ll i=a;i<n;i++)
#define tr(ds, it) for(auto it=ds.begin(); it!=ds.end(); it++)
#define rtr(ds, it) for(auto it=ds.rbegin(); it!=ds.rend(); it--)
#define all(ds) ds.begin(), ds.end()
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
typedef vector< long long > vi;
typedef pair<long long, long long> ii;
typedef priority_queue <ii, vector<ii>, greater<ii>> pq;
#define o_set tree<ll, null_type,less<ll>, rb_tree_tag,tree_order_statistics_node_update>
#define graph vector<set<ll> >

const ll mod = 1000000007;
ll po(ll x, ll n ){
    ll ans=1;
     while(n>0){
         if(n&1) ans=(ans*x)%mod;
         x=(x*x)%mod;
         n/=2;
     }
     return ans;
}

graph g;
pq q;

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    #ifndef ONLINE_JUDGE
    freopen("input.txt", "r" , stdin);
    freopen("output.txt", "w" , stdout);
    #endif
    int T=1;
    //cin >> T;
    while(T--){
        ll n,x,y;
        cin>>n;
        g.assign(n+1,set<ll>());
        vi v(n+1,0);
        rep(i,n-1){
            cin>>x>>y;
            g[x].insert(y);
            g[y].insert(x);
        }
        map<ll,ll> m;
        ll s1=0,a1=0,ans=0,s=0,c;
        rep_a(i,1,n+1){
            if(g[i].size()==1) q.push(mp(0ll,i)), v[i]=1, m.insert(mp(i,0ll));
        }
        //tr(m,h) cout<<h->ff<<" "<<h->ss<<el;
            while(!q.empty()){
                ii z=q.top();
                q.pop();
                c=z.ss;
                s=z.ff;
                //cout<<c<<" "<<s<<el;
                if(g[c].size()==0){
                    ans=m[c]+s1;
                    break;
                }
                while(g[c].size()==1){
                    y=*g[c].begin();
                    //cout<<y<<el;
                    s++;
                    if(v[y]){
                        if(m[y]!=s && a1==0){
                            a1=y;
                            s1=s;
                        }
                        else if(m[y]==s){
                        }
                        else{
                            if(y==a1 && s==s1){
                            }
                            else{
                                cout<<-1<<el;
                                return 0;
                            }
                        }
                        g[y].erase(c);
                        if(g[y].size()==1) c=y;
                        else{
                            q.push(mp(m[y],y));
                            break;
                        }
                    }
                    else{
                        v[y]=1;
                        g[y].erase(c);
                        if(g[y].size()!=1){
                            m.insert(mp(y,s));
                            //q.push(mp(s,y));
                            break;
                        }
                        else c=y;
                    }
                }

            }
        if(ans==0) ans=m[c]+s1;
        while(ans%2==0) ans/=2;
        cout<<ans<<el;

    }

    return 0;
}


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

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;

#define ll long long
#define db double
#define el "\n"
#define ld long double
#define rep(i,n) for(ll i=0;i<n;i++)
#define rev(i,n) for(ll i=n;i>=0;i--)
#define rep_a(i,a,n) for(ll i=a;i<n;i++)
#define tr(ds, it) for(auto it=ds.begin(); it!=ds.end(); it++)
#define rtr(ds, it) for(auto it=ds.rbegin(); it!=ds.rend(); it--)
#define all(ds) ds.begin(), ds.end()
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
typedef vector< long long > vi;
typedef pair<long long, long long> ii;
typedef priority_queue <ii, vector<ii>, greater<ii>> pq;
#define o_set tree<ll, null_type,less<ll>, rb_tree_tag,tree_order_statistics_node_update>
#define graph vector<set<ll> >

const ll mod = 1000000007;
ll po(ll x, ll n ){
    ll ans=1;
     while(n>0){
         if(n&1) ans=(ans*x)%mod;
         x=(x*x)%mod;
         n/=2;
     }
     return ans;
}

graph g;
pq q;

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    #ifndef ONLINE_JUDGE
    freopen("input.txt", "r" , stdin);
    freopen("output.txt", "w" , stdout);
    #endif
    int T=1;
    //cin >> T;
    while(T--){
        ll n,x,y;
        cin>>n;
        g.assign(n+1,set<ll>());
        vi v(n+1,0);
        rep(i,n-1){
            cin>>x>>y;
            g[x].insert(y);
            g[y].insert(x);
        }
        map<ll,ll> m;
        ll s1=0,a1=0,ans=0,s=0,c;
        rep_a(i,1,n+1){
            if(g[i].size()==1) q.push(mp(0ll,i)), v[i]=1, m.insert(mp(i,0ll));
        }
        //tr(m,h) cout<<h->ff<<" "<<h->ss<<el;
            while(!q.empty()){
                ii z=q.top();
                q.pop();
                c=z.ss;
                s=z.ff;
                //cout<<c<<" "<<s<<el;
                if(g[c].size()==0){
                    ans=m[c]+s1;
                    break;
                }
                if(c==a1) continue;
                while(g[c].size()==1){
                    y=*g[c].begin();
                    //cout<<y<<el;
                    s++;
                    if(v[y]){
                        if(m[y]!=s && a1==0){
                            a1=y;
                            s1=s;
                        }
                        else if(m[y]==s){
                        }
                        else{
                            if(y==a1 && s==s1){
                            }
                            else{
                                cout<<-1<<el;
                                return 0;
                            }
                        }
                        g[y].erase(c);
                        if(g[y].size()==1) q.push(mp(m[y],y));
                        break;
                    }
                    else{
                        v[y]=1;
                        g[y].erase(c);
                        if(g[y].size()!=1){
                            m.insert(mp(y,s));
                            //q.push(mp(s,y));
                            break;
                        }
                        else c=y;
                    }
                }

            }
        //if(ans==0) ans=m[c]+s1;
        while(ans%2==0) ans/=2;
        cout<<ans<<el;

    }

    return 0;
}


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

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;

#define ll long long
#define db double
#define el "\n"
#define ld long double
#define rep(i,n) for(int i=0;i<n;i++)
#define rev(i,n) for(int i=n;i>=0;i--)
#define rep_a(i,a,n) for(int i=a;i<n;i++)
#define tr(ds, it) for(auto it=ds.begin(); it!=ds.end(); it++)
#define rtr(ds, it) for(auto it=ds.rbegin(); it!=ds.rend(); it--)
#define all(ds) ds.begin(), ds.end()
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
typedef vector< long long > vi;
typedef pair<long long, long long> ii;
typedef priority_queue <ii, vector<ii>, greater<ii>> pq;
#define o_set tree<ll, null_type,less<ll>, rb_tree_tag,tree_order_statistics_node_update>
#define graph vector<set<ll> >

const ll mod = 1000000007;
ll po(ll x, ll n ){
    ll ans=1;
     while(n>0){
         if(n&1) ans=(ans*x)%mod;
         x=(x*x)%mod;
         n/=2;
     }
     return ans;
}

graph g;
pq q;

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    #ifndef ONLINE_JUDGE
    freopen("input.txt", "r" , stdin);
    freopen("output.txt", "w" , stdout);
    #endif
    int T=1;
    //cin >> T;
    while(T--){
        ll n,x,y;
        cin>>n;
        g.assign(n+1,set<ll>());
        vi v(n+1,0);
        rep(i,n-1){
            cin>>x>>y;
            g[x].insert(y);
            g[y].insert(x);
        }
        map<ll,ll> m;
        ll s1=0,a1=0,ans;
        rep_a(i,1,n+1){
            if(g[i].size()==1) q.push(mp(0,i)), v[i]=1, m.insert(mp(i,0));
        }
        //tr(m,h) cout<<h->ff<<" "<<h->ss<<el;
            while(!q.empty()){
                ii z=q.top();
                q.pop();
                ll c=z.ss, s=z.ff;
                //cout<<c<<" "<<s<<el;
                if(g[c].size()==0){
                    ans=m[c]+s1;
                    //cout<<"yy"<<el;
                }
                while(g[c].size()==1){
                    y=*g[c].begin();
                    //cout<<y<<el;
                    s++;
                    if(v[y]){
                        if(m[y]!=s && a1==0){
                            //cout<<"yy"<<el;
                            a1=y;
                            s1=s;
                            g[y].erase(c);
                            break;
                        }
                        else if(m[y]==s){
                            g[y].erase(c);
                            break;
                        }
                        else{
                            if(y==a1 && s==s1){
                                g[y].erase(c);
                                break;
                            }
                            else{
                                cout<<-1<<el;
                                return 0;
                            }
                        }
                    }
                    else{
                        v[y]=1;
                        g[y].erase(c);
                        if(g[y].size()!=1){
                            m.insert(mp(y,s));
                            q.push(mp(s,y));
                            break;
                        }
                        else c=y;
                    }
                }

            }
        while(ans%2==0) ans/=2;
        cout<<ans<<el;

    }

    return 0;
}


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

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;

#define ll long long
#define db double
#define el "\n"
#define ld long double
#define rep(i,n) for(ll i=0;i<n;i++)
#define rev(i,n) for(ll i=n;i>=0;i--)
#define rep_a(i,a,n) for(ll i=a;i<n;i++)
#define tr(ds, it) for(auto it=ds.begin(); it!=ds.end(); it++)
#define rtr(ds, it) for(auto it=ds.rbegin(); it!=ds.rend(); it--)
#define all(ds) ds.begin(), ds.end()
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
typedef vector< long long > vi;
typedef pair<long long, long long> ii;
typedef priority_queue <ii, vector<ii>, greater<ii>> pq;
#define o_set tree<ll, null_type,less<ll>, rb_tree_tag,tree_order_statistics_node_update>
#define graph vector<set<ll> >

const ll mod = 1000000007;
ll po(ll x, ll n ){
    ll ans=1;
     while(n>0){
         if(n&1) ans=(ans*x)%mod;
         x=(x*x)%mod;
         n/=2;
     }
     return ans;
}

graph g;
pq q;

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    #ifndef ONLINE_JUDGE
    freopen("input.txt", "r" , stdin);
    freopen("output.txt", "w" , stdout);
    #endif
    int T=1;
    //cin >> T;
    while(T--){
        ll n,x,y;
        cin>>n;
        g.assign(n+1,set<ll>());
        vi v(n+1,0);
        rep(i,n-1){
            cin>>x>>y;
            g[x].insert(y);
            g[y].insert(x);
        }
        map<ll,ll> m;
        ll s1=0,a1=0,ans=0,s=0,c;
        rep_a(i,1,n+1){
            if(g[i].size()==1) q.push(mp(0ll,i)), v[i]=1, m.insert(mp(i,0ll));
        }
        //tr(m,h) cout<<h->ff<<" "<<h->ss<<el;
            while(!q.empty()){
                ii z=q.top();
                q.pop();
                c=z.ss;
                s=z.ff;
                //cout<<c<<" "<<s<<el;
                if(g[c].size()==0){
                    ans=m[c]+s1;
                    break;
                }
                while(g[c].size()==1){
                    y=*g[c].begin();
                    //cout<<y<<el;
                    s++;
                    if(v[y]){
                        if(m[y]!=s && a1==0){
                            a1=y;
                            s1=s;
                        }
                        else if(m[y]==s){
                        }
                        else{
                            if(y==a1 && s==s1){
                            }
                            else{
                                cout<<-1<<el;
                                return 0;
                            }
                        }
                        g[y].erase(c);
                        if(g[y].size()==1) q.push(mp(m[y],y));
                        break;
                    }
                    else{
                        v[y]=1;
                        g[y].erase(c);
                        if(g[y].size()!=1){
                            m.insert(mp(y,s));
                            //q.push(mp(s,y));
                            break;
                        }
                        else c=y;
                    }
                }

            }
        if(ans==0) ans=m[c]+s1;
        while(ans%2==0) ans/=2;
        cout<<ans<<el;

    }

    return 0;
}


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

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;

#define ll long long
#define db double
#define el "\n"
#define ld long double
#define rep(i,n) for(ll i=0;i<n;i++)
#define rev(i,n) for(ll i=n;i>=0;i--)
#define rep_a(i,a,n) for(ll i=a;i<n;i++)
#define tr(ds, it) for(auto it=ds.begin(); it!=ds.end(); it++)
#define rtr(ds, it) for(auto it=ds.rbegin(); it!=ds.rend(); it--)
#define all(ds) ds.begin(), ds.end()
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
typedef vector< long long > vi;
typedef pair<long long, long long> ii;
typedef priority_queue <ii, vector<ii>, greater<ii>> pq;
#define o_set tree<ll, null_type,less<ll>, rb_tree_tag,tree_order_statistics_node_update>
#define graph vector<set<ll> >

const ll mod = 1000000007;
ll po(ll x, ll n ){
    ll ans=1;
     while(n>0){
         if(n&1) ans=(ans*x)%mod;
         x=(x*x)%mod;
         n/=2;
     }
     return ans;
}

graph g;
pq q;

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    #ifndef ONLINE_JUDGE
    freopen("input.txt", "r" , stdin);
    freopen("output.txt", "w" , stdout);
    #endif
    int T=1;
    //cin >> T;
    while(T--){
        ll n,x,y;
        cin>>n;
        g.assign(n+1,set<ll>());
        vi v(n+1,0);
        rep(i,n-1){
            cin>>x>>y;
            g[x].insert(y);
            g[y].insert(x);
        }
        map<ll,ll> m;
        ll s1=0,a1=0,ans=0,s=0,c;
        rep_a(i,1,n+1){
            if(g[i].size()==1) q.push(mp(0ll,i)), v[i]=1, m.insert(mp(i,0ll));
        }
        //tr(m,h) cout<<h->ff<<" "<<h->ss<<el;
            while(!q.empty()){
                ii z=q.top();
                q.pop();
                c=z.ss;
                s=z.ff;
                //cout<<c<<" "<<s<<el;
                if(g[c].size()==0){
                    ans=m[c]+s1;
                    break;
                }
                while(g[c].size()==1){
                    y=*g[c].begin();
                    //cout<<y<<el;
                    s++;
                    if(v[y]){
                        if(m[y]!=s && a1==0){
                            a1=y;
                            s1=s;
                        }
                        else if(m[y]==s){
                        }
                        else{
                            if(y==a1 && s==s1){
                            }
                            else{
                                cout<<-1<<el;
                                return 0;
                            }
                        }
                        g[y].erase(c);
                        if(g[y].size()==1) c=y;
                        else break;
                    }
                    else{
                        v[y]=1;
                        g[y].erase(c);
                        if(g[y].size()!=1){
                            m.insert(mp(y,s));
                            //q.push(mp(s,y));
                            break;
                        }
                        else c=y;
                    }
                }

            }
        if(ans==0) ans=m[c]+s1;
        while(ans%2==0) ans/=2;
        cout<<ans<<el;

    }

    return 0;
}


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

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;

#define ll long long
#define db double
#define el "\n"
#define ld long double
#define rep(i,n) for(int i=0;i<n;i++)
#define rev(i,n) for(int i=n;i>=0;i--)
#define rep_a(i,a,n) for(int i=a;i<n;i++)
#define tr(ds, it) for(auto it=ds.begin(); it!=ds.end(); it++)
#define rtr(ds, it) for(auto it=ds.rbegin(); it!=ds.rend(); it--)
#define all(ds) ds.begin(), ds.end()
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
typedef vector< long long > vi;
typedef pair<long long, long long> ii;
typedef priority_queue <ii, vector<ii>, greater<ii>> pq;
#define o_set tree<ll, null_type,less<ll>, rb_tree_tag,tree_order_statistics_node_update>
#define graph vector<set<ll> >

const ll mod = 1000000007;
ll po(ll x, ll n ){
    ll ans=1;
     while(n>0){
         if(n&1) ans=(ans*x)%mod;
         x=(x*x)%mod;
         n/=2;
     }
     return ans;
}

graph g;
pq q;

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    #ifndef ONLINE_JUDGE
    freopen("input.txt", "r" , stdin);
    freopen("output.txt", "w" , stdout);
    #endif
    int T=1;
    //cin >> T;
    while(T--){
        ll n,x,y;
        cin>>n;
        g.assign(n+1,set<ll>());
        vi v(n+1,0);
        rep(i,n-1){
            cin>>x>>y;
            g[x].insert(y);
            g[y].insert(x);
        }
        map<ll,ll> m;
        ll s1=0,a1=0,ans=0,s=0,c;
        rep_a(i,1,n+1){
            if(g[i].size()==1) q.push(mp(0ll,i)), v[i]=1, m.insert(mp(i,0ll));
        }
        //tr(m,h) cout<<h->ff<<" "<<h->ss<<el;
            while(!q.empty()){
                ii z=q.top();
                q.pop();
                c=z.ss;
                s=z.ff;
                //cout<<c<<" "<<s<<el;
                if(g[c].size()==0){
                    ans=m[c]+s1;
                    break;
                }
                while(g[c].size()==1){
                    y=*g[c].begin();
                    //cout<<y<<el;
                    s++;
                    if(v[y]){
                        if(m[y]!=s && a1==0){
                            //cout<<"yy"<<el;
                            a1=y;
                            s1=s;
                            g[y].erase(c);
                            break;
                        }
                        else if(m[y]==s){
                            g[y].erase(c);
                            break;
                        }
                        else{
                            if(y==a1 && s==s1){
                                g[y].erase(c);
                                break;
                            }
                            else{
                                cout<<-1<<el;
                                return 0;
                            }
                        }
                    }
                    else{
                        v[y]=1;
                        g[y].erase(c);
                        if(g[y].size()!=1){
                            m.insert(mp(y,s));
                            q.push(mp(s,y));
                            break;
                        }
                        else c=y;
                    }
                }

            }
        if(ans==0) ans=m[c]+s1;
        while(ans%2==0) ans/=2;
        cout<<ans<<el;

    }

    return 0;
}


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

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;

#define ll long long
#define db double
#define el "\n"
#define ld long double
#define rep(i,n) for(ll i=0;i<n;i++)
#define rev(i,n) for(ll i=n;i>=0;i--)
#define rep_a(i,a,n) for(ll i=a;i<n;i++)
#define tr(ds, it) for(auto it=ds.begin(); it!=ds.end(); it++)
#define rtr(ds, it) for(auto it=ds.rbegin(); it!=ds.rend(); it--)
#define all(ds) ds.begin(), ds.end()
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
typedef vector< long long > vi;
typedef pair<long long, long long> ii;
typedef priority_queue <ii, vector<ii>, greater<ii>> pq;
#define o_set tree<ll, null_type,less<ll>, rb_tree_tag,tree_order_statistics_node_update>
#define graph vector<set<ll> >

const ll mod = 1000000007;
ll po(ll x, ll n ){
    ll ans=1;
     while(n>0){
         if(n&1) ans=(ans*x)%mod;
         x=(x*x)%mod;
         n/=2;
     }
     return ans;
}

graph g;
pq q;

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    #ifndef ONLINE_JUDGE
    freopen("input.txt", "r" , stdin);
    freopen("output.txt", "w" , stdout);
    #endif
    int T=1;
    //cin >> T;
    while(T--){
        ll n,x,y;
        cin>>n;
        g.assign(n+1,set<ll>());
        vi v(n+1,0);
        rep(i,n-1){
            cin>>x>>y;
            g[x].insert(y);
            g[y].insert(x);
        }
        map<ll,ll> m;
        ll s1=0,a1=0,ans=0,s=0,c;
        rep_a(i,1,n+1){
            if(g[i].size()==1) q.push(mp(0ll,i)), v[i]=1, m.insert(mp(i,0ll));
        }
        //tr(m,h) cout<<h->ff<<" "<<h->ss<<el;
            while(!q.empty()){
                ii z=q.top();
                q.pop();
                c=z.ss;
                s=z.ff;
                //cout<<c<<" "<<s<<el;
                if(g[c].size()==0){
                    ans=m[c]+s1;
                    break;
                }
                if(c==a1) continue;
                while(g[c].size()==1){
                    y=*g[c].begin();
                    //cout<<y<<el;
                    s++;
                    if(v[y]){
                        if(m[y]!=s && a1==0){
                            a1=y;
                            s1=s;
                        }
                        else if(m[y]==s){
                        }
                        else{
                            if(y==a1 && s==s1){
                            }
                            else{
                                cout<<-1<<el;
                                return 0;
                            }
                        }
                        g[y].erase(c);
                        if(g[y].size()==1) q.push(mp(m[y],y));
                        break;
                    }
                    else{
                        v[y]=1;
                        g[y].erase(c);
                        if(g[y].size()!=1){
                            m.insert(mp(y,s));
                            //q.push(mp(s,y));
                            break;
                        }
                        else c=y;
                    }
                }

            }
        if(ans==0) ans=m[c]+s1;
        while(ans%2==0) ans/=2;
        cout<<ans<<el;

    }

    return 0;
}


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

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;

#define ll long long
#define db double
#define el "\n"
#define ld long double
#define rep(i,n) for(int i=0;i<n;i++)
#define rev(i,n) for(int i=n;i>=0;i--)
#define rep_a(i,a,n) for(int i=a;i<n;i++)
#define tr(ds, it) for(auto it=ds.begin(); it!=ds.end(); it++)
#define rtr(ds, it) for(auto it=ds.rbegin(); it!=ds.rend(); it--)
#define all(ds) ds.begin(), ds.end()
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
typedef vector< long long > vi;
typedef pair<long long, long long> ii;
typedef priority_queue <ii, vector<ii>, greater<ii>> pq;
#define o_set tree<ll, null_type,less<ll>, rb_tree_tag,tree_order_statistics_node_update>
#define graph vector<set<ll> >

const ll mod = 1000000007;
ll po(ll x, ll n ){
    ll ans=1;
     while(n>0){
         if(n&1) ans=(ans*x)%mod;
         x=(x*x)%mod;
         n/=2;
     }
     return ans;
}

graph g;
pq q;

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    #ifndef ONLINE_JUDGE
    freopen("input.txt", "r" , stdin);
    freopen("output.txt", "w" , stdout);
    #endif
    int T=1;
    //cin >> T;
    while(T--){
        ll n,x,y;
        cin>>n;
        g.assign(n+1,set<ll>());
        vi v(n+1,0);
        rep(i,n-1){
            cin>>x>>y;
            g[x].insert(y);
            g[y].insert(x);
        }
        map<ll,ll> m;
        ll s1=0,a1=0,ans=0,s=0;
        rep_a(i,1,n+1){
            if(g[i].size()==1) q.push(mp(0ll,i)), v[i]=1, m.insert(mp(i,0ll));
        }
        //tr(m,h) cout<<h->ff<<" "<<h->ss<<el;
            while(!q.empty()){
                ii z=q.top();
                q.pop();
                ll c=z.ss;
                s=z.ff;
                //cout<<c<<" "<<s<<el;
                if(g[c].size()==0){
                    ans=m[c]+s1;
                    break;
                }
                while(g[c].size()==1){
                    y=*g[c].begin();
                    //cout<<y<<el;
                    s++;
                    if(v[y]){
                        if(m[y]!=s && a1==0){
                            //cout<<"yy"<<el;
                            a1=y;
                            s1=s;
                            g[y].erase(c);
                            break;
                        }
                        else if(m[y]==s){
                            g[y].erase(c);
                            break;
                        }
                        else{
                            if(y==a1 && s==s1){
                                g[y].erase(c);
                                break;
                            }
                            else{
                                cout<<-1<<el;
                                return 0;
                            }
                        }
                    }
                    else{
                        v[y]=1;
                        g[y].erase(c);
                        if(g[y].size()!=1){
                            m.insert(mp(y,s));
                            q.push(mp(s,y));
                            break;
                        }
                        else c=y;
                    }
                }

            }
        if(ans==0) ans=s+s1;
        while(ans%2==0) ans/=2;
        cout<<ans<<el;

    }

    return 0;
}


#include <bits/stdc++.h>
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
using namespace std;
typedef long long   ll;
typedef unsigned long long   ull;
typedef long double ld;
typedef pair<ll, ll>    pll;
typedef pair<ull, ull>    pull;
typedef pair<ll, ll>  pii;
typedef pair<ld, ld>  pld;
int n;
vector<int> g[500009];
int path[500009];
int root = 1;
int changed = 0;
int psize;
int f(int v, int p){
    int cur = 0;
    set<int> dif;
    for(auto u : g[v]){
        if(u == p)continue;
        if(f(u, v))
            return 1;
        if(cur == 0)
            cur = path[u];
        if(v == root)
            dif.insert(path[u]);
        if(cur != path[u] && v != root && changed == 0){
            root = v;
            changed = 1;
            return 1;
        }
    }
    if(v != root){
        path[v] = cur+1;
    }
    else{
        if(dif.size() > 2){
            cout << "-1\n";
            exit(0);
        }
        psize = 1;
        for(auto u : dif)
            psize += u;
    }
    return 0;
}
int main() {
    //freopen("in.txt", "r", stdin);
    //freopen("out.txt", "w", stdout);
    cin >> n;
    for(int i = 0; i < n-1; ++i){
        int a, b;
        cin >> a >> b;
        g[a].pb(b);
        g[b].pb(a);
    }
    for(int i = 1; i <= n; ++i)
        if(g[i].size() > 2)
            root = i;
    if(f(root, -1))
        f(root, -1);
    while(psize % 2 && psize > 1){
        psize = (psize-1)/2+1;
    }
    cout << psize-1 << "\n";
}


#include <bits/stdc++.h>
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
using namespace std;
typedef long long   ll;
typedef unsigned long long   ull;
typedef long double ld;
typedef pair<ll, ll>    pll;
typedef pair<ull, ull>    pull;
typedef pair<ll, ll>  pii;
typedef pair<ld, ld>  pld;
int n;
vector<int> g[500009];
int path[500009];
int root = 1;
int psize;
void f(int v, int p){
    int cur = 0;
    set<int> dif;
    for(auto u : g[v]){
        if(u == p)continue;
        f(u, v);
        if(cur == 0)
            cur = path[u];
        if(v == root)
            dif.insert(path[u]);
        if(cur != path[u] && v != root){
            cout << "-1\n";
            exit(0);
        }
    }
    if(v != root){
        path[v] = cur+1;
        return;
    }
    else{
        if(dif.size() > 2){
            cout << "-1\n";
            exit(0);
        }
        psize = 1;
        for(auto u : dif)
            psize += u;
    }
}
int main() {
    //freopen("in.txt", "r", stdin);
    //freopen("out.txt", "w", stdout);
    cin >> n;
    for(int i = 0; i < n-1; ++i){
        int a, b;
        cin >> a >> b;
        g[a].pb(b);
        g[b].pb(a);
    }
    for(int i = 1; i <= n; ++i)
        if(g[i].size() > 2)
            root = i;
    f(root, -1);
    while(psize % 2 && psize > 1){
        psize = (psize-1)/2+1;
    }
    cout << psize-1 << "\n";
}


#include <bits/stdc++.h>
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
using namespace std;
typedef long long   ll;
typedef unsigned long long   ull;
typedef long double ld;
typedef pair<ll, ll>    pll;
typedef pair<ull, ull>    pull;
typedef pair<ll, ll>  pii;
typedef pair<ld, ld>  pld;
int n;
vector<int> g[500009];
int path[500009];
int root = 1;
int changed = 0;
int psize;
int f(int v, int p){
    int cur = 0;
    set<int> dif;
    for(auto u : g[v]){
        if(u == p)continue;
        if(f(u, v))
            return 1;
        if(cur == 0)
            cur = path[u];
        if(v == root)
            dif.insert(path[u]);
        if(cur != path[u] && v != root && changed == 0){
            root = v;
            changed = 1;
            return 1;
        }
        else if(cur != path[u] && v != root && changed == 1){
            cout << "-1\n";
            exit(0);
        }
    }
    if(v != root){
        path[v] = cur+1;
    }
    else{
        if(dif.size() > 2){
            cout << "-1\n";
            exit(0);
        }
        psize = 1;
        for(auto u : dif)
            psize += u;
    }
    return 0;
}
int main() {
    //freopen("in.txt", "r", stdin);
    //freopen("out.txt", "w", stdout);
    cin >> n;
    for(int i = 0; i < n-1; ++i){
        int a, b;
        cin >> a >> b;
        g[a].pb(b);
        g[b].pb(a);
    }
    for(int i = 1; i <= n; ++i)
        if(g[i].size() > 2)
            root = i;
    if(f(root, -1))
        f(root, -1);
    while(psize % 2 && psize > 1){
        psize = (psize-1)/2+1;
    }
    cout << psize-1 << "\n";
}


#include <bits/stdc++.h>
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
using namespace std;
typedef long long   ll;
typedef unsigned long long   ull;
typedef long double ld;
typedef pair<ll, ll>    pll;
typedef pair<ull, ull>    pull;
typedef pair<ll, ll>  pii;
typedef pair<ld, ld>  pld;
int n;
vector<int> g[500009];
int path[500009];
int root = 1;
int psize;
void f(int v, int p){
    int cur = 0;
    set<int> dif;
    for(auto u : g[v]){
        if(u == p)continue;
        f(u, v);
        if(cur == 0)
            cur = path[u];
        if(v == root)
            dif.insert(path[u]);
        if(cur != path[u] && v != root){
            cout << "-1\n";
            exit(0);
        }
    }
    if(v != root){
        path[v] = cur+1;
        return;
    }
    else{
        if(dif.size() > 2){
            cout << "-1\n";
            exit(0);
        }
        psize = 1;
        for(auto u : dif)
            psize += u;
    }
}
int main() {
    //freopen("in.txt", "r", stdin);
    //freopen("out.txt", "w", stdout);
    cin >> n;
    for(int i = 0; i < n-1; ++i){
        int a, b;
        cin >> a >> b;
        g[a].pb(b);
        g[b].pb(a);
    }
    for(int i = 1; i <= n; ++i)
        if(g[i].size() > 2)
            root = i;
    f(root, -1);
    cout << psize << "\n";
    while(psize % 2 && psize > 1){
        psize = (psize-1)/2+1;
    }
    cout << psize-1 << "\n";
}


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

#define F                      first
#define S                      second
#define pb                      push_back
#define endl                    '\n'
#define all(x)                  x.begin(),x.end()
#define pii                     pair<int,int>
#define pll                     pair< long long int, long long int>
#define fast                    ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define SZ(x)                   ((int)x.size())

#define FOR(i,a,b)              for(int i = a; i < b; ++i)
#define REV(i,a,b)              for(int i = a; i >= b; --i)
#define FORE(i,a,b)             for(int i = a; i <= b; ++i)

typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;

template <typename A, typename B>
string to_string(pair<A, B> p);

string to_string(const string& s) {
        return '"' + s + '"';
}

string to_string(const char* s) {
        return to_string((string) s);
}

string to_string(bool b) {
        return (b ? "true" : "false");
}

string to_string(vector<bool> v) {
        bool first = true;
        string res = "{";
        for (int i = 0; i < static_cast<int>(v.size()); i++) {
                if (!first) {
                        res += ", ";
                }
                first = false;
                res += to_string(v[i]);
        }
        res += "}";
        return res;
}

template <size_t N>
string to_string(bitset<N> v) {
        string res = "";
        for (size_t i = 0; i < N; i++) {
                res += static_cast<char>('0' + v[i]);
        }
        return res;
}

template <typename A>
string to_string(A v) {
        bool first = true;
        string res = "{";
        for (const auto &x : v) {
                if (!first) {
                        res += ", ";
                }
                first = false;
                res += to_string(x);
        }
        res += "}";
        return res;
}

template <typename A, typename B>
string to_string(pair<A, B> p) {
        return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}

void debug_out() {
        cerr << endl;
}

template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
        cerr << " " << to_string(H);
        debug_out(T...);
}

#ifdef IGM
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define debug(...)
#endif

void solve();

int main()
{
        fast;
        solve();
}

const int N = 2e5+2;
std::vector<int> adj[N];
int n;

int cent = -1;
int ans = -1;

int dfs(int node, int prev) {
	set<int> s;
	if(cent != -1)
		return 0;
	for(auto i:adj[node]) {
		if(i != prev) {
			s.insert(dfs(i,node));
		}
	}
	if(cent != -1)
		return 0;
	if(s.size() > 1 and prev != 0) {
		// debug(node,s);
		cent = node;
	}
	else if(prev == 0 and s.size() >= 1 and s.size() <= 2) {
		int sum = 0;
		for(auto i:s) {
			sum += i;
		}
		ans = sum;
	}
	if(s.size() == 0) {
		s.insert(0);
	}
	return (*s.begin() + 1);
}

void solve() {
        cin >> n;
        FOR(i,1,n) {
        	int u,v;
        	cin >> u >> v;
        	adj[u].pb(v);
        	adj[v].pb(u);
        }
        FOR(i,1,n+1) {
        	if(adj[i].size() > 2) {
        		dfs(i,0);
        		break;
        	}
        	if(i == n) {
        		dfs(i,0);
        	}
        }
        if(ans != -1) {
        	while(ans%2 == 0 and ans > 0) ans /= 2;
        	cout << ans << endl;
        	return;
        }
        int temp = cent;
        cent = -1;
        if(temp != -1)
        	dfs(temp,0);
        while(ans%2 == 0 and ans > 0) ans /= 2;
        cout << ans << endl;
}


#include <bits\stdc++.h>
using namespace std;
int n,i,u,v,m,t[200179],r,d;
vector<int> a,b;
set<int> s[200179];
int main(){
cin>>n;for(i=1;i<n;i++)cin>>v>>u,s[v].insert(u),s[u].insert(v);
for(i=1;i<=n;i++)if(s[i].size()==1)b.push_back(i);
for(m=n;m-=b.size(),b.size();){
 if(!m)b.pop_back();
 swap(a,b);b.clear();
 for(i=0;i<a.size();i++){
  v=a[i]; u=*s[v].begin();
  s[u].erase(v);
  r=t[v]+1;if(!t[u])t[u]=r;
  if(m<2){
   if(r!=t[u]&&!d)d=r;
   else if(r!=d&&r!=t[u])return cout<<-1,0;
  }
  else if(t[u]!=r)return cout<<-1,0;
  if(s[u].size()==1&&m>1)b.push_back(u);
  }
 }
u=t[u]+d;cout<<(u>>__builtin_ctz(u));
}


#include <map>
#include <set>
#include <ctime>
#include <queue>
#include <stack>
#include <cmath>
#include <vector>
#include <bitset>
#include <cstdio>
#include <cctype>
#include <string>
#include <numeric>
#include <cstring>
#include <cassert>
#include <climits>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <functional>
using namespace std ;
#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define per(i, a, b) for (int i = (a); i >= (b); i--)
#define loop(s, v, it) for (s::iterator it = v.begin(); it != v.end(); it++)
#define cont(i, x) for (int i = head[x]; i; i = e[i].nxt)
#define clr(a) memset(a, 0, sizeof(a))
#define ass(a, sum) memset(a, sum, sizeof(a))
#define lowbit(x) (x & -x)
#define all(x) x.begin(), x.end()
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define iv inline void
#define enter cout << endl
#define siz(x) ((int)x.size())
typedef long long ll ;
typedef unsigned long long ull ;
typedef pair <int, int> pii ;
typedef vector <int> vi ;
typedef vector <pii> vii ;
typedef queue <int> qi ;
typedef set <int> si ;
typedef map <int, int> mii ;
typedef map <string, int> msi ;
const int N = 200010 ;
const int INF = 0x3f3f3f3f ;
const int iinf = 1 << 30 ;
const ll linf = 2e18 ;
const int MOD = 1000000007 ;
void print(int x) { cout << x << endl ; exit(0) ; }
void PRINT(string x) { cout << x << endl ; exit(0) ; }
void douout(double x){ printf("%lf\n", x + 0.0000000001) ; }

vi e[N] ;
int rt, n ;

int dfs(int u, int fat) {
	si s ;
	rep(i, 0, siz(e[u]) - 1) {
		int v = e[u][i] ;
		if (v == fat) continue ;
		int res = dfs(v, u) ;
		if (res == -1) return -1 ;
		s.insert(res + 1) ;
		if (siz(s) > 2) return -1 ;
	}
	if (siz(s) == 2 && fat) {
		rt = u ;
		return -1 ;
	}
	return accumulate(all(s), 0) ;
}

signed main(){
	scanf("%d", &n) ;
	rep(i, 1, n - 1) {
		int x, y ; scanf("%d%d", &x, &y) ;
		e[x].pb(y) ; e[y].pb(x) ;
	}
	int ans = dfs(1, 0) ;
	if (rt) ans = dfs(rt, 0) ;
	while (ans % 2 == 0) ans /= 2 ;
	cout << ans << endl ;
	return 0 ;
}

/*
写代码时请注意：
	1.ll？数组大小，边界？数据范围？
	2.精度？
	3.特判？
	4.至少做一些
思考提醒：
	1.最大值最小->二分？
	2.可以贪心么？不行dp可以么
	3.可以优化么
	4.维护区间用什么数据结构？
	5.统计方案是用dp？模了么？
	6.逆向思维？
*/


#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<algorithm>
#include<map>
#include<vector>
#include<math.h>
#include<queue>
#include<stack>
#include<unordered_map>
#define fo(i,a,b) for(int i=a;i<=b;i++)
#define fod(i,a,b) for(int i=a;i>=b;i--)
#define me0(a) memset(a,0,sizeof(a))
#define me1(a) memset(a,-1,sizeof(a))
#define op freopen("in.txt", "r", stdin)
#define read(a) scanf("%d",&a)
#define mp(a,b) make_pair(a,b)
typedef long long LL;
using namespace std;
const int mod = 1e9 + 7;
const int maxn = 2e5 + 10;
const int INF = 0x3f3f3f3f;

vector<int>g[maxn];

int dep[maxn], mxdep[maxn];
int n, mx, mxnod;
int fa[maxn];

void dfs(int u, int v) {
	fa[u] = v;
	if (mx < dep[u]) mx = dep[u], mxnod = u;
	for (auto to : g[u]) {
		if (to == v) continue;
		dep[to] = dep[u] + 1;
		dfs(to, u);
	}
}

int ans;
int d[maxn];
int tmpdis[maxn];
int rt;

void solve(int u, int p) {
	int tot = 0;
	mxdep[u] = 1;
	for (auto to : g[u]) {
		if (to == p) continue;
		solve(to, u);
	}

	for (auto to : g[u]) {
		if (to == p) continue;
		tmpdis[++tot] = mxdep[to];
	}

	sort(tmpdis + 1, tmpdis + 1 + tot);
	if (ans != -1 && u != rt && d[u] > 2) {
		tot = unique(tmpdis + 1, tmpdis + 1 + tot) - tmpdis - 1;
		if (tot != 1) ans = -1;
	}
	mxdep[u] = tmpdis[tot] + 1;
}

int main() {
	//op;
	read(n);
	fo(i, 1, n - 1) {
		int u, v;
		read(u); read(v);
		g[u].push_back(v);
		g[v].push_back(u);
		d[u]++; d[v]++;
	}
	dep[1] = 1;
	dfs(1, 0);
	dep[mxnod] = 1;
	mx = 0;
	dfs(mxnod, 0);
	int mid = (mx - 1) / 2;
	rt = mxnod;
	fo(i, 1, mid) rt = fa[rt];
	dfs(rt, 0);

	solve(rt, 0);
	//fo(i, 1, n) cout << i << " " << mxdep[i] << endl;

	if (ans == -1) printf("-1\n");
	else {
		int tot = 0;
		for (auto to : g[rt]) {
			tmpdis[++tot] = mxdep[to];
		}
		sort(tmpdis + 1, tmpdis + 1 + tot);
		tot = unique(tmpdis + 1, tmpdis + 1 + tot) - tmpdis - 1;
		if (tot > 2) ans = -1;
		else if (tot == 2) {
			ans = tmpdis[1] + tmpdis[2];
		}
		else {
			ans = tmpdis[1];
		}
		if (ans == -1) printf("-1\n");
		else {
			while (ans % 2 == 0) ans /= 2;
			printf("%d\n", ans);
		}
	}

	return 0;
}


#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<algorithm>
#include<map>
#include<vector>
#include<math.h>
#include<queue>
#include<stack>
#include<unordered_map>
#define fo(i,a,b) for(int i=a;i<=b;i++)
#define fod(i,a,b) for(int i=a;i>=b;i--)
#define me0(a) memset(a,0,sizeof(a))
#define me1(a) memset(a,-1,sizeof(a))
#define op freopen("in.txt", "r", stdin)
#define read(a) scanf("%d",&a)
#define mp(a,b) make_pair(a,b)
typedef long long LL;
using namespace std;
const int mod = 1e9 + 7;
const int maxn = 2e5 + 10;
const int INF = 0x3f3f3f3f;

vector<int>g[maxn];

int dep[maxn], mxdep[maxn];
int n, mx, mxnod;
int fa[maxn];

void dfs(int u, int v) {
	fa[u] = v;
	if (mx < dep[u]) mx = dep[u], mxnod = u;
	for (auto to : g[u]) {
		if (to == v) continue;
		dep[to] = dep[u] + 1;
		dfs(to, u);
	}
}

int ans;
int d[maxn];
int tmpdis[maxn];
int rt;

void solve(int u, int p) {
	int tot = 0;
	mxdep[u] = 1;
	for (auto to : g[u]) {
		if (to == p) continue;
		solve(to, u);
		tmpdis[++tot] = mxdep[to];
	}
	sort(tmpdis + 1, tmpdis + 1 + tot);
	if (ans != -1 && u != rt && d[u] > 2) {
		tot = unique(tmpdis + 1, tmpdis + 1 + tot) - tmpdis - 1;
		if (tot != 1) ans = -1;
	}
	mxdep[u] = tmpdis[tot] + 1;
}

int main() {
	//op;
	read(n);
	fo(i, 1, n - 1) {
		int u, v;
		read(u); read(v);
		g[u].push_back(v);
		g[v].push_back(u);
		d[u]++; d[v]++;
	}
	dep[1] = 1;
	dfs(1, 0);
	dep[mxnod] = 1;
	mx = 0;
	dfs(mxnod, 0);
	int mid = (mx - 1) / 2;
	rt = mxnod;
	fo(i, 1, mid) rt = fa[rt];
	dfs(rt, 0);
	solve(rt, 0);
	if (n == 200000)cout << "ans=" << ans << endl;
	if (ans == -1) printf("-1\n");
	else {
		int tot = 0;
		for (auto to : g[rt]) {
			tmpdis[++tot] = mxdep[to];
		}
		sort(tmpdis + 1, tmpdis + 1 + tot);
		tot = unique(tmpdis + 1, tmpdis + 1 + tot) - tmpdis - 1;
		if (tot > 2) ans = -1;
		else if (tot == 2) {
			ans = tmpdis[1] + tmpdis[2];
		}
		else {
			ans = tmpdis[1];
		}
		if (ans == -1) printf("-1\n");
		else {
			while (ans % 2 == 0) ans /= 2;
			printf("%d\n", ans);
		}
	}

	return 0;
}


#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<algorithm>
#include<map>
#include<vector>
#include<math.h>
#include<queue>
#include<stack>
#include<unordered_map>
#define fo(i,a,b) for(int i=a;i<=b;i++)
#define fod(i,a,b) for(int i=a;i>=b;i--)
#define me0(a) memset(a,0,sizeof(a))
#define me1(a) memset(a,-1,sizeof(a))
#define op freopen("in.txt", "r", stdin)
#define read(a) scanf("%d",&a)
#define mp(a,b) make_pair(a,b)
typedef long long LL;
using namespace std;
const int mod = 1e9 + 7;
const int maxn = 2e5 + 10;
const int INF = 0x3f3f3f3f;

vector<int>g[maxn];

int dep[maxn], mxdep[maxn];
int n, mx, mxnod;

void dfs(int u, int v) {
	if (mx < dep[u]) mx = dep[u], mxnod = u;
	for (auto to : g[u]) {
		if (to == v) continue;
		dep[to] = dep[u] + 1;
		dfs(to, u);
	}
}

int fa[maxn];
void dfs2(int u, int p) {
	fa[u] = p;
	if (mx < dep[u]) mx = dep[u], mxnod = u;
	for (auto to : g[u]) {
		if (to == p) continue;
		dep[to] = dep[u] + 1;
		dfs2(to, u);
	}
}

int ans;
int d[maxn];
int tmpdis[maxn];
int rt;

void solve(int u, int p) {
	int tot = 0;
	mxdep[u] = 1;
	for (auto to : g[u]) {
		if (to == p) continue;
		solve(to, u);
		tmpdis[++tot] = mxdep[to];
	}
	sort(tmpdis + 1, tmpdis + 1 + tot);
	if (ans != -1 && u != rt && d[u] > 2) {
		tot = unique(tmpdis + 1, tmpdis + 1 + tot) - tmpdis - 1;
		if (tot != 1) ans = -1;
	}
	mxdep[u] = tmpdis[tot] + 1;
}

int main() {
	//op;
	read(n);
	fo(i, 1, n - 1) {
		int u, v;
		read(u); read(v);
		g[u].push_back(v);
		g[v].push_back(u);
		d[u]++; d[v]++;
	}
	dep[1] = 1;
	dfs(1, 0);
	dep[mxnod] = 1;
	mx = 0;
	dfs2(mxnod, 0);
	int mid = (mx - 1) / 2;
	rt = mxnod;
	fo(i, 1, mid) rt = fa[rt];
	dfs(rt, 0);
	solve(rt, 0);
	if (ans == -1) printf("-1\n");
	else {
		int tot = 0;
		for (auto to : g[rt]) {
			tmpdis[++tot] = mxdep[to];
		}
		sort(tmpdis + 1, tmpdis + 1 + tot);
		tot = unique(tmpdis + 1, tmpdis + 1 + tot) - tmpdis - 1;
		if (tot > 2) ans = -1;
		else if (tot == 2) {
			ans = tmpdis[1] + tmpdis[2];
		}
		else {
			ans = tmpdis[1];
		}
		if (ans == -1) printf("-1\n");
		else {
			while (ans % 2 == 0) ans /= 2;
			printf("%d\n", ans);
		}
	}

	return 0;
}


#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<algorithm>
#include<map>
#include<vector>
#include<math.h>
#include<queue>
#include<stack>
#include<unordered_map>
#define fo(i,a,b) for(int i=a;i<=b;i++)
#define fod(i,a,b) for(int i=a;i>=b;i--)
#define me0(a) memset(a,0,sizeof(a))
#define me1(a) memset(a,-1,sizeof(a))
#define op freopen("in.txt", "r", stdin)
#define read(a) scanf("%d",&a)
#define mp(a,b) make_pair(a,b)
typedef long long LL;
using namespace std;
const int mod = 1e9 + 7;
const int maxn = 2e5 + 10;
const int INF = 0x3f3f3f3f;

vector<int>g[maxn];

int dep[maxn], mxdep[maxn];
int n, mx, mxnod;

void dfs(int u, int v) {
	if (mx < dep[u]) mx = dep[u], mxnod = u;
	for (auto to : g[u]) {
		if (to == v) continue;
		dep[to] = dep[u] + 1;
		dfs(to, u);
	}
}

int fa[maxn];
void dfs2(int u, int p) {
	fa[u] = p;
	if (mx < dep[u]) mx = dep[u], mxnod = u;
	for (auto to : g[u]) {
		if (to == p) continue;
		dep[to] = dep[u] + 1;
		dfs2(to, u);
	}
}

int ans;
int d[maxn];
int tmpdis[maxn];
int rt;

void solve(int u, int p) {
	int tot = 0;
	mxdep[u] = 1;
	for (auto to : g[u]) {
		if (to == p) continue;
		solve(to, u);
		tmpdis[++tot] = mxdep[to];
	}
	sort(tmpdis + 1, tmpdis + 1 + tot);
	if (ans != -1 && u != rt && d[u] > 2) {
		tot = unique(tmpdis + 1, tmpdis + 1 + tot) - tmpdis - 1;
		if (tot != 1) ans = -1;
	}
	mxdep[u] = tmpdis[tot] + 1;
}

int main() {
	//op;
	read(n);
	fo(i, 1, n - 1) {
		int u, v;
		read(u); read(v);
		g[u].push_back(v);
		g[v].push_back(u);
		d[u]++; d[v]++;
	}
	dep[1] = 1;
	dfs(1, 0);
	dep[mxnod] = 1;
	mx = 0;
	dfs2(mxnod, 0);
	int mid = mx / 2;
	rt = mxnod;
	fo(i, 1, mid) rt = fa[rt];
	dfs(rt, 0);
	solve(rt, 0);
	if (ans == -1) printf("-1\n");
	else {
		int tot = 0;
		for (auto to : g[rt]) {
			tmpdis[++tot] = mxdep[to];
		}
		sort(tmpdis + 1, tmpdis + 1 + tot);
		tot = unique(tmpdis + 1, tmpdis + 1 + tot) - tmpdis - 1;
		if (tot > 2) ans = -1;
		else if (tot == 2) {
			ans = tmpdis[1] + tmpdis[2];
		}
		else {
			ans = tmpdis[1];
		}
		if (ans == -1) printf("-1\n");
		else {
			while (ans % 2 == 0) ans /= 2;
			printf("%d\n", ans);
		}
	}

	return 0;
}


#include<bits/stdc++.h>

using namespace std;

#define all(x) x.begin(), x.end()
typedef vector <int> vi;
typedef pair<int,int> ii;
typedef long long ll;

const int MOD = 1e9 + 7;
const ll inf = 3e18 + 5;

vi adj[200005];
int D[200005];
vi path;
int root, target;
int s[200005];

void dfs(int x, int dad, int d){
	for(auto y : adj[x]){
		if(y == dad) continue;
		D[y] = d;
		dfs(y, x, d + 1);
	}
}

void dfs2(int x, int dad, int d){
	if(d == target) root = path[path.size() / 2];
	for(auto y : adj[x]){
		if(y == dad) continue;
		path.push_back(y);
		dfs2(y, x, d + 1);
		path.pop_back();
	}
}

bool bio[200005];

bool dpfs(int x){
	bio[x] = 1;
	s[x] = 1;
	bool dobar = 1;
	set <int> sz;
	for(auto y : adj[x]){
		if(bio[y]) continue;
		dobar &= dpfs(y);
		s[x] += s[y];
		sz.insert(s[y]);
	}
	if(sz.size() == 2){
		if(x != root) dobar = 0;
	}
	else if(sz.size() > 2) dobar = 0;
	return dobar;
}

int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(0);

	#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
	#endif

	int n;
	cin >> n;
	for(int i = 0; i < n; i++){
		int a, b;
		cin >> a >> b;
		adj[a].push_back(b);
		adj[b].push_back(a);
	}
	dfs(1, 1, 0);
	int slj, mx = 0;
	for(int i = 1; i <= n; i++){
		if(D[i] > mx){
			mx = D[i];
			slj = i;
		}
	}
	memset(D, 0, sizeof(D));
	dfs(slj, slj, 0);
	target = *max_element(D, D + n + 1);
	dfs2(slj, slj, 0); // za root
	bool ok = dpfs(root);
	cout << (ok ? target : -1);
}


#include<bits/stdc++.h>

using namespace std;

#define all(x) x.begin(), x.end()
typedef vector <int> vi;
typedef pair<int,int> ii;
typedef long long ll;

const int MOD = 1e9 + 7;
const ll inf = 3e18 + 5;

vi adj[300500];
int D[300500];
vi path;
int root, target;
int s[300500];
bool bio[300500];

void dfs(int x, int d){
	bio[x] = 1;
	for(auto y : adj[x]){
		if(bio[y]) continue;
		D[y] = d;
		dfs(y, d + 1);
	}
}

void dfs2(int x, int d){
	bio[x] = 1;
	if(d == target) root = path[path.size() / 2];
	for(auto y : adj[x]){
		if(bio[y]) continue;
		path.push_back(y);
		dfs2(y, d + 1);
		path.pop_back();
	}
}

bool dpfs(int x){
	bio[x] = 1;
	s[x] = 1;
	bool dobar = 1;
	set <int> sz;
	for(auto y : adj[x]){
		if(bio[y]) continue;
		dobar &= dpfs(y);
		s[x] += s[y];
		sz.insert(s[y]);
	}
	if(sz.size() == 2){
		if(x != root) dobar = 0;
	}
	else if(sz.size() > 2) dobar = 0;
	else{
		int sum = 0;
		for(auto X : sz){
			sum += X;
		}
		s[x] = sum + 1;
	}
	return dobar;
}

int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(0);

	#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
	#endif

	int n;
	cin >> n;
	for(int i = 0; i < n; i++){
		int a, b;
		cin >> a >> b;
		adj[a].push_back(b);
		adj[b].push_back(a);
	}
	dfs(1, 0);
	int slj, mx = 0;
	for(int i = 1; i <= n; i++){
		if(D[i] > mx){
			mx = D[i];
			slj = i;
		}
	}
	memset(D, 0, sizeof(D));
	memset(bio, 0, sizeof(bio));
	dfs(slj, 0);
	memset(bio, 0, sizeof(bio));
	target = *max_element(D, D + n + 1);
	dfs2(slj, 0); // za root
	memset(bio, 0, sizeof(bio));
	bool ok = dpfs(root);
	cout << (ok ? target : -1);
}


#include<bits/stdc++.h>

using namespace std;

#define all(x) x.begin(), x.end()
typedef vector <int> vi;
typedef pair<int,int> ii;
typedef long long ll;

const int MOD = 1e9 + 7;
const ll inf = 3e18 + 5;

vi adj[300500];
int D[300500];
vi path;
int root, target;
int s[300500];
bool bio[300500];

void dfs(int x, int d){
	bio[x] = 1;
	for(auto y : adj[x]){
		if(bio[y]) continue;
		D[y] = d;
		dfs(y, d + 1);
	}
}

void dfs2(int x, int d){
	bio[x] = 1;
	if(d == target) root = path[path.size() / 2];
	for(auto y : adj[x]){
		if(bio[y]) continue;
		path.push_back(y);
		dfs2(y, d + 1);
		path.pop_back();
	}
}

bool dpfs(int x){
	bio[x] = 1;
	s[x] = 1;
	bool dobar = 1;
	set <int> sz;
	for(auto y : adj[x]){
		if(bio[y]) continue;
		dobar &= dpfs(y);
		s[x] += s[y];
		sz.insert(s[y]);
	}
	if(sz.size() == 2){
		if(x != root) dobar = 0;
	}
	else if(sz.size() > 2) dobar = 0;
	return dobar;
}

int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(0);

	#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
	#endif

	int n;
	cin >> n;
	for(int i = 0; i < n; i++){
		int a, b;
		cin >> a >> b;
		adj[a].push_back(b);
		adj[b].push_back(a);
	}
	dfs(1, 0);
	int slj, mx = 0;
	for(int i = 1; i <= n; i++){
		if(D[i] > mx){
			mx = D[i];
			slj = i;
		}
	}
	memset(D, 0, sizeof(D));
	memset(bio, 0, sizeof(bio));
	dfs(slj, 0);
	memset(bio, 0, sizeof(bio));
	target = *max_element(D, D + n + 1);
	dfs2(slj, 0); // za root
	memset(bio, 0, sizeof(bio));
	bool ok = dpfs(root);
	cout << (ok ? target : -1);
}


#include<bits/stdc++.h>

using namespace std;

#define all(x) x.begin(), x.end()
typedef vector <int> vi;
typedef pair<int,int> ii;
typedef long long ll;

const int MOD = 1e9 + 7;
const ll inf = 3e18 + 5;

vi adj[200500];
int D[200500];
vi path;
int root, target;
int s[200500];
bool bio[200500];

void dfs(int x, int d){
	bio[x] = 1;
	D[x] = d;
	for(auto y : adj[x]){
		if(bio[y]) continue;
		dfs(y, d + 1);
	}
}

void dfs2(int x, int d){
	bio[x] = 1;
	D[x] = d;
	if(D[x] == target){
		root = path[path.size() / 2];
	}
	for(auto y : adj[x]){
		if(bio[y]) continue;
		path.push_back(y);
		dfs2(y, d + 1);
		path.pop_back();
	}
}

bool dpfs(int x){
	bio[x] = 1;
	s[x] = 1;
	bool dobar = 1;
	set <int> sz;
	for(auto y : adj[x]){
		if(bio[y]) continue;
		dobar &= dpfs(y);
		s[x] += s[y];
		sz.insert(s[y]);
	}
	if(sz.size() == 2 && x != root){
		if(x != root) dobar = 0;
	}
	else if(sz.size() > 2) dobar = 0;
	else{
		int sum = 0;
		for(auto X : sz){
			sum += X;
		}
		s[x] = sum + 1;
	}
	return dobar;
}

int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(0);

	#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
	#endif

	int n;
	cin >> n;
	for(int i = 0; i < n - 1; i++){
		int a, b;
		cin >> a >> b;
		adj[a].push_back(b);
		adj[b].push_back(a);
	}
	dfs(1, 1);
	int slj, mx = 0;
	for(int i = 1; i <= n; i++){
		if(D[i] > mx){
			mx = D[i];
			slj = i;
		}
	}
	memset(D, 0, sizeof(D));
	memset(bio, 0, sizeof(bio));
	dfs(slj, 1);
	memset(bio, 0, sizeof(bio));
	target = *max_element(D, D + n + 1);
	memset(D, 0, sizeof(D));
	path.push_back(slj);
	dfs2(slj, 1); // za root
	memset(bio, 0, sizeof(bio));
	bool ok = dpfs(root);
	int res = s[root] - 1;
	cout << (ok ? res : -1);
}


#include<bits/stdc++.h>

using namespace std;

#define all(x) x.begin(), x.end()
typedef vector <int> vi;
typedef pair<int,int> ii;
typedef long long ll;

const int MOD = 1e9 + 7;
const ll inf = 3e18 + 5;

vi adj[200005];
int D[200005];
vi path;
int root, target;
int s[200005];
bool bio[200005];

void dfs(int x, int d){
	bio[x] = 1;
	for(auto y : adj[x]){
		if(bio[y]) continue;
		D[y] = d;
		dfs(y, d + 1);
	}
}

void dfs2(int x, int d){
	bio[x] = 1;
	if(d == target) root = path[path.size() / 2];
	for(auto y : adj[x]){
		if(bio[y]) continue;
		path.push_back(y);
		dfs2(y, d + 1);
		path.pop_back();
	}
}

bool dpfs(int x){
	bio[x] = 1;
	s[x] = 1;
	bool dobar = 1;
	set <int> sz;
	for(auto y : adj[x]){
		if(bio[y]) continue;
		dobar &= dpfs(y);
		s[x] += s[y];
		sz.insert(s[y]);
	}
	if(sz.size() == 2){
		if(x != root) dobar = 0;
	}
	else if(sz.size() > 2) dobar = 0;
	return dobar;
}

int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(0);

	#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
	#endif

	int n;
	cin >> n;
	for(int i = 0; i < n; i++){
		int a, b;
		cin >> a >> b;
		adj[a].push_back(b);
		adj[b].push_back(a);
	}
	dfs(1, 0);
	int slj, mx = 0;
	for(int i = 1; i <= n; i++){
		if(D[i] > mx){
			mx = D[i];
			slj = i;
		}
	}
	memset(D, 0, sizeof(D));
	memset(bio, 0, sizeof(bio));
	dfs(slj, 0);
	memset(bio, 0, sizeof(bio));
	target = *max_element(D, D + n + 1);
	dfs2(slj, 0); // za root
	memset(bio, 0, sizeof(bio));
	bool ok = dpfs(root);
	cout << (ok ? target : -1);
}


#include<bits/stdc++.h>

using namespace std;

#define all(x) x.begin(), x.end()
typedef vector <int> vi;
typedef pair<int,int> ii;
typedef long long ll;

const int MOD = 1e9 + 7;
const ll inf = 3e18 + 5;

vi adj[200500];
int D[200500];
vi path;
int root, target;
int s[200500];
bool bio[200500];

void dfs(int x, int d){
	bio[x] = 1;
	for(auto y : adj[x]){
		if(bio[y]) continue;
		D[y] = d;
		dfs(y, d + 1);
	}
}

void dfs2(int x, int d){
	bio[x] = 1;
	if(d == target) root = path[path.size() / 2];
	for(auto y : adj[x]){
		if(bio[y]) continue;
		path.push_back(y);
		dfs2(y, d + 1);
		path.pop_back();
	}
}

bool dpfs(int x){
	bio[x] = 1;
	s[x] = 1;
	bool dobar = 1;
	set <int> sz;
	for(auto y : adj[x]){
		if(bio[y]) continue;
		dobar &= dpfs(y);
		s[x] += s[y];
		sz.insert(s[y]);
	}
	if(sz.size() == 2){
		if(x != root) dobar = 0;
	}
	else if(sz.size() > 2) dobar = 0;
	else{
		int sum = 0;
		for(auto X : sz){
			sum += X;
		}
		s[x] = sum + 1;
	}
	return dobar;
}

int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(0);

	#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
	#endif

	int n;
	cin >> n;
	for(int i = 0; i < n - 1; i++){
		int a, b;
		cin >> a >> b;
		adj[a].push_back(b);
		adj[b].push_back(a);
	}
	dfs(1, 0);
	int slj, mx = 0;
	for(int i = 1; i <= n; i++){
		if(D[i] > mx){
			mx = D[i];
			slj = i;
		}
	}
	memset(D, 0, sizeof(D));
	memset(bio, 0, sizeof(bio));
	dfs(slj, 0);
	memset(bio, 0, sizeof(bio));
	target = *max_element(D, D + n + 1) + 1;
	dfs2(slj, 1); // za root
	memset(bio, 0, sizeof(bio));
	bool ok = dpfs(root);
	cout << (ok ? target - 1 : -1);
}


#include<bits/stdc++.h>

using namespace std;

#define all(x) x.begin(), x.end()
typedef vector <int> vi;
typedef pair<int,int> ii;
typedef long long ll;

const int MOD = 1e9 + 7;
const ll inf = 3e18 + 5;

vi adj[200500];
int D[200500];
vi path;
int root, target;
int s[200500];
bool bio[200500];

void dfs(int x, int d){
	bio[x] = 1;
	D[x] = d;
	for(auto y : adj[x]){
		if(bio[y]) continue;
		dfs(y, d + 1);
	}
}

void dfs2(int x, int d){
	bio[x] = 1;
	D[x] = d;
	if(D[x] == target){
		root = path[path.size() / 2];
	}
	for(auto y : adj[x]){
		if(bio[y]) continue;
		path.push_back(y);
		dfs2(y, d + 1);
		path.pop_back();
	}
}

bool dpfs(int x){
	bio[x] = 1;
	bool dobar = 1;
	set <int> sz;
	for(auto y : adj[x]){
		if(bio[y]) continue;
		dobar &= dpfs(y);
		s[x] += s[y];
		sz.insert(s[y]);
	}
	if(sz.size() == 2 && x != root) dobar = 0;
	else if(sz.size() > 2) dobar = 0;
	int sum = 0;
	for(auto X : sz){
		sum += X;
	}
	s[x] = sum + 1;
	return dobar;
}

int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(0);

	#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
	#endif

	int n;
	cin >> n;
	for(int i = 0; i < n - 1; i++){
		int a, b;
		cin >> a >> b;
		adj[a].push_back(b);
		adj[b].push_back(a);
	}
	dfs(1, 1);
	int slj, mx = 0;
	for(int i = 1; i <= n; i++){
		if(D[i] > mx){
			mx = D[i];
			slj = i;
		}
	}
	memset(D, 0, sizeof(D));
	memset(bio, 0, sizeof(bio));
	dfs(slj, 1);
	memset(bio, 0, sizeof(bio));
	target = *max_element(D, D + n + 1);
	memset(D, 0, sizeof(D));
	path.push_back(slj);
	dfs2(slj, 1); // za root
	memset(bio, 0, sizeof(bio));
	bool ok = dpfs(root);
	int res = s[root] - 1;
	while(res % 2 == 0) res /= 2;
	cout << (ok ? res : -1);
}


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

#define st first
#define nd second
#define mp make_pair
#define pb push_back
#define cl(x, v) memset((x), (v), sizeof(x))
#define db(x) cerr << #x << " == " << x << endl
#define dbs(x) cerr << x << endl
#define _ << ", " <<
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<int, pii> piii;
typedef pair<ll, ll> pll;
typedef pair<ll, pll> plll;
typedef vector<int> vi;
typedef vector<vi> vii;
const ld EPS = 1e-9, PI = acos(-1.);
const ll LINF = 0x3f3f3f3f3f3f3f3f, LMOD = 1011112131415161719ll;
const int INF = 0x3f3f3f3f, MOD = 1e9+7;
const int N = 2*(1e5+5);

int n, vis[N], sz[N];
vi adj[N], cent, leaves, heights;
int h[N];
int f[N];
set<int> heightsS;

void dfs (int u) {
	bool ok = true;
	sz[u] = 1;

	for (auto v : adj[u]) if (!vis[v]) {
		vis[v] = 1;
		dfs(v);
		sz[u] += sz[v];
		if (2*sz[v] > n) ok = false;
	}

	if (ok and 2*(n-sz[u]) <= n) cent.pb(u);
}

void dfsh(int u, int hei){
    vis[u] = 1;
    h[u] = hei;
    bool isleaf = 1;
    for(auto v:adj[u]){
        if(!vis[v]){
            isleaf = 0;
            dfsh(v,hei+1);
        }
    }
    if(isleaf) leaves.pb(u);
}

int main () {
	cin >> n;
	int u, v;
	int ans = INF;

	for(int i=1;i<n;i++){
        cin >> u >> v;
        adj[u].pb(v);
        adj[v].pb(u);
	}

	for (int i = 1; i <= n; i++) if (!vis[i]) {
		vis[i] = 1;
		dfs(i);
	}
	//cout << "check 1\n";
	for (auto c : cent) { // centroids
        leaves.clear();
		for(int i=1;i<=n;i++){
            vis[i] = 0;
            f[i] = 0;
        }
        dfsh(c,0);
        //cout << "check 2\n";
        heights.clear();
        heightsS.clear();
        for(auto l : leaves){
            heights.pb(h[l]);
            heightsS.insert(h[l]);
        }
        //cout << "check 3\n";
        if(heightsS.size()>2) continue;
        //cout << "check cont\n";
        sort(heights.begin(),heights.end());
        for(auto ah : heights){
            f[ah]++;
        }
        //cout << "check precont\n";
        //cout << "DEBUG: leaves.size(): " << leaves.size() << endl;
        //cout << "DEBUG: heights.size(): " << heights.size() << endl;
        int preans = heights[0] + heights[heights.size()-1];

        //cout << "check premin?\n";
        ans = min(ans,preans);
        //cout << "check 4\n";
	}
	if(ans==INF){
        cout << "-1\n";
	}else{
	    cout << ans << "\n";
	}
	return 0;
}


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

#define st first
#define nd second
#define mp make_pair
#define pb push_back
#define cl(x, v) memset((x), (v), sizeof(x))
#define db(x) cerr << #x << " == " << x << endl
#define dbs(x) cerr << x << endl
#define _ << ", " <<
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<int, pii> piii;
typedef pair<ll, ll> pll;
typedef pair<ll, pll> plll;
typedef vector<int> vi;
typedef vector<vi> vii;
const ld EPS = 1e-9, PI = acos(-1.);
const ll LINF = 0x3f3f3f3f3f3f3f3f, LMOD = 1011112131415161719ll;
const int INF = 0x3f3f3f3f, MOD = 1e9+7;

const int N = 2*(1e5+10); // Maximum number of nodes
int vis[N];
int depth[N];
vi adj[N];
int n, diameter, r;
int m_depth = 0;
int w_depth = 1;

void dfs(int u, int h) {
	vis[u] = 1;
	depth[u] = h;
	if(h>m_depth){
        m_depth = h;
        w_depth = u;
	}
	for (int v : adj[u]) if (!vis[v]) dfs(v,h+1);
}

bool dfs2(int u, int h, int tgt){
    bool ans = 0;
    if(u==tgt) ans = 1;
    vis[u] = 1;
	depth[u] = h;
	if(h>m_depth){
        m_depth = h;
        w_depth = u;
	}
	for (int v : adj[u]) if (!vis[v]) ans = ans||dfs2(v,h+1,tgt);

	if(ans && (h==diameter/2)) r = u;

	return ans;
}

bool dfs3(int u, int h){
    if(adj[u].size()==1){
        if(m_depth!=-1){
            if(h!=m_depth){
                //cout << "DEBUG: LEAF #" << u << " IS ODD!\n";
                return 0;
            }
        }
        m_depth = max(m_depth,h);
    }

    vis[u] = 1;
	depth[u] = h;

	for (int v : adj[u]) if (!vis[v]) if(dfs3(v,h+1)==0) return 0;

	return 1;
}

void clear_all(){
    for(int i=0;i<=n;i++){
        vis[i] = 0;
        depth[i] = 0;
        m_depth = 0;
    }
}

int main(){
    cin >> n;

    int u, v;

    for(int i=1;i<n;i++){
        cin >> u >> v;
        adj[u].pb(v);
        adj[v].pb(u);
    }
    //FIND A:
    dfs(1,0);
    int a = w_depth;
    clear_all();
    //FIND B:
    dfs(a,0);
    int b = w_depth;
    diameter = m_depth;
    clear_all();
    //FIND ROOT:
    dfs2(a,0,b);
    clear_all();

    //cout << "DEBUG: ROOT: " << r << "\n";

    //FIND ALL HEIGHTS:
    vector<int> sub_heights;

    vis[r] = 1;
    for(auto sub_root:adj[r]){
        m_depth = -1;
        bool b = dfs3(sub_root,1);
        if(!b){
            //cout << "DEBUG: EXIT CODE 1\n";
            cout << "-1\n";
            return 0;
        }
        bool need_insert = 1;
        for(auto ins_i:sub_heights){
            if(ins_i==m_depth) need_insert = 0;
        }
        if(need_insert) sub_heights.pb(m_depth);
        if(sub_heights.size()>2){
            //cout << "DEBUG: EXIT CODE 2\n";
            cout << "-1\n";
            return 0;
        }
    }

    //FIND THE ANSWER:
    int fans = 1;

    if(sub_heights.size()==1){
        if(adj[r].size() > 1){
            fans = 2*sub_heights[0] + 1;
        }else{
            fans = sub_heights[0] + 1;
        }
    }else{
        fans = sub_heights[0] + sub_heights[1] + 1;
    }
    while(fans%2 && fans>1) fans = (fans/2)+1;

    cout << fans-1 << "\n";

    return 0;
}


#include <iostream>
#include <set>
#include <vector>
#include <string>
#include <algorithm>
#include <queue>
#include <set>
#include <cstring>
#include <map>
#include <bitset>
#include <random>
#include <stack>
#include <list>
#include <unordered_set>
#include <unordered_map>
#include <ctime>

using namespace std;

#define ll long long
#define ld long double
#define sc second
#define fs first
#define mp make_pair
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()

template<class T> T sqr(T x) { return x*x; }
ld pi = 3.1415926535897932384626433832795;

ll mod = 1e9 + 7;

ll gcd(ll a, ll b) {
	return b ? gcd(b, a % b) : a;
}

ll _gcd(ll a, ll b, ll & x, ll & y) {
	if (a == 0) {
		x = 0; y = 1;
		return b;
	}
	ll _x1, _y1;
	ll d = _gcd(b%a, a, _x1, _y1);
	x = _y1 - (b / a) * _x1;
	y = _x1;
	return d;
}

ll binpow(ll a, ll n) {
	if (n == -1) return 0;
	ll res = 1;
	while (n) {
		if (n & 1)
			res *= a;
		a *= a;
		n >>= 1;
		a %= mod;
		res %= mod;
	}
	return res;
}

const int N = 2e5 + 10;

vector<int> g[N];
int dist[N];
int n;

int ans = 1e9;
int tt = -1;

int dfs(int v, int p = 0, int ln = 0){
	set<int> lens;
	int new_len = ln + 1, go_up = 0;
	if (ln == -1 || g[v].size() > 2) new_len = -1;
	for (int i = 0; i < g[v].size(); i++){
		int to = g[v][i];
		if (to == p) continue;
		int get = dfs(to, v, new_len);
		if (get == -1)
			return -1;
		else
			lens.insert(get);
	}
	if (lens.size() == 0)
		go_up = 1;
	if (lens.size() == 1)
		go_up = 1+*lens.begin();
	int lenn = lens.size();
	if ((lens.size() == 1 && ln != -1) || (lens.size() == 2 && !lens.count(ln)){
		tt = v;
	}
	if (lens.size() > 2 || (lens.size() == 2 && !lens.count(ln))){
		return -1;
	}
	if (ln != -1){
		lens.insert(ln);
		lens.erase(0);
		set<int>::iterator it;
		int temp = 0;
		for (it = lens.begin(); it != lens.end(); it++){
			temp += *it;
		}
		while (temp % 2 == 0) temp /= 2;
		ans = min(ans, temp);
	}
	if (lenn > 1) go_up = -1;
	return go_up;
}

int main()
{
	cin >> n;
	for (int i = 1; i < n; i++){
		int u, v;
		cin >> u >> v;
		g[u].push_back(v);
		g[v].push_back(u);
	}
	int start = 1;
	while (g[start].size() > 1) start++;
	dist[start] = 0;
	dfs(start);
	dfs(tt);
	if (ans == 1e9) cout << -1;
	else cout << ans;
	return 0;
}


#include <iostream>
#include <set>
#include <vector>
#include <string>
#include <algorithm>
#include <queue>
#include <set>
#include <cstring>
#include <map>
#include <bitset>
#include <random>
#include <stack>
#include <list>
#include <unordered_set>
#include <unordered_map>
#include <ctime>

using namespace std;

#define ll long long
#define ld long double
#define sc second
#define fs first
#define mp make_pair
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()

template<class T> T sqr(T x) { return x*x; }
ld pi = 3.1415926535897932384626433832795;

ll mod = 1e9 + 7;

ll gcd(ll a, ll b) {
	return b ? gcd(b, a % b) : a;
}

ll _gcd(ll a, ll b, ll & x, ll & y) {
	if (a == 0) {
		x = 0; y = 1;
		return b;
	}
	ll _x1, _y1;
	ll d = _gcd(b%a, a, _x1, _y1);
	x = _y1 - (b / a) * _x1;
	y = _x1;
	return d;
}

ll binpow(ll a, ll n) {
	if (n == -1) return 0;
	ll res = 1;
	while (n) {
		if (n & 1)
			res *= a;
		a *= a;
		n >>= 1;
		a %= mod;
		res %= mod;
	}
	return res;
}

const int N = 2e5 + 10;

vector<int> g[N];
int dist[N];
int n;

int ans = 1e9;

int dfs(int v, int p = 0, int ln = 0){
	set<int> lens;
	int new_len = (ln == -1)?-1:(ln + 1);
	if (ln == 0){
		if (g[v].size() != 1) new_len = -1;
	}
	else {
		if (g[v].size() != 2) new_len = -1;
	}
	int go_up = 0;
	for (int i = 0; i < g[v].size(); i++){
		int to = g[v][i];
		if (to == p) continue;
		dist[to] = dist[v] + 1;
		int get = dfs(to, v, new_len);
		if (get == -1) go_up = -1;
		else{
			lens.insert(get);
		}
	}
	if (go_up == -1) return -1;
	if (lens.size() == 0 && go_up != -1){
		go_up = 1;
	}
	if (lens.size() == 1 && go_up != -1){
		go_up = 1+*lens.begin();
	}
	int lenn = lens.size();
	if (lens.size() > 2 || (lens.size() == 2 && !lens.count(ln)))
		go_up = -1;
	if (ln != -1 && go_up != -1){
		if (ln != 0)
			lens.insert(ln);
		if (lens.size() < 3){
			set<int>::iterator it;
			int temp = 0;
			for (it = lens.begin(); it != lens.end(); it++){
				temp += *it;
			}
			while (temp % 2 == 0) temp /= 2;
			ans = min(ans, temp);
		}
	}
	if (lenn > 1) go_up = -1;
	return go_up;
}

int main()
{
	cin >> n;
	for (int i = 1; i < n; i++){
		int u, v;
		cin >> u >> v;
		g[u].push_back(v);
		g[v].push_back(u);
	}
	int start = 1;
	while (g[start].size() > 1) start++;
	dist[start] = 0;
	dfs(start);
	int next_one = 0;
	for (int i = 1; i <= n; i++){
		if (i != start && g[i].size() == 1)
		{
			if (dist[next_one] < dist[i])
				next_one = i;
		}
	}
	dfs(next_one);
	if (ans == 1e9) cout << -1;
	else cout << ans;
	return 0;
}


#include <iostream>
#include <set>
#include <vector>
#include <string>
#include <algorithm>
#include <queue>
#include <set>
#include <cstring>
#include <map>
#include <bitset>
#include <random>
#include <stack>
#include <list>
#include <unordered_set>
#include <unordered_map>
#include <ctime>

using namespace std;

#define ll long long
#define ld long double
#define sc second
#define fs first
#define mp make_pair
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()

template<class T> T sqr(T x) { return x*x; }
ld pi = 3.1415926535897932384626433832795;

ll mod = 1e9 + 7;

ll gcd(ll a, ll b) {
	return b ? gcd(b, a % b) : a;
}

ll _gcd(ll a, ll b, ll & x, ll & y) {
	if (a == 0) {
		x = 0; y = 1;
		return b;
	}
	ll _x1, _y1;
	ll d = _gcd(b%a, a, _x1, _y1);
	x = _y1 - (b / a) * _x1;
	y = _x1;
	return d;
}

ll binpow(ll a, ll n) {
	if (n == -1) return 0;
	ll res = 1;
	while (n) {
		if (n & 1)
			res *= a;
		a *= a;
		n >>= 1;
		a %= mod;
		res %= mod;
	}
	return res;
}

const int N = 2e5 + 10;

vector<int> g[N];
int dist[N];
int n;

int ans = 1e9;

int dfs(int v, int p = 0, int ln = 0){
	set<int> lens;
	int new_len = (ln == -1)?-1:(ln + 1);
	if (ln == 0){
		if (g[v].size() != 1) new_len = -1;
	}
	else {
		if (g[v].size() != 2) new_len = -1;
	}
	int go_up = 0;
	for (int i = 0; i < g[v].size(); i++){
		int to = g[v][i];
		if (to == p) continue;
		dist[to] = dist[v] + 1;
		int get = dfs(to, v, new_len);
		if (get == -1) go_up = -1;
		else{
			lens.insert(get);
		}
	}
	if (go_up == -1) return -1;
	if (lens.size() == 0 && go_up != -1){
		go_up = 1;
	}
	if (lens.size() == 1 && go_up != -1){
		go_up = 1+*lens.begin();
	}
	int lenn = lens.size();
	if (lens.size() > 2 || (lens.size() == 2 && !lens.count(ln)))
		go_up = -1;
	if (ln != -1 && go_up != -1){
		if (ln != 0)
			lens.insert(ln);
		if (lens.size() < 3){
			set<int>::iterator it;
			int temp = 0;
			for (it = lens.begin(); it != lens.end(); it++){
				temp += *it;
			}
			while (temp % 2 == 0) temp /= 2;
			ans = min(ans, temp);
		}
	}
	if (lenn > 1) go_up = -1;
	return go_up;
}

int main()
{
	cin >> n;
	for (int i = 1; i < n; i++){
		int u, v;
		cin >> u >> v;
		g[u].push_back(v);
		g[v].push_back(u);
	}
	int start = 1;
	while (g[start].size() > 1) start++;
	dist[start] = 0;
	dfs(start);
	/*int next_one = 0;
	for (int i = 1; i <= n; i++){
		if (i != start && g[i].size() == 1)
		{
			if (dist[next_one] < dist[i])
				next_one = i;
		}
	}
	dfs(next_one);*/
	if (ans == 1e9) cout << -1;
	else cout << ans;
	return 0;
}


#include <iostream>
#include <set>
#include <vector>
#include <string>
#include <algorithm>
#include <queue>
#include <set>
#include <cstring>
#include <map>
#include <bitset>
#include <random>
#include <stack>
#include <list>
#include <unordered_set>
#include <unordered_map>
#include <ctime>

using namespace std;

#define ll long long
#define ld long double
#define sc second
#define fs first
#define mp make_pair
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()

template<class T> T sqr(T x) { return x*x; }
ld pi = 3.1415926535897932384626433832795;

ll mod = 1e9 + 7;

ll gcd(ll a, ll b) {
	return b ? gcd(b, a % b) : a;
}

ll _gcd(ll a, ll b, ll & x, ll & y) {
	if (a == 0) {
		x = 0; y = 1;
		return b;
	}
	ll _x1, _y1;
	ll d = _gcd(b%a, a, _x1, _y1);
	x = _y1 - (b / a) * _x1;
	y = _x1;
	return d;
}

ll binpow(ll a, ll n) {
	if (n == -1) return 0;
	ll res = 1;
	while (n) {
		if (n & 1)
			res *= a;
		a *= a;
		n >>= 1;
		a %= mod;
		res %= mod;
	}
	return res;
}

const int N = 2e5 + 10;

vector<int> g[N];
int dist[N];
int n;

int ans = 1e9;

int dfs(int v, int p = 0, int ln = 0){
	set<int> lens;
	int new_len = ln + 1, go_up = 0;
	if (ln == -1 || g[v].size() > 2) new_len = -1;
	for (int i = 0; i < g[v].size(); i++){
		int to = g[v][i];
		if (to == p) continue;
		int get = dfs(to, v, new_len);
		if (get == -1)
			return -1;
		else
			lens.insert(get);
	}
	if (lens.size() == 0)
		go_up = 1;
	if (lens.size() == 1)
		go_up = 1+*lens.begin();
	int lenn = lens.size();
	if (lens.size() > 2 || (lens.size() == 2 && !lens.count(ln))){
		return -1;
	}
	if (ln != -1){
		lens.insert(ln);
		lens.erase(0);
		set<int>::iterator it;
		int temp = 0;
		for (it = lens.begin(); it != lens.end(); it++){
			temp += *it;
		}
		while (temp % 2 == 0) temp /= 2;
		ans = min(ans, temp);
	}
	if (lenn > 1) go_up = -1;
	return go_up;
}

int main()
{
	cin >> n;
	for (int i = 1; i < n; i++){
		int u, v;
		cin >> u >> v;
		g[u].push_back(v);
		g[v].push_back(u);
	}
	int start = 1;
	while (g[start].size() > 1) start++;
	dist[start] = 0;
	dfs(start);
	if (ans == 1e9) cout << -1;
	else cout << ans;
	return 0;
}


#include <iostream>
#include <set>
#include <vector>
#include <string>
#include <algorithm>
#include <queue>
#include <set>
#include <cstring>
#include <map>
#include <bitset>
#include <random>
#include <stack>
#include <list>
#include <unordered_set>
#include <unordered_map>
#include <ctime>

using namespace std;

#define ll long long
#define ld long double
#define sc second
#define fs first
#define mp make_pair
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()

template<class T> T sqr(T x) { return x*x; }
ld pi = 3.1415926535897932384626433832795;

ll mod = 1e9 + 7;

ll gcd(ll a, ll b) {
	return b ? gcd(b, a % b) : a;
}

ll _gcd(ll a, ll b, ll & x, ll & y) {
	if (a == 0) {
		x = 0; y = 1;
		return b;
	}
	ll _x1, _y1;
	ll d = _gcd(b%a, a, _x1, _y1);
	x = _y1 - (b / a) * _x1;
	y = _x1;
	return d;
}

ll binpow(ll a, ll n) {
	if (n == -1) return 0;
	ll res = 1;
	while (n) {
		if (n & 1)
			res *= a;
		a *= a;
		n >>= 1;
		a %= mod;
		res %= mod;
	}
	return res;
}

const int N = 2e5 + 10;

vector<int> g[N];
int n;

int ans = 1e9;

int dfs(int v, int p = 0, int ln = 0){
	set<int> lens;
	if (ln == -1){
		int ere = 123;
	}
	int new_len = (ln == -1)?-1:(ln + 1);
	if (g[v].size() != 1) new_len = -1;
	int go_up = 0;
	for (int i = 0; i < g[v].size(); i++){
		int to = g[v][i];
		if (to == p) continue;
		int get = dfs(to, v, new_len);
		if (get == -1) go_up = -1;
		else{
			lens.insert(get);
		}
	}
	if (lens.size() == 0 && go_up != -1){
		go_up = 1;
	}
	if (lens.size() == 1 && go_up != -1){
		go_up = 1+*lens.begin();
	}
	if (lens.size() > 1) go_up = -1;
	if (ln != -1 && go_up != -1){
		if (ln != 0)
			lens.insert(ln);
		if (lens.size() == 1){
			int temp = *lens.begin();
			while (temp % 2 == 0) temp /= 2;
			ans = min(ans, temp);
		}
		if (lens.size() == 2){
			set<int>::iterator it;
			int temp = 0;
			for (it = lens.begin(); it != lens.end(); it++){
				temp += *it;
			}
			while (temp % 2 == 0) temp /= 2;
			ans = min(ans, temp);
		}
	}
	return go_up;
}

int main()
{
	cin >> n;
	for (int i = 1; i < n; i++){
		int u, v;
		cin >> u >> v;
		g[u].push_back(v);
		g[v].push_back(u);
	}
	int start = 1;
	while (g[start].size() > 1) start++;
	dfs(start);
	if (ans == 1e9) cout << -1;
	else cout << ans;
	return 0;
}


#include <iostream>
#include <set>
#include <vector>
#include <string>
#include <algorithm>
#include <queue>
#include <set>
#include <cstring>
#include <map>
#include <bitset>
#include <random>
#include <stack>
#include <list>
#include <unordered_set>
#include <unordered_map>
#include <ctime>

using namespace std;

#define ll long long
#define ld long double
#define sc second
#define fs first
#define mp make_pair
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()

template<class T> T sqr(T x) { return x*x; }
ld pi = 3.1415926535897932384626433832795;

ll mod = 1e9 + 7;

ll gcd(ll a, ll b) {
	return b ? gcd(b, a % b) : a;
}

ll _gcd(ll a, ll b, ll & x, ll & y) {
	if (a == 0) {
		x = 0; y = 1;
		return b;
	}
	ll _x1, _y1;
	ll d = _gcd(b%a, a, _x1, _y1);
	x = _y1 - (b / a) * _x1;
	y = _x1;
	return d;
}

ll binpow(ll a, ll n) {
	if (n == -1) return 0;
	ll res = 1;
	while (n) {
		if (n & 1)
			res *= a;
		a *= a;
		n >>= 1;
		a %= mod;
		res %= mod;
	}
	return res;
}

const int N = 2e5 + 10;

vector<int> g[N];
int n;

int ans = 1e9;

int dfs(int v, int p = 0, int ln = 0){
	set<int> lens;
	int new_len = (ln == -1)?-1:(ln + 1);
	if (g[v].size() != 1) new_len = -1;
	int go_up = 0;
	for (int i = 0; i < g[v].size(); i++){
		int to = g[v][i];
		if (to == p) continue;
		int get = dfs(to, v, new_len);
		if (get == 0){
			int ss = 213;
		}
		if (get == -1) go_up = -1;
		else{
			lens.insert(get);
		}
	}
	if (lens.size() == 0 && go_up != -1){
		go_up = 1;
	}
	if (lens.size() == 1 && go_up != -1){
		go_up = 1+*lens.begin();
	}
	if (lens.size() == 2){
		int d432 = 123;
	}
	if (lens.size() == 2){
		int s234 = 12;
	}
	int lenn = lens.size();
	if (lens.size() > 2 || (lens.size() == 2 && !lens.count(ln)))
		go_up = -1;
	if (ln != -1 && go_up != -1){
		if (ln != 0)
			lens.insert(ln);
		if (lens.size() < 3){
			set<int>::iterator it;
			int temp = 0;
			for (it = lens.begin(); it != lens.end(); it++){
				temp += *it;
			}
			while (temp % 2 == 0) temp /= 2;
			ans = min(ans, temp);
		}
	}
	if (lenn > 1) go_up = -1;
	return go_up;
}

int main()
{
	cin >> n;
	for (int i = 1; i < n; i++){
		int u, v;
		cin >> u >> v;
		g[u].push_back(v);
		g[v].push_back(u);
	}
	int start = 1;
	while (g[start].size() > 1) start++;
	dfs(start);
	if (ans == 1e9) cout << -1;
	else cout << ans;
	return 0;
}


#include <iostream>
#include <set>
#include <vector>
#include <string>
#include <algorithm>
#include <queue>
#include <set>
#include <cstring>
#include <map>
#include <bitset>
#include <random>
#include <stack>
#include <list>
#include <unordered_set>
#include <unordered_map>
#include <ctime>

using namespace std;

#define ll long long
#define ld long double
#define sc second
#define fs first
#define mp make_pair
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()

template<class T> T sqr(T x) { return x*x; }
ld pi = 3.1415926535897932384626433832795;

ll mod = 1e9 + 7;

ll gcd(ll a, ll b) {
	return b ? gcd(b, a % b) : a;
}

ll _gcd(ll a, ll b, ll & x, ll & y) {
	if (a == 0) {
		x = 0; y = 1;
		return b;
	}
	ll _x1, _y1;
	ll d = _gcd(b%a, a, _x1, _y1);
	x = _y1 - (b / a) * _x1;
	y = _x1;
	return d;
}

ll binpow(ll a, ll n) {
	if (n == -1) return 0;
	ll res = 1;
	while (n) {
		if (n & 1)
			res *= a;
		a *= a;
		n >>= 1;
		a %= mod;
		res %= mod;
	}
	return res;
}

const int N = 2e5 + 10;

vector<int> g[N];
int dist[N];
int n;

int ans = 1e9;

int dfs(int v, int p = 0, int ln = 0){
	set<int> lens;
	int new_len = (ln == -1)?-1:(ln + 1);
	if (ln == 0){
		if (g[v].size() != 1) new_len = -1;
	}
	else {
		if (g[v].size() != 2) new_len = -1;
	}
	int go_up = 0;
	for (int i = 0; i < g[v].size(); i++){
		int to = g[v][i];
		if (to == p) continue;
		int get = dfs(to, v, new_len);
		if (get == -1){
			return -1;
		}
		else{
			lens.insert(get);
		}
	}
	if (lens.size() == 0){
		go_up = 1;
	}
	if (lens.size() == 1){
		go_up = 1+*lens.begin();
	}
	int lenn = lens.size();
	if (lens.size() > 2 || (lens.size() == 2 && !lens.count(ln))){
		return -1;
	}
	if (ln != -1 && go_up != -1){
		if (ln != 0)
			lens.insert(ln);
		else{
			int rfd = 2;
		}
		if (lens.size() < 3 && lens.size() > 0){
			set<int>::iterator it;
			int temp = 0;
			for (it = lens.begin(); it != lens.end(); it++){
				temp += *it;
			}
			while (temp % 2 == 0) temp /= 2;
			ans = min(ans, temp);
		}
	}
	if (lenn > 1) go_up = -1;
	if (go_up == 0){
		int fef = 213;
	}
	return go_up;
}

int main()
{
	cin >> n;
	for (int i = 1; i < n; i++){
		int u, v;
		cin >> u >> v;
		g[u].push_back(v);
		g[v].push_back(u);
	}
	int start = 1;
	while (g[start].size() > 1) start++;
	dist[start] = 0;
	dfs(start);
	int next_one = 0;
	for (int i = 1; i <= n; i++){
		if (i != start && g[i].size() == 1)
		{
			if (dist[next_one] < dist[i])
				next_one = i;
		}
	}
	dfs(next_one);
	if (ans == 1e9) cout << -1;
	else cout << ans;
	return 0;
}