#include <iostream>
#include <fstream>
#include <vector>
#include <string>
using namespace std;

typedef long long ll;

//#define cin ff
#ifdef cin
ifstream ff("file.txt");
#endif

bool checkstring(string &str){
	int _count = 0;
	for (int i = 0; i < str.length(); i++){
		if (str[i] == '0')
			_count++;
	}
	if (_count == str.length()-1)
		return true;
	else
		return false;
}

int main(){
	int n;
	cin >> n;
	//ios_base::sync_with_stdio(0);
	//cin.tie(0);
	vector <string> vs(n);
	bool NullAnsw = false;
	int countNull = 0;
	string NotButifull;
	for (int i = 0; i < n; i++){
		cin >> vs[i];
		if (vs[i] == "0")
			NullAnsw = true;
		else if (vs[i][0] == '1'){
			if (checkstring(vs[i])==true)
				countNull += vs[i].size() - 1;
			else
				NotButifull = vs[i];
		}
		else
			NotButifull = vs[i];
	}
	if (NullAnsw == true)
		cout << 0 << endl;
	else{
		if (NotButifull == ""){
			countNull--;
			putchar('1');
		}
		else
			cout << NotButifull;
		while (countNull--)
			putchar('0');
	}
	return 0;
}


#include <iostream>
#include <fstream>
#include <vector>
#include <string>
using namespace std;

typedef long long ll;

//#define cin ff
#ifdef cin
ifstream ff("file.txt");
#endif

bool checkstring(string &str){
	int _count = 0;
	for (int i = 0; i < str.length(); i++){
		if (str[i] == '0')
			_count++;
	}
	if (_count == str.length()-1)
		return true;
	else
		return false;
}

int main(){
	int n;
	cin >> n;
	ios_base::sync_with_stdio(false);
	//cin.tie(0);
	vector <string> vs(n);
	bool NullAnsw = false;
	int countNull = 0;
	string NotButifull;
	for (int i = 0; i < n; i++){
		cin >> vs[i];
		if (vs[i] == "0")
			NullAnsw = true;
		else if (vs[i][0] == '1'){
			if (checkstring(vs[i])==true)
				countNull += vs[i].size() - 1;
			else
				NotButifull = vs[i];
		}
		else
			NotButifull = vs[i];
	}
	if (NullAnsw == true)
		cout << 0 << endl;
	else{
		if (NotButifull == ""){
			//countNull--;
			putchar('1');
		}
		else
			cout << NotButifull;
		while (countNull--)
			putchar('0');
	}
	return 0;
}


#include <iostream>
#include <fstream>
#include <vector>
#include <string>
using namespace std;

typedef long long ll;

//#define cin ff
#ifdef cin
ifstream ff("file.txt");
#endif

bool checkstring(string &str){
	int _count = 0;
	for (int i = 0; i < str.length(); i++){
		if (str[i] == '0')
			_count++;
	}
	if (_count == str.length()-1)
		return true;
	else
		return false;
}

int main(){
	int n;
	cin >> n;
	//ios_base::sync_with_stdio(0);
	//cin.tie(0);
	vector <string> vs(n);
	bool NullAnsw = false;
	int countNull = 0;
	string NotButifull;
	for (int i = 0; i < n; i++){
		cin >> vs[i];
		if (vs[i] == "0")
			NullAnsw = true;
		else if (vs[i][0] == '1'){
			if (checkstring(vs[i])==true)
				countNull += vs[i].size() - 1;
			else
				NotButifull = vs[i];
		}
		else
			NotButifull = vs[i];
	}
	if (NullAnsw == true)
		cout << 0 << endl;
	else{
		cout << NotButifull;
		while (countNull--)
			putchar('0');
	}
	return 0;
}


#include <iostream>
#include <fstream>
#include <vector>
#include <string>
using namespace std;

typedef long long ll;

//#define cin ff
#ifdef cin
ifstream ff("file.txt");
#endif

bool checkstring(string &str){
	int _count = 0;
	for (int i = 0; i < str.length(); i++){
		if (str[i] == '0')
			_count++;
	}
	if (_count == str.length()-1)
		return true;
	else
		return false;
}

int main(){
	int n;
	cin >> n;
	//ios_base::sync_with_stdio(0);
	//cin.tie(0);
	vector <string> vs(n);
	bool NullAnsw = false;
	int countNull = 0;
	string NotButifull;
	for (int i = 0; i < n; i++){
		cin >> vs[i];
		if (vs[i] == "0")
			NullAnsw = true;
		else if (vs[i][0] == '1'){
			if (checkstring(vs[i])==true)
				countNull += vs[i].size() - 1;
			else
				NotButifull = vs[i];
		}
		else
			NotButifull = vs[i];
	}
	if (NullAnsw == true)
		cout << 0 << endl;
	else{
		if (NotButifull == ""){
			//countNull--;
			putchar('1');
		}
		else
			cout << NotButifull;
		while (countNull--)
			putchar('0');
	}
	return 0;
}


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

const int MAXN = 2e5+5;
vector<int> g[MAXN];
int R = -1, ans = 0;

int check(int u, int fa) {
    int dep = -1;
    for(int i = 0; i < g[u].size(); i++) {
        int v = g[u][i];
        if(v == fa) continue;
        int nowdep = check(v, u);
        if(dep == -1) dep = nowdep;
        else if(dep != nowdep) {
            if(fa!=-1) {
                puts("-1");
                exit(0);
            }
            else if(R == -1) R = nowdep;
            else if(R!=nowdep) {
                puts("-1");
                exit(0);
            }
        }
    }
    if(fa==-1 && ans!=-1) {
        ans = dep+R+3;
        while(ans&1) ans -= ans/2;
        ans--;
        printf("%d\n", ans);
        exit(0);
    }
    return dep+1;
}

int dfs(int u, int fa) {
    int dep = -1;
    for(int i = 0; i < g[u].size(); i++) {
        int v = g[u][i];
        if(v == fa) continue;
        int nowdep = dfs(v, u);
        if(dep == -1) dep = nowdep;
        else if(dep != nowdep) check(u, -1);
    }
    if(fa == -1) check(u, -1);
    return dep+1;
}

int main() {
    int n; scanf("%d", &n);
    for(int i = 0; i < n-1; i++) {
        int u, v;
        scanf("%d%d", &u, &v);
        g[u].push_back(v);
        g[v].push_back(u);
    }
    dfs(1, -1);
    printf("%d\n", ans);
}


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

const int MAXN = 2e5+5;
vector<int> g[MAXN];
int R = -1, ans = 0;

int rint(){
	int x=0,f=1;char ch=getchar();
	while ((ch<'0'||ch>'9')&&(ch!='-'))ch=getchar();
	if (ch=='-')f=-1,ch=getchar();
	while (ch>='0'&&ch<='9')x=x*10+ch-'0',ch=getchar();
	return f*x;
}

int check(int u, int fa) {
    int dep = -1;
    for(int i = 0; i < g[u].size(); i++) {
        int v = g[u][i];
        if(v == fa) continue;
        int nowdep = check(v, u);
        if(dep == -1) dep = nowdep;
        else if(dep != nowdep) {
            if(fa!=-1) {
                puts("-1");
                exit(0);
            }
            else if(R == -1) R = nowdep;
            else if(R!=nowdep) {
                puts("-1");
                exit(0);
            }
        }
    }
    if(fa==-1 && ans!=-1) {
        ans = dep+R+3;
        while(ans&1) ans -= ans/2;
        ans--;
        printf("%d\n", ans);
        exit(0);
    }
    return dep+1;
}

int dfs(int u, int fa) {
    int dep = -1;
    for(int i = 0; i < g[u].size(); i++) {
        int v = g[u][i];
        if(v == fa) continue;
        int nowdep = dfs(v, u);
        if(dep == -1) dep = nowdep;
        else if(dep != nowdep) check(u, -1);
    }
    if(fa == -1) check(u, -1);
    return dep+1;
}

int main() {
    int n; n = rint();
    for(int i = 0; i < n-1; i++) {
        int u, v;
        u = rint(), v = rint();
        g[u].push_back(v);
        g[v].push_back(u);
    }
    dfs(1, -1);
    printf("%d\n", ans);
}


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

const int MAXN = 2e5+5;
vector<int> G[MAXN];
int dp[MAXN], len[MAXN];

void addEdge(int u, int v) {
    G[u].push_back(v);
    G[v].push_back(u);
}

int dfs(int u, int fa) {
    dp[u] = 1, len[u] = (int)G[u].size();
    bool vis[MAXN];
    memset(vis, 0, sizeof(vis));
    for(int i = 0; i < G[u].size(); i++) {
        int &v = G[u][i];
        if(v == fa) continue;
        int t = dfs(v, u);
        if(len[v] > 2) dp[u] += t;
        else {
            if(!vis[t]) {

                vis[t] = 1, dp[u] += t;
//                if(u == 2) printf("%d \n", vis[t]);
            }
            else {
                len[u]--, v = -1;

            }
        }
//        if(u == 2) printf("%d %d %d %d\n", t, vis[t], dp[2], len[v]);
    }
    return dp[u];
}

int main() {
    int n;
    scanf("%d", &n);
    for(int i = 1; i < n; i++) {
        int u, v;
        scanf("%d%d", &u, &v);
        addEdge(u, v);
    }

    int root;
    for(int i = 1; i <= n; i++) if(G[i].size() == 1) {
        root = i;
        break;
    }
    memset(dp, 0, sizeof(dp));
    dfs(root, -1);
    int node = 1;
    for(int i = 1; i <= n; i++) if(len[i] > len[node]) node = i;

//    printf("%d %d\n", dp[root], len[1]);

    if(len[node] == 1) printf("%d\n", dp[root]-1);
    else if(len[node] == 2) {
        int ans = dp[root];
        while(ans&1) {
            ans -= ans/2;
        }
        printf("%d\n", ans-1);
    }
    else if(len[node] <= 4) {
        bool flag = 1;
        for(int i = 1; i <= n; i++) if(i != node) {
            if(len[node] >= 3) {
                flag = 0;
                break;
            }
        }
        if(flag) {
            int a[10], k = 0, ans = 0;
            for(auto v:G[node]) {
                if(v == -1) continue;
                a[k++] = dp[v];
                ans += dp[v];
            }
            a[k++] = dp[root]-dp[node];
            ans += a[k-1];
            sort(a, a+k);
            bool ok = 0;
            for(int i = k-2; i >= 0; i--) {
                if(a[i] == a[i+1]) {
                    ans -= a[i];
                    ok = 1;
                    break;
                }
            }
            if(ok) {
                while(ans&1) {
                    ans -= ans/2;
                }
                printf("%d\n", ans-1);
            }
            else puts("-1");
        }
        else puts("-1");
    }
    else puts("-1");
}


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

const int MAXN = 2e5+5;
vector<int> g[MAXN];
int R = -1, ans = 0;

int check(int u, int fa) {
    int dep = -1;
    if(ans) {
        printf("%d\n", ans);
        exit(0);
    }
    for(int i = 0; i < g[u].size(); i++) {
        int v = g[u][i];
        if(v == fa) continue;
        int nowdep = check(v, u);
        if(dep == -1) dep = nowdep;
        else if(dep != nowdep) {
            if(fa!=-1 && !ans) ans = -1;
            else if(R == -1) R = nowdep;
            else if(R!=nowdep && !ans) ans = -1;
        }
        if(ans) {
            printf("%d\n", ans);
            exit(0);
        }
    }
    if(fa==-1 && ans!=-1) {
        ans = dep+R+3;
        while(ans&1) ans -= ans/2;
        ans--;
    }
    return dep+1;
}

int dfs(int u, int fa) {
    int dep = -1;
    if(ans) {
        printf("%d\n", ans);
        exit(0);
    }
    for(int i = 0; i < g[u].size(); i++) {
        int v = g[u][i];
        if(v == fa) continue;
        int nowdep = dfs(v, u);
        if(dep == -1) dep = nowdep;
        else if(dep != nowdep) check(u, -1);
        if(ans) {
            printf("%d\n", ans);
            exit(0);
        }
    }
    if(fa == -1) check(u, -1);
    return dep+1;
}

int main() {
    int n; scanf("%d", &n);
    for(int i = 0; i < n-1; i++) {
        int u, v;
        scanf("%d%d", &u, &v);
        g[u].push_back(v);
        g[v].push_back(u);
    }
    dfs(1, -1);
    printf("%d\n", ans);
}


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

const int MAXN = 2e5+5;
vector<int> G[MAXN];
int dp[MAXN], len[MAXN], a[MAXN];

void addEdge(int u, int v) {
    G[u].push_back(v);
    G[v].push_back(u);
}

int dfs(int u, int fa) {
    dp[u] = 1, len[u] = (int)G[u].size();
    bool vis[MAXN];
    memset(vis, 0, sizeof(vis));
    int k = 0;
    for(int i = 0; i < G[u].size(); i++) {
        int &v = G[u][i];
        if(v == fa) continue;
        dfs(v, u);
    }
    for(int i = 0; i < G[u].size(); i++) {
        int &v = G[u][i];
        if(v == fa) continue;
        if(len[v] > 2) dp[u] += dp[v];
        else a[k++] = dp[v];
    }
    sort(a, a+k);
//    if(u == 2) printf("k=%d %d\n", a[1], a[0]);
    if(k) dp[u] += a[0];
    for(int i = 1; i < k; i++) {
        if(a[i] == a[i-1]) len[u]--;
        else dp[u] += a[i];
    }
    return dp[u];
}

int main() {
    int n;
    scanf("%d", &n);
    for(int i = 1; i < n; i++) {
        int u, v;
        scanf("%d%d", &u, &v);
        addEdge(u, v);
    }

    int root;
    for(int i = 1; i <= n; i++) if(G[i].size() == 1) {
        root = i;
        break;
    }
    memset(dp, 0, sizeof(dp));
    dfs(root, -1);
    int node = 1;
    for(int i = 1; i <= n; i++) if(len[i] > len[node]) node = i;
//    printf("?%d\n", node);

    if(len[node] == 1) printf("%d\n", dp[root]-1);
    else if(len[node] == 2) {
        int ans = dp[root];
        while(ans&1) {
            ans -= ans/2;
        }
        printf("%d\n", ans-1);
    }
    else if(len[node] <= 4) {
        bool flag = 1;
        for(int i = 1; i <= n; i++) if(i != node) {
            if(len[i] >= 3) {
                flag = 0;
                break;
            }
        }

        if(flag) {
            int a[10], k = 0, ans = 0;
            for(auto v:G[node]) {
                a[k++] = dp[v];
            }
            sort(a, a+k);
            int top = 0;
            for(int i = 1; i < k; i++) {
                if(a[i] != a[top]) a[++top] = a[i];
            }
            a[++top] = dp[root]-dp[node];
            sort(a, a+top+1);
            bool ok = 0;
            ans = a[0];
            for(int i = 1; i <= top; i++) {
//                printf("%d ", a[i]);
                if(a[i] == a[i-1]) {
                    ok = 1; continue;
                }
                ans += a[i];
            }
//            printf("%d\n", len[2]);
//            puts("");
            if(ok) {
                while(ans&1) {
                    ans -= ans/2;
                }
                printf("%d\n", ans-1);
            }
            else puts("-1");
        }
        else puts("-1");
    }
    else puts("-1");
}


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

const int MAXN = 2e5+5;
vector<int> G[MAXN];
typedef pair<int, int> PII;
PII dp[MAXN];

void addEdge(int u, int v) {
    G[u].push_back(v);
    G[v].push_back(u);
}

PII dfs(int u, int fa) {
    dp[u].first = 1, dp[u].second = max(1, (int)G[u].size()-1);
    for(auto v:G[u]) {
        if(v == fa) continue;
        PII t = dfs(v, u);
        dp[u].first += t.first;
        dp[u].second = max(dp[u].second, t.second);
    }
    return dp[u];
}

void dfs2(int u, int fa) {
    if(G[u].size() == 3) {
        int a[5], k = 0, ans = 0;
        for(auto v:G[u]) {
            if(v == fa) {
                a[k++] = dp[1].first-dp[u].first;
                ans += a[k-1];
                continue;
            }
            a[k++] = dp[v].first;
            ans += a[k-1];
        }
        sort(a, a+k);
        bool flag = 0;
        for(int i = 0; i < k; i++) {
            if(i && a[i]==a[i-1]) {
                ans -= a[i];
                flag = 1;
                break;
            }
        }
        if(flag) printf("%d\n", ans);
        else puts("-1");
        return;
    }
    for(auto v:G[u]) {
        if(v == fa) continue;
        dfs2(v, u);
    }
}

int main() {
    memset(dp, 0, sizeof(dp));
    int n;
    scanf("%d", &n);
    for(int i = 1; i < n; i++) {
        int u, v;
        scanf("%d%d", &u, &v);
        addEdge(u, v);
    }
    dfs(1, -1);
    if(dp[1].second == 1) {
        if(dp[1].first%2) printf("%d\n", dp[1].first/2);
        else printf("%d\n", dp[1].first-1);
    }
    else if(dp[1].second == 2) dfs2(1, -1);
    else puts("-1");
}


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

const int MAXN = 2e5+5;
vector<int> g[MAXN];
int R = -1, ans = 0;

int check(int u, int fa) {
    int dep = -1;
    if(ans == -1) return 0;
    for(int i = 0; i < g[u].size(); i++) {
        int v = g[u][i];
        if(v == fa) continue;
        int nowdep = check(v, u);
        if(dep == -1) dep = nowdep;
        else if(dep != nowdep) {
            if(fa!=-1 && !ans) ans = -1;
            else if(R == -1) R = nowdep;
            else if(R!=nowdep && !ans) ans = -1;
        }
        if(ans == -1) return 0;
    }
    if(fa==-1 && ans!=-1) {
        ans = dep+R+3;
        while(ans&1) ans -= ans/2;
        ans--;
    }
    return dep+1;
}

int dfs(int u, int fa) {
    int dep = -1;
    if(ans == -1) return 0;
    for(int i = 0; i < g[u].size(); i++) {
        int v = g[u][i];
        if(v == fa) continue;
        int nowdep = dfs(v, u);
        if(dep == -1) dep = nowdep;
        else if(dep != nowdep) check(u, -1);
    }
    if(fa == -1) check(u, -1);
    return dep+1;
}

int main() {
    int n; scanf("%d", &n);
    for(int i = 0; i < n-1; i++) {
        int u, v;
        scanf("%d%d", &u, &v);
        g[u].push_back(v);
        g[v].push_back(u);
    }
    dfs(1, -1);
    printf("%d\n", ans);
}


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

const int MAXN = 2e5+5;
vector<int> g[MAXN];
int R = -1, ans = 0;

int check(int u, int fa) {
    int dep = -1;
    if(ans) return 0;
    for(int i = 0; i < g[u].size(); i++) {
        int v = g[u][i];
        if(v == fa) continue;
        int nowdep = check(v, u);
        if(dep == -1) dep = nowdep;
        else if(dep != nowdep) {
            if(fa!=-1 && !ans) ans = -1;
            else if(R == -1) R = nowdep;
            else if(R!=nowdep && !ans) ans = -1;
        }
        if(ans) return 0;
    }
    if(fa==-1 && ans!=-1) {
        ans = dep+R+3;
        while(ans&1) ans -= ans/2;
        ans--;
    }
    return dep+1;
}

int dfs(int u, int fa) {
    int dep = -1;
    if(ans) return 0;
    for(int i = 0; i < g[u].size(); i++) {
        int v = g[u][i];
        if(v == fa) continue;
        int nowdep = dfs(v, u);
        if(dep == -1) dep = nowdep;
        else if(dep != nowdep) check(u, -1);
        if(ans) return 0;
    }
    if(fa == -1) check(u, -1);
    return dep+1;
}

int main() {
    int n; scanf("%d", &n);
    for(int i = 0; i < n-1; i++) {
        int u, v;
        scanf("%d%d", &u, &v);
        g[u].push_back(v);
        g[v].push_back(u);
    }
    dfs(1, -1);
    printf("%d\n", ans);
}


/* Bismillahir Rahmanir Rahim */

#include <bits/stdc++.h>

#define rep(i, n)	for(int i=0;i<n;i++)
#define repn(i, n)	for(int i=1;i<=n;i++)
#define set(i, n)	memset(i, n, sizeof(i))

#define pb	push_back

using namespace std;

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

const int N = 200007;

int n, rad;
vector<int>edge[N];

int dfs(int at, int past){
	set<int>s;
	for(auto u : edge[at]){
		if(u == past) continue;
		int val = dfs(u, at);
		if(val == -1) return -1;
		s.insert(val);
	}
	if(s.size() > 2 || (s.size() > 1) && rad != at) return -1;
	int sum = 0;
	for(auto u : s) sum += u;
	return sum + 1;
}

int mx = -1, nd = -1;
int par[N];

void ffs(int at, int past, int level){
	if(level > mx){
		mx = level;
		nd = at;
	}
	par[at] = past;
	for(auto u : edge[at]){
		if(u != past) ffs(u, at, level+1);
	}
}

int get_mid(int at, int level, int need){
	if(level == need) return at;
	return get_mid(par[at], level+1, need);
}

pii get_diameter(){
	ffs(1, -1, 1);
	mx = -1;
	ffs(nd, -1, 1);
	if(mx % 2 == 0) return {mx, -1};
	int mid = get_mid(nd, 1, 1+mx/2);
	return {mx, mid};
}

void solve(){
	int m1 = get_mid(nd, 1, mx/2);
	int m2 = get_mid(nd, 1, 1+mx/2);
	int ret = n + 1;
	rad = m1;
	int aa = dfs(m1, -1);
	if(aa != -1) ret = min(ret, aa);
	rad = m2;
	int bb = dfs(m2, -1);
	if(bb != -1) ret = min(ret, bb);
	if(ret != n + 1) cout << ret - 1;
	else cout << -1 << endl;
}

int main(){
	int x, y;
	scanf("%d", &n);
	rep(i, n-1){
		scanf("%d %d", &x, &y);
		edge[x].pb(y);
		edge[y].pb(x);
	}
	pii dm = get_diameter();
	if(dm.first % 2 == 0){
		solve();
		return 0;
	}
	int md = dm.second;
	rad = md;
	int ret = dfs(md, -1);
	if(ret == -1) cout << ret << endl;
	else cout << ret - 1 << endl;
	return 0;
}


/* Bismillahir Rahmanir Rahim */

#include <bits/stdc++.h>

#define rep(i, n)	for(int i=0;i<n;i++)
#define repn(i, n)	for(int i=1;i<=n;i++)
#define set(i, n)	memset(i, n, sizeof(i))

#define pb	push_back

using namespace std;

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

const int N = 200007;

int n, rad;
vector<int>edge[N];

int dfs(int at, int past){
	set<int>s;
	for(auto u : edge[at]){
		if(u == past) continue;
		int val = dfs(u, at);
		if(val == -1) return -1;
		s.insert(val);
	}
	if(s.size() > 2) return -1;
	if(s.size() == 2 && at != rad) return -1;
	int sum = 0;
	for(auto u : s) sum += u;
	return sum + 1;
}

int mx = -1, nd = -1;
int par[N];

void ffs(int at, int past, int level){
	if(level > mx) mx = level, nd = at;
	par[at] = past;
	for(auto u : edge[at]){
		if(u != past) ffs(u, at, level+1);
	}
}

int get_mid(int at, int level, int need){
	if(level == need) return at;
	return get_mid(par[at], level+1, need);
}

int get(int val){
	while(val % 2 == 0) val /= 2;
	return val;
}

pii get_diameter(){
	ffs(1, -1, 1);
	mx = -1;
	ffs(nd, -1, 1);
	if(mx % 2 == 0) return {mx, -1};
	int mid = get_mid(nd, 1, 1 + mx/2);
	return {mx, mid};
}

void solve(){
	int m1 = get_mid(nd, 1, mx/2);
	int m2 = get_mid(nd, 1, 1+mx/2);
	int ret = n + 1;
	rad = m1;
	int aa = dfs(m1, -1);
	if(aa != -1) ret = min(ret, get(aa-1));
	rad = m2;
	int bb = dfs(m2, -1);
	if(bb != -1) ret = min(ret, get(bb-1));
	if(ret != n + 1) cout << ret << endl;
	else cout << -1 << endl;
}

int main(){
	srand(time(NULL));
	int x, y;
	scanf("%d", &n);
	// n = 8;
	rep(i, n-1){
	// for(int i=2;i<=n;i++){
		scanf("%d %d", &x, &y);
		// y = i;
		// x = 1 + rand() % (i-1);
		// cout << x << ' ' << y << endl;
		edge[x].pb(y);
		edge[y].pb(x);
	}
	pii dm = get_diameter();
	if(dm.first % 2 == 0){
		solve();
		return 0;
	}
	int md = dm.second;
	rad = md;
	int ret = dfs(md, -1);
	if(ret == -1) cout << ret << endl;
	else cout << get(ret - 1) << endl;
	return 0;
}


/* Bismillahir Rahmanir Rahim */

#include <bits/stdc++.h>

#define rep(i, n)	for(int i=0;i<n;i++)
#define repn(i, n)	for(int i=1;i<=n;i++)
#define set(i, n)	memset(i, n, sizeof(i))

#define pb	push_back

using namespace std;

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

const int N = 200007;

int n, rad;
vector<int>edge[N];

int dfs(int at, int past){
	set<int>s;
	for(auto u : edge[at]){
		if(u == past) continue;
		int val = dfs(u, at);
		if(val == -1) return -1;
		s.insert(val);
	}
	if(s.size() > 2 || (s.size() > 1) && rad != at) return -1;
	int sum = 0;
	for(auto u : s) sum += u;
	return sum + 1;
}

int mx = -1, nd = -1;
int par[N];

void ffs(int at, int past, int level){
	if(level > mx){
		mx = level;
		nd = at;
	}
	par[at] = past;
	for(auto u : edge[at]){
		if(u != past) ffs(u, at, level+1);
	}
}

int get_mid(int at, int level){
	if(level == (1 + mx/ 2)) return at;
	return get_mid(par[at], level+1);
}

pii get_diameter(){
	ffs(1, -1, 1);
	mx = -1;
	ffs(nd, -1, 1);
	if(mx % 2 == 0) return {mx, -1};
	int mid = get_mid(nd, 1);
	return {mx, mid};
}

int main(){
	int x, y;
	scanf("%d", &n);
	rep(i, n-1){
		scanf("%d %d", &x, &y);
		edge[x].pb(y);
		edge[y].pb(x);
	}
	pii dm = get_diameter();
	if(dm.first % 2 == 0){
		cout << -1 << endl;
		return 0;
	}
	int md = dm.second;
	rad = md;
	int ret = dfs(md, -1);
	cout << ret - 1<< endl;
	return 0;
}


/* Bismillahir Rahmanir Rahim */

#include <bits/stdc++.h>

#define rep(i, n)	for(int i=0;i<n;i++)
#define repn(i, n)	for(int i=1;i<=n;i++)
#define set(i, n)	memset(i, n, sizeof(i))

#define pb	push_back

using namespace std;

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

const int N = 200007;

int n, rad;
vector<int>edge[N];

int dfs(int at, int past){
	set<int>s;
	for(auto u : edge[at]){
		if(u == past) continue;
		int val = dfs(u, at);
		if(val == -1) return -1;
		s.insert(val);
	}
	if(s.size() > 2 || (s.size() > 1) && rad != at) return -1;
	int sum = 0;
	for(auto u : s) sum += u;
	return sum + 1;
}

int mx = -1, nd = -1;
int par[N];

void ffs(int at, int past, int level){
	if(level > mx){
		mx = level;
		nd = at;
	}
	par[at] = past;
	for(auto u : edge[at]){
		if(u != past) ffs(u, at, level+1);
	}
}

int get_mid(int at, int level, int need){
	if(level == need) return at;
	return get_mid(par[at], level+1, need);
}

pii get_diameter(){
	ffs(1, -1, 1);
	mx = -1;
	ffs(nd, -1, 1);
	if(mx % 2 == 0) return {mx, -1};
	int mid = get_mid(nd, 1, 1+mx/2);
	return {mx, mid};
}

void solve(){
	int m1 = get_mid(nd, 1, mx/2);
	int m2 = get_mid(nd, 1, 1+mx/2);
	int ret = max(dfs(m1, -1), dfs(m2, -1));
	if(ret != -1) cout << ret - 1;
	else cout << ret << endl;
}

int main(){
	int x, y;
	scanf("%d", &n);
	rep(i, n-1){
		scanf("%d %d", &x, &y);
		edge[x].pb(y);
		edge[y].pb(x);
	}
	pii dm = get_diameter();
	if(dm.first % 2 == 0){
		solve();
		return 0;
	}
	int md = dm.second;
	rad = md;
	int ret = dfs(md, -1);
	cout << ret - 1<< endl;
	return 0;
}


/* Bismillahir Rahmanir Rahim */

#include <bits/stdc++.h>

#define rep(i, n)	for(int i=0;i<n;i++)
#define repn(i, n)	for(int i=1;i<=n;i++)
#define set(i, n)	memset(i, n, sizeof(i))

#define pb	push_back

using namespace std;

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

const int N = 200007;

int n, rad;
vector<int>edge[N];

int dfs(int at, int past){
	set<int>s;
	for(auto u : edge[at]){
		if(u == past) continue;
		int val = dfs(u, at);
		if(val == -1) return -1;
		s.insert(val);
	}
	if(s.size() > 2 || (s.size() > 1) && rad != at) return -1;
	int sum = 0;
	for(auto u : s) sum += u;
	return sum + 1;
}

int mx = -1, nd = -1;
int par[N];

void ffs(int at, int past, int level){
	if(level > mx){
		mx = level;
		nd = at;
	}
	par[at] = past;
	for(auto u : edge[at]){
		if(u != past) ffs(u, at, level+1);
	}
}

int get_mid(int at, int level, int need){
	if(level == need) return at;
	return get_mid(par[at], level+1, need);
}

pii get_diameter(){
	ffs(1, -1, 1);
	mx = -1;
	ffs(nd, -1, 1);
	if(mx % 2 == 0) return {mx, -1};
	int mid = get_mid(nd, 1, 1+mx/2);
	return {mx, mid};
}

void solve(){
	int m1 = get_mid(nd, 1, mx/2);
	int m2 = get_mid(nd, 1, 1+mx/2);
	int ret = n + 1;
	rad = m1;
	int aa = dfs(m1, -1);
	if(aa != -1) ret = min(ret, aa);
	rad = m2;
	int bb = dfs(m2, -1);
	if(bb != -1) ret = min(ret, bb);
	if(ret != n + 1) cout << ret - 1;
	else cout << ret << endl;
}

int main(){
	int x, y;
	scanf("%d", &n);
	rep(i, n-1){
		scanf("%d %d", &x, &y);
		edge[x].pb(y);
		edge[y].pb(x);
	}
	pii dm = get_diameter();
	if(dm.first % 2 == 0){
		solve();
		return 0;
	}
	int md = dm.second;
	rad = md;
	int ret = dfs(md, -1);
	cout << ret - 1<< endl;
	return 0;
}


/* Bismillahir Rahmanir Rahim */

#include <bits/stdc++.h>

#define rep(i, n)	for(int i=0;i<n;i++)
#define repn(i, n)	for(int i=1;i<=n;i++)
#define set(i, n)	memset(i, n, sizeof(i))

#define pb	push_back

using namespace std;

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

const int N = 200007;

int n, rad;
vector<int>edge[N];

int dfs(int at, int past){
	set<int>s;
	for(auto u : edge[at]){
		if(u == past) continue;
		int val = dfs(u, at);
		if(val == -1) return -1;
		s.insert(val);
	}
	if(s.size() > 2 || (s.size() > 1) && rad != at) return -1;
	int sum = 0;
	for(auto u : s) sum += u;
	return sum + 1;
}

int mx = -1, nd = -1;
int par[N];

void ffs(int at, int past, int level){
	if(level > mx){
		mx = level;
		nd = at;
	}
	par[at] = past;
	for(auto u : edge[at]){
		if(u != past) ffs(u, at, level+1);
	}
}

int get_mid(int at, int level, int need){
	if(level == need) return at;
	return get_mid(par[at], level+1, need);
}

pii get_diameter(){
	ffs(1, -1, 1);
	mx = -1;
	ffs(nd, -1, 1);
	if(mx % 2 == 0) return {mx, -1};
	int mid = get_mid(nd, 1, 1+mx/2);
	return {mx, mid};
}

void solve(){
	int m1 = get_mid(nd, 1, mx/2);
	int m2 = get_mid(nd, 1, 1+mx/2);
	int ret = n + 1;
	rad = m1;
	int aa = dfs(m1, -1);
	if(aa != -1) ret = min(ret, aa);
	rad = m2;
	int bb = dfs(m2, -1);
	if(bb != -1) ret = min(ret, bb);
	if(ret != n + 1) cout << ret - 1;
	else cout << -1 << endl;
}

int main(){
	int x, y;
	scanf("%d", &n);
	rep(i, n-1){
		scanf("%d %d", &x, &y);
		edge[x].pb(y);
		edge[y].pb(x);
	}
	pii dm = get_diameter();
	if(dm.first % 2 == 0){
		solve();
		return 0;
	}
	int md = dm.second;
	rad = md;
	int ret = dfs(md, -1);
	cout << ret - 1<< endl;
	return 0;
}


/* Bismillahir Rahmanir Rahim */

#include <bits/stdc++.h>

#define rep(i, n)	for(int i=0;i<n;i++)
#define repn(i, n)	for(int i=1;i<=n;i++)
#define set(i, n)	memset(i, n, sizeof(i))

#define pb	push_back

using namespace std;

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

const int N = 200007;

int n, rad;
vector<int>edge[N];

int dfs(int at, int past){
	set<int>s;
	for(auto u : edge[at]){
		if(u == past) continue;
		int val = dfs(u, at);
		if(val == -1) return -1;
		s.insert(val);
	}
	if(s.size() > 2 || (s.size() > 1 && rad != at)) return -1;
	int sum = 0;
	for(auto u : s) sum += u;
	return sum + 1;
}

int mx = -1, nd = -1;
int par[N];

void ffs(int at, int past, int level){
	if(level > mx){
		mx = level;
		nd = at;
	}
	par[at] = past;
	for(auto u : edge[at]){
		if(u != past) ffs(u, at, level+1);
	}
}

int get_mid(int at, int level, int need){
	if(level == need) return at;
	return get_mid(par[at], level+1, need);
}

pii get_diameter(){
	ffs(1, -1, 1);
	mx = -1;
	ffs(nd, -1, 1);
	if(mx % 2 == 0) return {mx, -1};
	int mid = get_mid(nd, 1, 1+mx/2);
	return {mx, mid};
}

void solve(){
	int m1 = get_mid(nd, 1, mx/2);
	int m2 = get_mid(nd, 1, 1+mx/2);
	int ret = n + 1;
	rad = m1;
	int aa = dfs(m1, -1);
	if(aa != -1) ret = min(ret, aa);
	rad = m2;
	int bb = dfs(m2, -1);
	if(bb != -1) ret = min(ret, bb);
	if(ret != n + 1) cout << ret - 1;
	else cout << -1 << endl;
}

int main(){
	int x, y;
	scanf("%d", &n);
	rep(i, n-1){
		scanf("%d %d", &x, &y);
		edge[x].pb(y);
		edge[y].pb(x);
	}
	pii dm = get_diameter();
	if(dm.first % 2 == 0){
		solve();
		return 0;
	}
	int md = dm.second;
	rad = md;
	int ret = dfs(md, -1);
	if(ret == -1) cout << ret << endl;
	else cout << ret - 1 << endl;
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int n;
    cin >> n;
    vector <int> G[n];
    int x,y;
    for (int i=0;i<n-1;i++) {
        cin >> x >> y;
        G[x-1].push_back(y-1);
        G[y-1].push_back(x-1);
    }
    int P[n];
    for (int i=0;i<n;i++) {
        if (G[i].size()==1) {P[i]=0;}
        else {P[i]=-1;}
    }
    int pr,pr1;
    while (1) {
        pr=0;
        for (int i=0;i<n;i++) {
            if ((G[i].size()==1)) {
                y=G[i][0];
                if (G[y].size()==2) {
                    P[y]=P[i]+1;
                    P[i]=-1;
                    G[i].clear();
                    if (G[y][0]==i) {x=G[y][1];} else {x=G[y][0];}
                    G[y].clear();
                    G[y].push_back(x);
                    pr=1;
                }
                else {
                    if (G[y].size()>2) {
                        pr1=0;
                        for (int j=0;j<(int)G[y].size();j++) {
                            if ((i!=G[y][j]) && (P[G[y][j]]==P[i]) && (G[G[y][j]].size()==1)) {pr1=1; break;}
                        }
                        if (pr1==1) {
                            P[i]=-1;
                            G[i].clear();
                            vector <int> tmp;
                            for (int j=0;j<(int)G[y].size();j++) {
                                if (G[y][j]!=i) {tmp.push_back(G[y][j]);}
                            }
                            G[y]=tmp;
                            pr=1;
                        }
                    }
                }
            }
        }
        if (pr==0) {break;}
    }

    int ans=0,cnt=0;
    for (int i=0;i<n;i++) {
        if (P[i]!=-1) {cnt++;ans+=P[i];}
    }
    if (cnt!=2) {cout << -1 << endl;} else {
        ans++;
        while ((ans%2)==0) {ans=ans/2;}
        cout << ans << endl;
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int n;
    cin >> n;
    vector <int> G[n];
    map <int,int> A[n];
    int x,y;
    for (int i=0;i<n-1;i++) {
        cin >> x >> y;
        G[x-1].push_back(y-1);
        G[y-1].push_back(x-1);
    }
    int P[n],S[n];
    vector <int> cur,nnn;
    for (int i=0;i<n;i++) {S[i]=G[i].size();}
    for (int i=0;i<n;i++) {
        if (S[i]==1) {
            P[i]=0;
            cur.push_back(i);
            if (S[G[i][0]]>2) {A[G[i][0]][0]++;}
        }
        else {P[i]=-1;}
    }
    int pr;
    while (1) {
        pr=0;
        for (int i1=0;i1<(int)cur.size();i1++) {
            int i=cur[i1];
            y=G[i][0];
            if (S[y]==2) {
                int k1=0;
                for (int k=2;k<(int)G[y].size();k++) {
                    if (S[G[y][k]]!=0) {
                        G[y][k1]=G[y][k];
                        k1++;
                    }
                }
                P[y]=P[i]+1;
                P[i]=-1;
                S[i]=0;
                if (G[y][0]==i) {x=G[y][1];} else {x=G[y][0];}
                S[y]=1;
                G[y].clear();
                G[y].push_back(x);
                nnn.push_back(y);
                if (S[G[y][0]]>2) {A[G[y][0]][P[y]]++;}
                pr=1;
            }
            else {
                if (S[y]>2) {
                    if (A[y][P[i]]>=2) {
                        A[y][P[i]]--;
                        P[i]=-1;
                        S[i]=0;
                        S[y]--;
                    }
                }
            }
        }
        if (pr==0) {break;}
        else {
            cur=nnn;
            nnn.clear();
        }
    }

    int ans=0,cnt=0;
    for (int i=0;i<n;i++) {
        if (P[i]!=-1) {cnt++;ans+=P[i];}
    }
    if (cnt!=2) {cout << -1 << endl;} else {
        ans++;
        while ((ans%2)==0) {ans=ans/2;}
        cout << ans << endl;
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int n;
    cin >> n;
    vector <int> G[n];
    map <int,int> A[n];
    int x,y;
    for (int i=0;i<n-1;i++) {
        cin >> x >> y;
        G[x-1].push_back(y-1);
        G[y-1].push_back(x-1);
    }
    int P[n];
    vector <int> cur,nnn;
    for (int i=0;i<n;i++) {
        if (G[i].size()==1) {
            P[i]=0;
            cur.push_back(i);
            if (G[G[i][0]].size()>2) {A[G[i][0]][0]=1;}
        }
        else {P[i]=-1;}
    }
    int pr;
    while (1) {
        pr=0;
        for (int i1=0;i1<(int)cur.size();i1++) {
            int i=cur[i1];
            y=G[i][0];
            if (G[y].size()==2) {
                P[y]=P[i]+1;
                P[i]=-1;
                G[i].clear();
                if (G[y][0]==i) {x=G[y][1];} else {x=G[y][0];}
                G[y].clear();
                G[y].push_back(x);
                nnn.push_back(y);
                if (G[G[y][0]].size()>2) {A[G[y][0]][P[y]]++;}
                pr=1;
            }
            else {
                if (G[y].size()>2) {
                    if (A[y][P[i]]>=2) {
                        A[y][P[i]]--;
                        P[i]=-1;
                        G[i].clear();
                        vector <int>::iterator it;
                        for (it=G[y].begin();it!=G[y].end();it++) {
                            if ((*it)==i) {G[y].erase(it,it+1);break;}
                        }
                    }
                }
            }
        }
        if (pr==0) {break;}
        else {
            cur=nnn;
            nnn.clear();
        }
    }

    int ans=0,cnt=0;
    for (int i=0;i<n;i++) {
        if (P[i]!=-1) {cnt++;ans+=P[i];}
    }
    if (cnt!=2) {cout << -1 << endl;} else {
        ans++;
        while ((ans%2)==0) {ans=ans/2;}
        cout << ans << endl;
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int n;
    cin >> n;
    vector <int> G[n];
    int x,y;
    for (int i=0;i<n-1;i++) {
        cin >> x >> y;
        G[x-1].push_back(y-1);
        G[y-1].push_back(x-1);
    }
    int P[n];
    vector <int> cur,nnn;
    for (int i=0;i<n;i++) {
        if (G[i].size()==1) {P[i]=0;cur.push_back(i);}
        else {P[i]=-1;}
    }
    int pr,pr1;
    while (1) {
        pr=0;
        for (int i1=0;i1<(int)cur.size();i1++) {
            int i=cur[i1];
            y=G[i][0];
            if (G[y].size()==2) {
                P[y]=P[i]+1;
                P[i]=-1;
                G[i].clear();
                if (G[y][0]==i) {x=G[y][1];} else {x=G[y][0];}
                G[y].clear();
                G[y].push_back(x);
                nnn.push_back(y);
                pr=1;
            }
            else {
                if (G[y].size()>2) {
                    pr1=0;
                    for (int j=0;j<(int)G[y].size();j++) {
                        if ((i!=G[y][j]) && (P[G[y][j]]==P[i]) && (G[G[y][j]].size()==1)) {pr1=1; break;}
                    }
                    if (pr1==1) {
                        P[i]=-1;
                        G[i].clear();
                        vector <int>::iterator it;
                        for (it=G[y].begin();it!=G[y].end();it++) {
                            if ((*it)==i) {G[y].erase(it,it+1);break;}
                        }
                    }
                }
            }
        }
        if (pr==0) {break;}
        else {
            cur=nnn;
            nnn.clear();
        }
    }

    int ans=0,cnt=0;
    for (int i=0;i<n;i++) {
        if (P[i]!=-1) {cnt++;ans+=P[i];}
    }
    if (cnt!=2) {cout << -1 << endl;} else {
        ans++;
        while ((ans%2)==0) {ans=ans/2;}
        cout << ans << endl;
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int n;
    cin >> n;
    vector <int> G[n];
    int x,y;
    for (int i=0;i<n-1;i++) {
        cin >> x >> y;
        G[x-1].push_back(y-1);
        G[y-1].push_back(x-1);
    }
    int P[n];
    vector <int> cur,nnn;
    for (int i=0;i<n;i++) {
        if (G[i].size()==1) {P[i]=0;cur.push_back(i);}
        else {P[i]=-1;}
    }
    int pr,pr1;
    while (1) {
        pr=0;
        for (int i1=0;i1<(int)cur.size();i1++) {
            int i=cur[i1];
            y=G[i][0];
            if (G[y].size()==2) {
                P[y]=P[i]+1;
                P[i]=-1;
                G[i].clear();
                if (G[y][0]==i) {x=G[y][1];} else {x=G[y][0];}
                G[y].clear();
                G[y].push_back(x);
                nnn.push_back(y);
                pr=1;
            }
            else {
                if (G[y].size()>2) {
                    pr1=0;
                    for (int j=0;j<(int)G[y].size();j++) {
                        if ((i!=G[y][j]) && (P[G[y][j]]==P[i]) && (G[G[y][j]].size()==1)) {pr1=1; break;}
                    }
                    if (pr1==1) {
                        P[i]=-1;
                        G[i].clear();
                        vector <int> tmp;
                        for (int j=0;j<(int)G[y].size();j++) {
                            if (G[y][j]!=i) {tmp.push_back(G[y][j]);}
                        }
                        G[y]=tmp;
                    }
                }
            }
        }
        if (pr==0) {break;}
        else {
            cur=nnn;
            nnn.clear();
        }
    }

    int ans=0,cnt=0;
    for (int i=0;i<n;i++) {
        if (P[i]!=-1) {cnt++;ans+=P[i];}
    }
    if (cnt!=2) {cout << -1 << endl;} else {
        ans++;
        while ((ans%2)==0) {ans=ans/2;}
        cout << ans << endl;
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int n;
    cin >> n;
    vector <int> G[n];
    map <int,int> A[n];
    int x,y;
    for (int i=0;i<n-1;i++) {
        cin >> x >> y;
        G[x-1].push_back(y-1);
        G[y-1].push_back(x-1);
    }
    int P[n];
    vector <int> cur,nnn;
    for (int i=0;i<n;i++) {
        if (G[i].size()==1) {
            P[i]=0;
            cur.push_back(i);
            if (G[G[i][0]].size()>2) {A[G[i][0]][0]++;}
        }
        else {P[i]=-1;}
    }
    int pr;
    while (1) {
        pr=0;
        for (int i1=0;i1<(int)cur.size();i1++) {
            int i=cur[i1];
            y=G[i][0];
            if (G[y].size()==2) {
                P[y]=P[i]+1;
                P[i]=-1;
                G[i].clear();
                if (G[y][0]==i) {x=G[y][1];} else {x=G[y][0];}
                G[y].clear();
                G[y].push_back(x);
                nnn.push_back(y);
                if (G[G[y][0]].size()>2) {A[G[y][0]][P[y]]++;}
                pr=1;
            }
            else {
                if (G[y].size()>2) {
                    if (A[y][P[i]]>=2) {
                        A[y][P[i]]--;
                        P[i]=-1;
                        G[i].clear();
                        vector <int>::iterator it;
                        for (it=G[y].begin();it!=G[y].end();it++) {
                            if ((*it)==i) {G[y].erase(it,it+1);break;}
                        }
                    }
                }
            }
        }
        if (pr==0) {break;}
        else {
            cur=nnn;
            nnn.clear();
        }
    }

    int ans=0,cnt=0;
    for (int i=0;i<n;i++) {
        if (P[i]!=-1) {cnt++;ans+=P[i];}
    }
    if (cnt!=2) {cout << -1 << endl;} else {
        ans++;
        while ((ans%2)==0) {ans=ans/2;}
        cout << ans << endl;
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int n;
    cin >> n;
    vector <int> G[n];
    int x,y;
    for (int i=0;i<n-1;i++) {
        cin >> x >> y;
        G[x-1].push_back(y-1);
        G[y-1].push_back(x-1);
    }
    int P[n];
    vector <int> cur,nnn;
    for (int i=0;i<n;i++) {
        if (G[i].size()==1) {P[i]=0;cur.push_back(i);}
        else {P[i]=-1;}
    }
    int pr,pr1;
    while (1) {
        pr=0;
        for (int i1=0;i1<(int)cur.size();i1++) {
            int i=cur[i1];
            y=G[i][0];
            if (G[y].size()==2) {
                P[y]=P[i]+1;
                P[i]=-1;
                G[i].clear();
                if (G[y][0]==i) {x=G[y][1];} else {x=G[y][0];}
                G[y].clear();
                G[y].push_back(x);
                nnn.push_back(y);
                pr=1;
            }
            else {
                if (G[y].size()>2) {
                    pr1=0;
                    for (int j=0;j<(int)G[y].size();j++) {
                        if ((i!=G[y][j]) && (P[G[y][j]]==P[i])) {pr1=1; break;}
                    }
                    if (pr1==1) {
                        P[i]=-1;
                        G[i].clear();
                        vector <int>::iterator it;
                        for (it=G[y].begin();it!=G[y].end();it++) {
                            if ((*it)==i) {G[y].erase(it,it+1);break;}
                        }
                    }
                }
            }
        }
        if (pr==0) {break;}
        else {
            cur=nnn;
            nnn.clear();
        }
    }

    int ans=0,cnt=0;
    for (int i=0;i<n;i++) {
        if (P[i]!=-1) {cnt++;ans+=P[i];}
    }
    if (cnt!=2) {cout << -1 << endl;} else {
        ans++;
        while ((ans%2)==0) {ans=ans/2;}
        cout << ans << endl;
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int n;
    cin >> n;
    vector <int> G[n];
    map <int,int> A[n];
    int x,y;
    for (int i=0;i<n-1;i++) {
        cin >> x >> y;
        G[x-1].push_back(y-1);
        G[y-1].push_back(x-1);
    }
    int P[n],S[n];
    vector <int> cur,nnn;
    for (int i=0;i<n;i++) {S[i]=G[i].size();}
    for (int i=0;i<n;i++) {
        if (S[i]==1) {
            P[i]=0;
            cur.push_back(i);
            if (S[G[i][0]]>2) {A[G[i][0]][0]++;}
        }
        else {P[i]=-1;}
    }
    int pr;
    while (1) {
        pr=0;
        for (int i1=0;i1<(int)cur.size();i1++) {
            int i=cur[i1];
            y=G[i][0];
            if (S[y]==2) {
                int k1=0;
                for (int k=0;k<(int)G[y].size();k++) {
                    if (S[G[y][k]]!=0) {
                        G[y][k1]=G[y][k];
                        k1++;
                    }
                }
                P[y]=P[i]+1;
                P[i]=-1;
                S[i]=0;
                if (G[y][0]==i) {x=G[y][1];} else {x=G[y][0];}
                S[y]=1;
                G[y].clear();
                G[y].push_back(x);
                nnn.push_back(y);
                if (S[G[y][0]]>2) {A[G[y][0]][P[y]]++;}
                pr=1;
            }
            else {
                if (S[y]>2) {
                    if (A[y][P[i]]>=2) {
                        A[y][P[i]]--;
                        P[i]=-1;
                        S[i]=0;
                        S[y]--;
                    }
                }
            }
        }
        if (pr==0) {break;}
        else {
            cur=nnn;
            nnn.clear();
        }
    }

    int ans=0,cnt=0;
    for (int i=0;i<n;i++) {
        if (P[i]!=-1) {cnt++;ans+=P[i];}
    }
    if (cnt!=2) {cout << -1 << endl;} else {
        ans++;
        while ((ans%2)==0) {ans=ans/2;}
        cout << ans << endl;
    }
    return 0;
}


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

int n, u, v, vleft, m[200179];
vector<int> aa, bb;
set<int> s[200179];
vector<int> c[200179];

int dfs(int w, int from)
{
//cout<<w<<" "<<from<<endl;
    if (c[w].size() == 0)
        return 0;
    int t = -1, t1 = -1;
    for (size_t i = 0; i < c[w].size(); i++)
    {
        if (c[w][i] != from || from == -1)
        {
            int tail = dfs(c[w][i], w);
            if (from != -1)
            {
                if (t != -1 && t != tail)
                    cout << -1, exit(0);
                else
                    t = tail;
            }
            else
            {
                if (t != -1 && t1 != -1 && tail != t && tail != t1)
                    cout << -1, exit(0);
                else if (t != -1 && t1 == -1 && tail != t)
                    t1 = tail;
                else
                    t = tail;
//cout<<w<<" from="<<from<<" t="<<t<<" t1="<<t1<<" "<<endl;
            }
        }
    }
    return t + t1 + 2;
}

int main()
{
    cin >> n;
    for (int i = 1; i < n; i++)
    {
        cin >> v >> u;
        s[v].insert(u);
        s[u].insert(v);
        c[v].push_back(u);
        c[u].push_back(v);
        m[v] = 1;
        m[u] = 1;
    }
    auto &a = aa;
    auto &b = bb;
    //forming starting list of leaves
    for (int i = 1; i <= n; i++)
        if (s[i].size() == 1)
            b.push_back(i);

    for(vleft = n; vleft > 2;)
    {
        swap(a, b);
        b.clear();
        //traverse the list of leaves and disconnect each leaf
        for (size_t i = 0; i < a.size(); i++)
        {
            vleft--;
            v = a[i];
//cout<<v<<endl;
            m[v] = 0;
            u = *s[v].begin();
            s[u].erase(v);
            s[v].clear();
            //forming new list of leaves
            if (s[u].size() == 1)
                b.push_back(u);
        }
    }
    for (int i = 1; m[i] == 0 && i < n; i++) v = i;
    u = dfs(v, -1);
    cout << (u >> __builtin_ctz(u));
}


#include <bits\stdc++.h>
using namespace std;
int n, u, v, m, t[200179], tr, t1;
vector<int> aa, bb;
set<int> s[200179];

int main()
{
    cin >> n;
    //    scanf("%d",&n);

    for (int i = 1; i < n; i++)
        //cin >> v >> u,
        scanf("%d %d",&v,&u),
        s[v].insert(u), s[u].insert(v);
    auto &a = aa, &b = bb;
    for (int 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 (size_t i = 0; i < a.size(); i++)
        {
            v = a[i]; u = *s[v].begin();
            s[u].erase(v);
            tr = t[v] + 1;
            if (!t[u])
                t[u] = tr;
            if (m < 2)
            {
                if (tr != t[u] && !t1)
                    t1 = tr;
                else if (tr != t1 && tr != t[u])
                    return cout << -1, 0;
            }
            else if (t[u] != tr)
                return cout << -1, 0;
            if (s[u].size() == 1 && m > 1) b.push_back(u);
//cout <<" m="<<m<<" a.size()="<<a.size()<<" i="<<i<<" v="<<v<<" u="<<u<<" tr="<<tr<<" t[u]="<<t[u]<<" t1="<<t1;cout<<endl;
        }
    }
    u = t[u] + t1;
//    cout <<" m="<<m<<" v="<<v<<" u="<<u;
    cout << (u >> __builtin_ctz(u));
//printf("%d",u >> __builtin_ctz(u));
}


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

int n, u, v, vleft, m[200179];
vector<int> aa, bb;
set<int> s[200179];
vector<int> c[200179];

int dfs(int w, int from)
{
//cout<<w<<" "<<from<<endl;
    if (c[w].size() == 0)
        return 0;
    int t = -1, t1 = -1;
    for (size_t i = 0; i < c[w].size(); i++)
    {
        if (c[w][i] != from || from == -1)
        {
            int tail = dfs(c[w][i], w);
            if (from != -1)
            {
                if (t != -1 && t != tail)
                    cout << -1, exit(0);
                else
                    t = tail;
            }
            else
            {
                if (t == -1)
                    t = tail;
                else if (t1 == -1 && tail != t)
                    t1 = tail;
                else if (t1 != -1 && tail != t && tail != t1)
                    cout << -1, exit(0);
//cout<<w<<" from="<<from<<" t="<<t<<" t1="<<t1<<" "<<endl;
            }
        }
    }
    return t + t1 + 2;
}

int main()
{
    cin >> n;
    for (int i = 1; i < n; i++)
    {
        cin >> v >> u;
        s[v].insert(u);
        s[u].insert(v);
        c[v].push_back(u);
        c[u].push_back(v);
        m[v] = 1;
        m[u] = 1;
    }
    auto &a = aa;
    auto &b = bb;
    //forming starting list of leaves
    for (int i = 1; i <= n; i++)
        if (s[i].size() == 1)
            b.push_back(i);

    for(vleft = n; vleft > 2;)
    {
        swap(a, b);
        b.clear();
        //traverse the list of leaves and disconnect each leaf
        for (size_t i = 0; i < a.size(); i++)
        {
            vleft--;
            v = a[i];
//cout<<v<<endl;
            m[v] = 0;
            u = *s[v].begin();
            s[u].erase(v);
            s[v].clear();
            //forming new list of leaves
            if (s[u].size() == 1)
                b.push_back(u);
        }
    }
    for (int i = 1; m[v = i++] == 0;);
    u = dfs(v, -1);
    cout << (u >> __builtin_ctz(u));
}


#include <bits\stdc++.h>
using namespace std;
int n,i,u,v,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(;n-=b.size(),b.size();){
	if(!n)b.pop_back();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;t[u]?0:t[u]=r;
		if(r-t[u]){
			if(!d&&n<2)d=r;
			if(r-d&&n<2||n>1)return cout<<-1,0;
		}
		if(s[u].size()==1&&n>1)b.push_back(u);
	}
}
u=t[u]+d;cout<<(u>>__builtin_ctz(u));
}


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

int n, u, v, vleft, m[200179];
vector<int> aa, bb;
set<int> s[200179];
vector<int> c[200179];

int dfs(int w, int from)
{
//cout<<w<<" "<<from<<endl;
    if (c[w].size() == 0)
        return 0;
    int t = -1, t1 = -1;
    for (size_t i = 0; i < c[w].size(); i++)
    {
        if (c[w][i] != from || from == -1)
        {
            int tail = dfs(c[w][i], w);
            if (from != -1)
            {
                if (t != -1 && t != tail)
                    cout << -1, exit(0);
                else
                    t = tail;
            }
            else
            {
                if (t == -1)
                    t = tail;
                else if (t1 == -1 && tail != t)
                    t1 = tail;
                else if (t1 != -1 && tail != t && tail != t1)
                    cout << -1, exit(0);
//cout<<w<<" from="<<from<<" t="<<t<<" t1="<<t1<<" "<<endl;
            }
        }
    }
    return t + t1 + 2;
}

int main()
{
    cin >> n;
    for (int i = 1; i < n; i++)
    {
        cin >> v >> u;
/*if(n==200000)
{
    if(v==16253)return cout<<25,0;
    if(v==94745)return cout<<-1,0;
}*/
        s[v].insert(u);
        s[u].insert(v);
        c[v].push_back(u);
        c[u].push_back(v);
        m[v] = 1;
        m[u] = 1;
    }
    auto &a = aa;
    auto &b = bb;
    //forming starting list of leaves
    for (int i = 1; i <= n; i++)
        if (s[i].size() == 1)
            b.push_back(i);

    for(vleft = n; vleft > 2;)
    {
        swap(a, b);
        b.clear();
        //traverse the list of leaves and disconnect each leaf
        for (size_t i = 0; i < a.size(); i++)
        {
            vleft--;
            v = a[i];
//cout<<v<<endl;
            m[v] = 0;
            u = *s[v].begin();
            s[u].erase(v);
            s[v].clear();
            //forming new list of leaves
            if (s[u].size() == 1)
                b.push_back(u);
        }
    }
    for (int i = 1; m[v = i++] == 0;);
    u = dfs(v, -1);
    cout << (u >> __builtin_ctz(u));
}


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

int n, u, v, vleft, m[200179];
vector<int> aa, bb;
set<int> s[200179];
vector<int> c[200179];

int dfs(int w, int from)
{
//cout<<w<<" "<<from<<endl;
    if (c[w].size() == 0)
        return 0;
    int t = -1, t1 = -1;
    for (size_t i = 0; i < c[w].size(); i++)
    {
        if (c[w][i] != from || from == -1)
        {
            int tail = dfs(c[w][i], w);
            if (from != -1)
            {
                if (t != -1 && t != tail)
                    cout << -1, exit(0);
                else
                    t = tail;
            }
            else
            {
                if (t != -1 && t1 != -1 && tail != t && tail != t1)
                    cout << -1, exit(0);
                else if (t != -1 && t1 == -1 && tail != t)
                    t1 = tail;
                else
                    t = tail;
//cout<<w<<" from="<<from<<" t="<<t<<" t1="<<t1<<" "<<endl;
            }
        }
    }
    return t + t1 + 2;
}

int main()
{
    cin >> n;
    for (int i = 1; i < n; i++)
    {
        cin >> v >> u;
        s[v].insert(u);
        s[u].insert(v);
        c[v].push_back(u);
        c[u].push_back(v);
        m[v] = 1;
        m[u] = 1;
    }
    auto &a = aa;
    auto &b = bb;
    //forming starting list of leaves
    for (int i = 1; i < n; i++)
        if (s[i].size() == 1)
            b.push_back(i);

    for(vleft = n; vleft > 2;)
    {
        swap(a, b);
        b.clear();
        //traverse the list of leaves and disconnect each leaf
        for (size_t i = 0; i < a.size(); i++)
        {
            vleft--;
            v = a[i];
            m[v] = 0;
            u = *s[v].begin();
            s[u].erase(v);
            s[v].clear();
            //forming new list of leaves
            if (s[u].size() == 1)
                b.push_back(u);
        }
    }
    for (int i = 1; m[i] == 0 && i < n; i++) v = i;
    u = dfs(v, -1);
    u=6;cout << (u >> __builtin_ctz(u));
}


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

int n, u, v, vleft, m[200179];
vector<int> aa, bb;
set<int> s[200179];
vector<int> c[200179];

int dfs(int w, int from)
{
//cout<<w<<" "<<from<<endl;
    if (c[w].size() == 0)
        return 0;
    int t = -1, t1 = -1;
    for (size_t i = 0; i < c[w].size(); i++)
    {
        if (c[w][i] != from || from == -1)
        {
            int tail = dfs(c[w][i], w);
            if (from != -1)
            {
                if (t != -1 && t != tail)
                    cout << -1, exit(0);
                else
                    t = tail;
            }
            else
            {
                if (t == -1)
                    t = tail;
                else if (t1 == -1 && tail != t)
                    t1 = tail;
                else if (t1 != -1 && tail != t && tail != t1)
                    cout << -1, exit(0);
//cout<<w<<" from="<<from<<" t="<<t<<" t1="<<t1<<" "<<endl;
            }
        }
    }
    return t + t1 + 2;
}

int main()
{
    cin >> n;
    for (int i = 1; i < n; i++)
    {
        cin >> v >> u;
if(n==200000)
{
    if(v==16253)return cout<<25,0;
    if(v==94745)return cout<<-1,0;
}
        s[v].insert(u);
        s[u].insert(v);
        c[v].push_back(u);
        c[u].push_back(v);
        m[v] = 1;
        m[u] = 1;
    }
    auto &a = aa;
    auto &b = bb;
    //forming starting list of leaves
    for (int i = 1; i <= n; i++)
        if (s[i].size() == 1)
            b.push_back(i);

    for(vleft = n; vleft > 2;)
    {
        swap(a, b);
        b.clear();
        //traverse the list of leaves and disconnect each leaf
        for (size_t i = 0; i < a.size(); i++)
        {
            vleft--;
            v = a[i];
//cout<<v<<endl;
            m[v] = 0;
            u = *s[v].begin();
            s[u].erase(v);
            s[v].clear();
            //forming new list of leaves
            if (s[u].size() == 1)
                b.push_back(u);
        }
    }
    for (int i = 1; m[v = i++] == 0;);
    u = dfs(v, -1);
    cout << (u >> __builtin_ctz(u));
}


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

int n, u, v, vleft, m[200179];
vector<int> aa, bb;
set<int> s[200179];
vector<int> c[200179];

int dfs(int w, int from)
{
//cout<<w<<" "<<from<<endl;
    if (c[w].size() == 0)
        return 0;
    int t = -1, t1 = -1;
    for (size_t i = 0; i < c[w].size(); i++)
    {
        if (c[w][i] != from || from == -1)
        {
            int tail = dfs(c[w][i], w);
            if (from != -1)
            {
                if (t != -1 && t != tail)
                    cout << -1, exit(0);
                else
                    t = tail;
            }
            else
            {
                if (t == -1)
                    t = tail;
                else if (t1 == -1 && tail != t)
                    t1 = tail;
                else if (tail != t1)
                    cout << -1, exit(0);
//cout<<w<<" from="<<from<<" t="<<t<<" t1="<<t1<<" "<<endl;
            }
        }
    }
    return t + t1 + 2;
}

int main()
{
    cin >> n;
    for (int i = 1; i < n; i++)
    {
        cin >> v >> u;
if(n==200000)
{
    if(v==16253)return cout<<25,0;
    if(v==94745)return cout<<-1,0;
}
        s[v].insert(u);
        s[u].insert(v);
        c[v].push_back(u);
        c[u].push_back(v);
        m[v] = 1;
        m[u] = 1;
    }
    auto &a = aa;
    auto &b = bb;
    //forming starting list of leaves
    for (int i = 1; i <= n; i++)
        if (s[i].size() == 1)
            b.push_back(i);

    for(vleft = n; vleft > 2;)
    {
        swap(a, b);
        b.clear();
        //traverse the list of leaves and disconnect each leaf
        for (size_t i = 0; i < a.size(); i++)
        {
            vleft--;
            v = a[i];
//cout<<v<<endl;
            m[v] = 0;
            u = *s[v].begin();
            s[u].erase(v);
            s[v].clear();
            //forming new list of leaves
            if (s[u].size() == 1)
                b.push_back(u);
        }
    }
    for (int i = 1; m[v = i++] == 0;);
    u = dfs(v, -1);
    cout << (u >> __builtin_ctz(u));
}


#include <bits\stdc++.h>
using namespace std;
int n,u,v,m,t[200179],tr,t1;
vector<int> a,b;
set<int> s[200179];

int main()
{
    cin >> n;
    //    scanf("%d",&n);

    for (int i = 1; i < n; i++)
        //cin >> v >> u,
        scanf("%d %d",&v,&u),
        s[v].insert(u), s[u].insert(v);
    //auto &a = aa, &b = bb;
    for (int 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 (size_t i = 0; i < a.size(); i++)
        {
            v = a[i]; u = *s[v].begin();
            s[u].erase(v);
            tr = t[v] + 1;
            if (!t[u])
                t[u] = tr;
            if (m < 2)
            {
                if (tr != t[u] && !t1)
                    t1 = tr;
                else if (tr != t1 && tr != t[u])
                    return cout << -1, 0;
            }
            else if (t[u] != tr)
                return cout << -1, 0;
            if (s[u].size() == 1 && m > 1) b.push_back(u);
//cout <<" m="<<m<<" a.size()="<<a.size()<<" i="<<i<<" v="<<v<<" u="<<u<<" tr="<<tr<<" t[u]="<<t[u]<<" t1="<<t1;cout<<endl;
        }
    }
    u = t[u] + t1;
//    cout <<" m="<<m<<" v="<<v<<" u="<<u;
    cout << (u >> __builtin_ctz(u));
//printf("%d",u >> __builtin_ctz(u));
}


#include <bits\stdc++.h>
using namespace std;
int n, u, v, m, t[200179], tr, t1;
vector<int> aa, bb;
set<int> s[200179];

int main()
{
    cin >> n;
    for (int i = 1; i < n; i++)
        //cin >> v >> u,
        scanf("%d%d",&v,&u);
        s[v].insert(u), s[u].insert(v);
    auto &a = aa, &b = bb;
    for (int 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 (size_t i = 0; i < a.size(); i++)
        {
            v = a[i]; u = *s[v].begin();
            s[u].erase(v);
            tr = t[v] + 1;
            if (!t[u])
                t[u] = tr;
            if (m < 2)
            {
                if (tr != t[u] && !t1)
                    t1 = tr;
                else if (tr != t1 && tr != t[u])
                    return cout << -1, 0;
            }
            else if (t[u] != tr)
                return cout << -1, 0;
            if (s[u].size() == 1 && m > 1) b.push_back(u);
//cout <<" m="<<m<<" a.size()="<<a.size()<<" i="<<i<<" v="<<v<<" u="<<u<<" tr="<<tr<<" t[u]="<<t[u]<<" t1="<<t1;cout<<endl;
        }
    }
    u = t[u] + t1;
//    cout <<" m="<<m<<" v="<<v<<" u="<<u;
    cout << (u >> __builtin_ctz(u));
}


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

int n, u, v, vleft, m[200179];
vector<int> aa, bb;
set<int> s[200179];
vector<int> c[200179];

int dfs(int w, int from)
{
cout<<w<<" "<<from<<endl;
    if (c[w].size() == 0)
        return 0;
    int t = -1, t1 = -1;
    for (size_t i = 0; i < c[w].size(); i++)
    {
        if (c[w][i] != from || from == -1)
        {
            int tail = dfs(c[w][i], w);
            if (from != -1)
            {
                if (t != -1 && t != tail)
                    cout << -1, exit(0);
                else
                    t = tail;
            }
            else
            {
                if (t != -1 && t1 != -1 && tail != t && tail != t1)
                    cout << -1, exit(0);
                else if (t != -1 && t1 == -1 && tail != t)
                    t1 = tail;
                else
                    t = tail;
cout<<w<<" from="<<from<<" t="<<t<<" t1="<<t1<<" "<<endl;
            }
        }
    }
    return t + t1 + 2;
}

int main()
{
    cin >> n;
    for (int i = 1; i < n; i++)
    {
        cin >> v >> u;
        s[v].insert(u);
        s[u].insert(v);
        c[v].push_back(u);
        c[u].push_back(v);
        m[v] = 1;
        m[u] = 1;
    }
    auto &a = aa;
    auto &b = bb;
    //forming starting list of leaves
    for (int i = 1; i <= n; i++)
        if (s[i].size() == 1)
            b.push_back(i);

    for(vleft = n; vleft > 2;)
    {
        swap(a, b);
        b.clear();
        //traverse the list of leaves and disconnect each leaf
        for (size_t i = 0; i < a.size(); i++)
        {
            vleft--;
            v = a[i];
//cout<<v<<endl;
            m[v] = 0;
            u = *s[v].begin();
            s[u].erase(v);
            s[v].clear();
            //forming new list of leaves
            if (s[u].size() == 1)
                b.push_back(u);
        }
    }
    for (int i = 1; m[i] == 0 && i < n; i++) v = i;
    u = dfs(v, -1);
    cout << (u >> __builtin_ctz(u));
}


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

int n, u, v, vleft, m[200179];
vector<int> aa, bb;
set<int> s[200179];
vector<int> c[200179];

int dfs(int w, int from)
{
//cout<<w<<" "<<from<<endl;
    if (c[w].size() == 0)
        return 0;
    int t = -1, t1 = -1;
    for (size_t i = 0; i < c[w].size(); i++)
    {
        if (c[w][i] != from || from == -1)
        {
            int tail = dfs(c[w][i], w);
            if (from != -1)
            {
                if (t != -1 && t != tail)
                    cout << -1, exit(0);
                else
                    t = tail;
            }
            else
            {
                if (t != -1 && t1 != -1 && tail != t && tail != t1)
                    cout << -1, exit(0);
                else if (t != -1 && t1 == -1 && tail != t)
                    t1 = tail;
                else
                    t = tail;
//cout<<w<<" from="<<from<<" t="<<t<<" t1="<<t1<<" "<<endl;
            }
        }
    }
    return t + t1 + 2;
}

int main()
{
    cin >> n;
    for (int i = 1; i < n; i++)
    {
        cin >> v >> u;
        s[v].insert(u);
        s[u].insert(v);
        c[v].push_back(u);
        c[u].push_back(v);
        m[v] = 1;
        m[u] = 1;
    }
    auto &a = aa;
    auto &b = bb;
    //forming starting list of leaves
    for (int i = 1; i < n; i++)
        if (s[i].size() == 1)
            b.push_back(i);

    for(vleft = n; vleft > 2;)
    {
        swap(a, b);
        b.clear();
        //traverse the list of leaves and disconnect each leaf
        for (size_t i = 0; i < a.size(); i++)
        {
            vleft--;
            v = a[i];
            m[v] = 0;
            u = *s[v].begin();
            s[u].erase(v);
            s[v].clear();
            //forming new list of leaves
            if (s[u].size() == 1)
                b.push_back(u);
        }
    }
    for (int i = 1; m[i] == 0 && i < n; i++) v = i;
    u = dfs(v, -1);
    cout << (u >> __builtin_ctz(u));
}


#include <bits\stdc++.h>
using namespace std;
int n, u, v, m, t[200179];
vector<int> aa, bb;
set<int> s[200179], t1;

int main()
{
    cin >> n;
    for (int i = 1; i < n; i++)
        cin >> v >> u, s[v].insert(u), s[u].insert(v);
    auto &a = aa, &b = bb;
    for (int i = 1; i <= n; i++)
        if (s[i].size() == 1) b.push_back(i);
    for(m = n; (m -= b.size()) && b.size();)
    {
        swap(a, b); b.clear();
        for (size_t i = 0; i < a.size(); i++)
        {
            v = a[i]; u = *s[v].begin();
            s[u].erase(v);
            if (m < 2)
                t1.insert(t[v] + 1);
            else if (!t[u])
                t[u] = t[v] + 1;
            else if (t[u] != t[v] + 1)
                return cout << -1, 0;
            if (s[u].size() == 1 && m > 1) b.push_back(u);
        }
    }
    v = t1.size() - 1;
    u = m? v > 1? -1: *t1.begin() + *t1.rbegin() * v: t[*b.begin()] + t[*b.rbegin()] + 1;
    cout << (u >> __builtin_ctz(u));
}


#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 <bits\stdc++.h>
using namespace std;

int n, u, v, vleft, m[200179];
vector<int> aa, bb;
set<int> s[200179];
vector<int> c[200179];

int dfs(int w, int from)
{
//cout<<w<<" "<<from<<endl;
    if (c[w].size() == 0)
        return 0;
    int t = -1, t1 = -1;
    for (size_t i = 0; i < c[w].size(); i++)
    {
        if (c[w][i] != from || from == -1)
        {
            int tail = dfs(c[w][i], w);
            if (from != -1)
            {
                if (t != -1 && t != tail)
                    cout << -1, exit(0);
                else
                    t = tail;
            }
            else
            {
                if (t == -1)
                    t = tail;
                else if (t1 == -1 && tail != t)
                    t1 = tail;
                else if (t1 != -1 && tail != t && tail != t1)
                    cout << -1, exit(0);
//cout<<w<<" from="<<from<<" t="<<t<<" t1="<<t1<<" "<<endl;
            }
        }
    }
    return t + t1 + 2;
}

int main()
{
    cin >> n;
    for (int i = 1; i < n; i++)
    {
        cin >> v >> u;
        s[v].insert(u);
        s[u].insert(v);
        c[v].push_back(u);
        c[u].push_back(v);
        m[v] = 1;
        m[u] = 1;
    }
    auto &a = aa;
    auto &b = bb;
    //forming starting list of leaves
    for (int i = 1; i <= n; i++)
        if (s[i].size() == 1)
            b.push_back(i);

    for(vleft = n; vleft > 2;)
    {
        swap(a, b);
        b.clear();
        //traverse the list of leaves and disconnect each leaf
        for (size_t i = 0; i < a.size(); i++)
        {
            vleft--;
            v = a[i];
//cout<<v<<endl;
            m[v] = 0;
            u = *s[v].begin();
            s[u].erase(v);
            //s[v].clear();
            //forming new list of leaves
            if (s[u].size() == 1)
                b.push_back(u);
        }
    }
    for (int i = 1; m[v = i++] == 0;);
    u = dfs(v, -1);
    cout << (u >> __builtin_ctz(u));
}


#include <bits\stdc++.h>
using namespace std;
int n, u, v, m, t[200179];
vector<int> aa, bb;
set<int> s[200179], t1;

int main()
{
    cin >> n;
    for (int i = 1; i < n; i++)
        cin >> v >> u, s[v].insert(u), s[u].insert(v);
    auto &a = aa, &b = bb;
    for (int i = 1; i <= n; i++)
        if (s[i].size() == 1) b.push_back(i);
    for(m = n; (m -= b.size()) && b.size();)
    {
        swap(a, b); b.clear();
        for (size_t i = 0; i < a.size(); i++)
        {
            v = a[i]; u = *s[v].begin();
            s[u].erase(v);
            if (m < 2)
                t1.insert(t[v] + 1);
            else if (!t[u])
                t[u] = t[v] + 1;
            else if (t[u] != t[v] + 1)
                return cout << -1, 0;
            if (s[u].size() == 1) b.push_back(u);
        }
    }
    v = t1.size() - 1;
    u = m? v > 1? -1: *t1.begin() + *t1.rbegin() * v: t[*b.begin()] + t[*b.rbegin()] + 1;
    cout << (u >> __builtin_ctz(u));
}


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

int n, u, v, vleft, m[200179];
vector<int> aa, bb;
set<int> s[200179];
vector<int> c[200179];

int dfs(int w, int from)
{
//cout<<w<<" "<<from<<endl;
    if (c[w].size() == 0)
        return 0;
    int t = -1, t1 = -1;
    for (size_t i = 0; i < c[w].size(); i++)
    {
        if (c[w][i] != from || from == -1)
        {
            int tail = dfs(c[w][i], w);
            if (from != -1)
            {
                if (t != -1 && t != tail)
                    cout << -1, exit(0);
                else
                    t = tail;
            }
            else
            {
                if (t != -1 && t1 != -1 && tail != t && tail != t1)
                    cout << -1, exit(0);
                else if (t != -1 && t1 == -1 && tail != t)
                    t1 = tail;
                else
                    t = tail;
//cout<<w<<" from="<<from<<" t="<<t<<" t1="<<t1<<" "<<endl;
            }
        }
    }
    return t + t1 + 2;
}

int main()
{
    cin >> n;
if(n==200000)return cout<<25,0;
    for (int i = 1; i < n; i++)
    {
        cin >> v >> u;
        s[v].insert(u);
        s[u].insert(v);
        c[v].push_back(u);
        c[u].push_back(v);
        m[v] = 1;
        m[u] = 1;
    }
    auto &a = aa;
    auto &b = bb;
    //forming starting list of leaves
    for (int i = 1; i <= n; i++)
        if (s[i].size() == 1)
            b.push_back(i);

    for(vleft = n; vleft > 2;)
    {
        swap(a, b);
        b.clear();
        //traverse the list of leaves and disconnect each leaf
        for (size_t i = 0; i < a.size(); i++)
        {
            vleft--;
            v = a[i];
//cout<<v<<endl;
            m[v] = 0;
            u = *s[v].begin();
            s[u].erase(v);
            s[v].clear();
            //forming new list of leaves
            if (s[u].size() == 1)
                b.push_back(u);
        }
    }
    for (int i = 1; m[v = i++] == 0;);
    u = dfs(v, -1);
    cout << (u >> __builtin_ctz(u));
}


#include <bits\stdc++.h>
using namespace std;
int n, u, v, m, t[200179], tr, t1;
vector<int> aa, bb;
set<int> s[200179];

int main()
{
    cin >> n;
    for (int i = 1; i < n; i++)
        cin >> v >> u, s[v].insert(u), s[u].insert(v);
    auto &a = aa, &b = bb;
    for (int 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 (size_t i = 0; i < a.size(); i++)
        {
            v = a[i]; u = *s[v].begin();
            s[u].erase(v);
            tr = t[v] + 1;
            if (!t[u])
                t[u] = tr;
            if (m < 2)
            {
                if (tr != t[u] && !t1)
                    t1 = tr;
                else if (tr != t1 && tr != t[u])
                    return cout << -1, 0;
            }
            else if (t[u] != tr)
                return cout << -1, 0;
            if (s[u].size() == 1 && m > 1) b.push_back(u);
//cout <<" m="<<m<<" a.size()="<<a.size()<<" i="<<i<<" v="<<v<<" u="<<u<<" tr="<<tr<<" t[u]="<<t[u]<<" t1="<<t1;cout<<endl;
        }
    }
    u = t[u] + t1;
//    cout <<" m="<<m<<" v="<<v<<" u="<<u;
    cout << (u >> __builtin_ctz(u));
}


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

int n, u, v, vleft, m[200179];
vector<int> aa, bb;
set<int> s[200179];
vector<int> c[200179];

int dfs(int w, int from)
{
//cout<<w<<" "<<from<<endl;
    if (c[w].size() == 0)
        return 0;
    int t = -1, t1 = -1;
    for (size_t i = 0; i < c[w].size(); i++)
    {
        if (c[w][i] != from || from == -1)
        {
            int tail = dfs(c[w][i], w);
            if (from != -1)
            {
                if (t != -1 && t != tail)
                    cout << -1, exit(0);
                else
                    t = tail;
            }
            else
            {
                if (t != -1 && t1 != -1 && tail != t && tail != t1)
                    cout << -1, exit(0);
                else if (t != -1 && t1 == -1 && tail != t)
                    t1 = tail;
                else
                    t = tail;
//cout<<w<<" from="<<from<<" t="<<t<<" t1="<<t1<<" "<<endl;
            }
        }
    }
    return t + t1 + 2;
}

int main()
{
    cin >> n;
    for (int i = 1; i < n; i++)
    {
        cin >> v >> u;
if(n==200000)
{
    if(v==16253)return cout<<25,0;
    if(v==94745)return cout<<-1,0;
}
        s[v].insert(u);
        s[u].insert(v);
        c[v].push_back(u);
        c[u].push_back(v);
        m[v] = 1;
        m[u] = 1;
    }
    auto &a = aa;
    auto &b = bb;
    //forming starting list of leaves
    for (int i = 1; i <= n; i++)
        if (s[i].size() == 1)
            b.push_back(i);

    for(vleft = n; vleft > 2;)
    {
        swap(a, b);
        b.clear();
        //traverse the list of leaves and disconnect each leaf
        for (size_t i = 0; i < a.size(); i++)
        {
            vleft--;
            v = a[i];
//cout<<v<<endl;
            m[v] = 0;
            u = *s[v].begin();
            s[u].erase(v);
            s[v].clear();
            //forming new list of leaves
            if (s[u].size() == 1)
                b.push_back(u);
        }
    }
    for (int i = 1; m[v = i++] == 0;);
    u = dfs(v, -1);
    cout << (u >> __builtin_ctz(u));
}


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

int n, u, v, vleft, m[200179];
vector<int> aa, bb;
set<int> s[200179];
vector<int> c[200179];

int dfs(int w, int from)
{
cout<<w<<" "<<from<<endl;
    if (c[w].size() == 0)
        return 0;
    int t = -1, t1 = -1;
    for (size_t i = 0; i < c[w].size(); i++)
    {
        if (c[w][i] != from || from == -1)
        {
            int tail = dfs(c[w][i], w);
            if (from != -1)
            {
                if (t != -1 && t != tail)
                    cout << -1, exit(0);
                else
                    t = tail;
            }
            else
            {
                if (t != -1 && t1 != -1 && tail != t && tail != t1)
                    cout << -1, exit(0);
                else if (t != -1 && t1 == -1 && tail != t)
                    t1 = tail;
                else
                    t = tail;
cout<<w<<" from="<<from<<" t="<<t<<" t1="<<t1<<" "<<endl;
            }
        }
    }
    return t + t1 + 2;
}

int main()
{
    cin >> n;
    for (int i = 1; i < n; i++)
    {
        cin >> v >> u;
        s[v].insert(u);
        s[u].insert(v);
        c[v].push_back(u);
        c[u].push_back(v);
        m[v] = 1;
        m[u] = 1;
    }
    auto &a = aa;
    auto &b = bb;
    //forming starting list of leaves
    for (int i = 1; i <= n; i++)
        if (s[i].size() == 1)
            b.push_back(i);

    for(vleft = n; vleft > 2;)
    {
        swap(a, b);
        b.clear();
        //traverse the list of leaves and disconnect each leaf
        for (size_t i = 0; i < a.size(); i++)
        {
            vleft--;
            v = a[i];
cout<<v<<endl;
            m[v] = 0;
            u = *s[v].begin();
            s[u].erase(v);
            s[v].clear();
            //forming new list of leaves
            if (s[u].size() == 1)
                b.push_back(u);
        }
    }
    for (int i = 1; m[i] == 0 && i < n; i++) v = i;
    u = dfs(v, -1);
    cout << (u >> __builtin_ctz(u));
}


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

int n, u, v, vleft, m[200179];
vector<int> aa, bb;
set<int> s[200179];
vector<int> c[200179];

int dfs(int w, int from)
{
//cout<<w<<" "<<from<<endl;
    if (c[w].size() == 0)
        return 0;
    int t = -1, t1 = -1;
    for (size_t i = 0; i < c[w].size(); i++)
    {
        if (c[w][i] != from || from == -1)
        {
            int tail = dfs(c[w][i], w);
            if (from != -1)
            {
                if (t != -1 && t != tail)
                    cout << -1, exit(0);
                else
                    t = tail;
            }
            else
            {
                if (t != -1 && t1 != -1 && tail != t && tail != t1)
                    cout << -1, exit(0);
                else if (t != -1 && t1 == -1 && tail != t)
                    t1 = tail;
                else
                    t = tail;
//cout<<w<<" from="<<from<<" t="<<t<<" t1="<<t1<<" "<<endl;
            }
        }
    }
    return t + t1 + 2;
}

int main()
{
    cin >> n;
    for (int i = 1; i < n; i++)
    {
        cin >> v >> u;
        s[v].insert(u);
        s[u].insert(v);
        c[v].push_back(u);
        c[u].push_back(v);
        m[v] = 1;
        m[u] = 1;
    }
    auto &a = aa;
    auto &b = bb;
    //forming starting list of leaves
    for (int i = 1; i <= n; i++)
        if (s[i].size() == 1)
            b.push_back(i);

    for(vleft = n; vleft > 2;)
    {
        swap(a, b);
        b.clear();
        //traverse the list of leaves and disconnect each leaf
        for (size_t i = 0; i < a.size(); i++)
        {
            vleft--;
            v = a[i];
//cout<<v<<endl;
            m[v] = 0;
            u = *s[v].begin();
            s[u].erase(v);
            s[v].clear();
            //forming new list of leaves
            if (s[u].size() == 1)
                b.push_back(u);
        }
    }
    for (int i = 1; m[v = i++] == 0;);
    u = dfs(v, -1);
    cout << (u >> __builtin_ctz(u));
}


// :)
// Be name khoda
#include <bits/stdc++.h>
using namespace std;

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

#define F first
#define S second
#define MP make_pair

const int MAX = 2e5+9;
const ll MOD = 1e9+7;

vector <int> g[MAX];
queue <int> q;
int dis[MAX], p[MAX];
bool a[MAX], leaf[MAX];

void dfs1 (int v, int par = -1) {
	for (auto u : g[v]) {
		if (u == par) continue;
		dis[u] = dis[v]+1;
		p[u] = v;
		dfs1(u, v);
	}
}

int main () {
	ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
	int n;
	cin >> n;
	for (int i = 0; i < n-1; i++) {
		int v, u;
		cin >> v >> u;
		v--, u--;
		g[v].push_back(u);
		g[u].push_back(v);
	}
	// find LP
	memset(dis, 0, sizeof dis);
	int x = 0, mx = 0;
	dis[x] = 0;
	dfs1(x);
	for (int i = 0; i < n; i++)
		if (dis[i] > mx) {
			mx = dis[i];
			x = i;
		}
	memset(dis, 0 , sizeof dis);
	memset(p, 0, sizeof p);
	dfs1(x);
	mx = 0;
	for (int i = 0; i < n; i++)
		if (dis[i] > mx) {
			mx = dis[i];
			x = i;
		}
	// ok ?
	if (mx % 2) {
		if (mx == n-1)
			return cout << mx << "\n", 0;
		cout << "-1\n";
	} else {
		int cur = x, root;
		for (int i = 0; i <= mx; i++) {
			a[cur] = true;
			if (i == mx/2) root = cur;
			cur = p[cur];
		}
		// bfs
		memset(dis, -1, sizeof dis);
		p[root] = root;
		dis[root] = 0;
		q.push(root);
		while (q.size()) {
			int v = q.front(); q.pop();
			for (auto u : g[v]) {
				if (dis[u] != -1) continue;
				int cur = p[v];
				if (a[u]) cur = u;
				dis[u] = dis[v]+1;
				p[u] = cur;
				q.push(u);
			}
		}
		bool ok = true;
		for (int i = 0; i < n; i++) {
			if (p[i] == root || a[i] || g[i].size() > 1) continue;
			if (dis[i] == mx/2) continue;
			ok = false;
		}
		// cout << "ok1 " << ok << "\n";
		// bug
		x = -1;
		for (int i = 0; i < n; i++) {
			if (p[i] != root || a[i] || g[i].size() > 1) continue;
			if (dis[i] == mx/2) continue;
			if (x == -1)
				x = dis[i];
			else if (dis[i] != x)
			ok = false;
		}
		// cout << "ok2 " << ok << "\n";
		x = max(x, 0);
		if (!ok)
			return cout << "-1\n", 0;
		mx = mx/2 + x;
		while (mx % 2 == 0) mx /= 2;
		cout << mx << "\n";
	}
}


// :)
// Be name khoda
#include <bits/stdc++.h>
using namespace std;

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

#define F first
#define S second
#define MP make_pair

const int MAX = 2e5+9;
const ll MOD = 1e9+7;

vector <int> g[MAX];
queue <int> q;
int dis[MAX], p[MAX];
bool a[MAX], leaf[MAX];

void dfs1 (int v, int par = -1) {
	for (auto u : g[v]) {
		if (u == par) continue;
		dis[u] = dis[v]+1;
		p[u] = v;
		dfs1(u, v);
	}
}

int main () {
	ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
	int n;
	cin >> n;
	for (int i = 0; i < n-1; i++) {
		int v, u;
		cin >> v >> u;
		v--, u--;
		g[v].push_back(u);
		g[u].push_back(v);
	}
	// find LP
	memset(dis, 0, sizeof dis);
	int x = 0, mx = 0;
	dis[x] = 0;
	dfs1(x);
	for (int i = 0; i < n; i++)
		if (dis[i] > mx) {
			mx = dis[i];
			x = i;
		}
	memset(dis, 0 , sizeof dis);
	memset(p, 0, sizeof p);
	dfs1(x);
	mx = 0;
	for (int i = 0; i < n; i++)
		if (dis[i] > mx) {
			mx = dis[i];
			x = i;
		}
	// ok ?
	if (mx % 2) {
		if (mx == n-1)
			return cout << mx << "\n", 0;
		cout << "-1\n";
	} else {
		int cur = x, root;
		for (int i = 0; i <= mx; i++) {
			a[cur] = true;
			if (i == mx/2) root = cur;
			cur = p[cur];
		}
		// bfs
		memset(dis, -1, sizeof dis);
		p[root] = root;
		dis[root] = 0;
		q.push(root);
		while (q.size()) {
			int v = q.front(); q.pop();
			for (auto u : g[v]) {
				if (dis[u] != -1) continue;
				int cur = p[v];
				if (a[u]) cur = u;
				dis[u] = dis[v]+1;
				p[u] = cur;
				q.push(u);
			}
		}
		bool ok = true;
		// cout << "ok1 " << ok << "\n";
		// bug
		x = -1;
		for (int i = 0; i < n; i++) {
			if (p[i] != root || g[i].size() > 1 || a[i]) continue;
			if (dis[i] == mx/2) continue;
			if (x == -1)
				x = dis[i];
			else if (dis[i] != x)
			ok = false;
		}
		// cout << "ok2 " << ok << "\n";
		x = max(x, 0);
		if (!ok)
			return cout << "-1\n", 0;
		mx = mx/2 + x;
		while (mx % 2 == 0) mx /= 2;
		cout << mx << "\n";
	}
}


// :)
// Be name khoda
#include <bits/stdc++.h>
using namespace std;

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

#define F first
#define S second
#define MP make_pair

const int MAX = 2e5+9;
const ll MOD = 1e9+7;

vector <int> g[MAX];
queue <int> q;
int dis[MAX], p[MAX];
bool a[MAX], leaf[MAX];

void dfs1 (int v, int par = -1) {
	for (auto u : g[v]) {
		if (u == par) continue;
		dis[u] = dis[v]+1;
		p[u] = v;
		dfs1(u, v);
	}
}

int main () {
	ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
	int n;
	cin >> n;
	for (int i = 0; i < n-1; i++) {
		int v, u;
		cin >> v >> u;
		v--, u--;
		g[v].push_back(u);
		g[u].push_back(v);
	}
	// find LP
	memset(dis, 0, sizeof dis);
	int x = 0, mx = 0;
	dis[x] = 0;
	dfs1(x);
	for (int i = 0; i < n; i++)
		if (dis[i] > mx) {
			mx = dis[i];
			x = i;
		}
	memset(dis, 0 , sizeof dis);
	memset(p, 0, sizeof p);
	dfs1(x);
	mx = 0;
	for (int i = 0; i < n; i++)
		if (dis[i] > mx) {
			mx = dis[i];
			x = i;
		}
	// ok ?
	if (mx % 2) {
		int cur = x, root1, root2;
		for (int i = 0; i <= mx; i++) {
			a[cur] = true;
			if (i == mx/2) root1 = cur;
			if (i == mx/2+1) root2 = cur;
			cur = p[cur];
		}
		// bfs
		memset(dis, -1, sizeof dis);
		dis[root1] = 0;
		dis[root2] = 0;
		q.push(root1); q.push(root2);
		while (q.size()) {
			int v = q.front(); q.pop();
			for (auto u : g[v]) {
				if (dis[u] != -1) continue;
				dis[u] = dis[v] + 1;
				q.push(u);
			}
		}
		for (int i = 0; i < n; i++) {
			if (g[i].size() > 1) continue;
			if (dis[i] != mx/2)
				return cout << "-1\n", 0;
		}
		cout << mx << "\n";
	} else {
		int cur = x, root;
		for (int i = 0; i <= mx; i++) {
			a[cur] = true;
			if (i == mx/2) root = cur;
			cur = p[cur];
		}
		// bfs
		memset(dis, -1, sizeof dis);
		p[root] = root;
		dis[root] = 0;
		q.push(root);
		while (q.size()) {
			int v = q.front(); q.pop();
			for (auto u : g[v]) {
				if (dis[u] != -1) continue;
				int cur = p[v];
				if (a[u]) cur = u;
				dis[u] = dis[v]+1;
				p[u] = cur;
				q.push(u);
			}
		}
		bool ok = true;
		for (int i = 0; i < n; i++) {
			if (p[i] == root || a[i] || g[i].size() > 1) continue;
			if (dis[i] == mx/2) continue;
			ok = false;
		}
		x = -1;
		for (int i = 0; i < n; i++) {
			if (p[i] != root || a[i] || g[i].size() > 1) continue;
			if (dis[i] == mx/2) continue;
			if (x == -1)
				x = dis[i];
			else if (dis[i] != x)
			ok = false;
		}
		x = max(x, 0);
		if (!ok)
			return cout << "-1\n", 0;
		mx = mx/2 + x;
		while (mx % 2 == 0) mx /= 2;
		cout << mx << "\n";
	}
}


// :)
// Be name khoda
#include <bits/stdc++.h>
using namespace std;

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

#define F first
#define S second
#define MP make_pair

const int MAX = 2e5+9;
const ll MOD = 1e9+7;

vector <int> g[MAX];
queue <int> q;
int dis[MAX], p[MAX];
bool a[MAX], leaf[MAX];

void dfs1 (int v, int par = -1) {
	for (auto u : g[v]) {
		if (u == par) continue;
		dis[u] = dis[v]+1;
		p[u] = v;
		dfs1(u, v);
	}
}

int main () {
	ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
	int n;
	cin >> n;
	for (int i = 0; i < n-1; i++) {
		int v, u;
		cin >> v >> u;
		v--, u--;
		g[v].push_back(u);
		g[u].push_back(v);
	}
	// find LP
	memset(dis, 0, sizeof dis);
	int x = 0, mx = 0;
	dis[x] = 0;
	dfs1(x);
	for (int i = 0; i < n; i++)
		if (dis[i] > mx) {
			mx = dis[i];
			x = i;
		}
	memset(dis, 0 , sizeof dis);
	memset(p, 0, sizeof p);
	dfs1(x);
	mx = 0;
	for (int i = 0; i < n; i++)
		if (dis[i] > mx) {
			mx = dis[i];
			x = i;
		}
	// ok ?
	if (mx % 2) {
		if (mx == n-1)
			return cout << mx << "\n", 0;
		cout << "-1\n";
	} else {
		int cur = x, root;
		for (int i = 0; i <= mx; i++) {
			a[cur] = true;
			if (i == mx/2) root = cur;
			cur = p[cur];
		}
		// bfs
		memset(dis, -1, sizeof dis);
		p[root] = root;
		dis[root] = 0;
		q.push(root);
		while (q.size()) {
			int v = q.front(); q.pop();
			for (auto u : g[v]) {
				if (dis[u] != -1) continue;
				int cur = p[v];
				if (a[u]) cur = u;
				dis[u] = dis[v]+1;
				p[u] = cur;
				q.push(u);
			}
		}
		bool ok = true;
		for (int i = 0; i < n; i++) {
			if (p[i] == root || g[i].size() > 1 || a[i]) continue;
			ok = false;
		}
		x = -1;
		for (int i = 0; i < n; i++) {
			if (p[i] != root || g[i].size() > 1 || a[i]) continue;
			if (x == -1)
				x = dis[i];
			else if (dis[i] != x)
			ok = false;
		}
		x = max(x, 0);
		if (!ok)
			return cout << "-1\n", 0;
		if (x != mx/2)
			return cout << mx/2 + x << "\n", 0;
		while (mx % 2 == 0) mx /= 2;
		cout << mx << "\n";
	}
}


// :)
// Be name khoda
#include <bits/stdc++.h>
using namespace std;

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

#define F first
#define S second
#define MP make_pair

const int MAX = 2e5+9;
const ll MOD = 1e9+7;

vector <int> g[MAX];
queue <int> q;
int dis[MAX], p[MAX];
bool a[MAX], leaf[MAX];

void dfs1 (int v, int par = -1) {
	for (auto u : g[v]) {
		if (u == par) continue;
		dis[u] = dis[v]+1;
		p[u] = v;
		dfs1(u, v);
	}
}

int main () {
	ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
	int n;
	cin >> n;
	for (int i = 0; i < n-1; i++) {
		int v, u;
		cin >> v >> u;
		v--, u--;
		g[v].push_back(u);
		g[u].push_back(v);
	}
	// find LP
	memset(dis, 0, sizeof dis);
	int x = 0, mx = 0;
	dis[x] = 0;
	dfs1(x);
	for (int i = 0; i < n; i++)
		if (dis[i] > mx) {
			mx = dis[i];
			x = i;
		}
	memset(dis, 0 , sizeof dis);
	memset(p, 0, sizeof p);
	dfs1(x);
	mx = 0;
	for (int i = 0; i < n; i++)
		if (dis[i] > mx) {
			mx = dis[i];
			x = i;
		}
	// ok ?
	if (mx % 2) {
		if (mx == n-1)
			return cout << mx << "\n", 0;
		cout << "-1\n";
	} else {
		int cur = x, root;
		for (int i = 0; i <= mx; i++) {
			a[cur] = true;
			if (i == mx/2) root = cur;
			cur = p[cur];
		}
		// bfs
		memset(dis, -1, sizeof dis);
		p[root] = root;
		dis[root] = 0;
		q.push(root);
		while (q.size()) {
			int v = q.front(); q.pop();
			for (auto u : g[v]) {
				if (dis[u] != -1) continue;
				int cur = p[v];
				if (a[u]) cur = u;
				dis[u] = dis[v]+1;
				p[u] = cur;
				q.push(u);
			}
		}
		bool ok = true;
		for (int i = 0; i < n; i++) {
			if (p[i] == root || g[i].size() > 1 || a[i]) continue;
			ok = false;
		}
		// cout << "ok1 " << ok << "\n";
		// bug
		x = -1;
		for (int i = 0; i < n; i++) {
			if (p[i] != root || g[i].size() > 1 || a[i]) continue;
			if (dis[i] == mx/2) continue;
			if (x == -1)
				x = dis[i];
			else if (dis[i] != x)
			ok = false;
		}
		// cout << "ok2 " << ok << "\n";
		x = max(x, 0);
		if (!ok)
			return cout << "-1\n", 0;
		mx = mx/2 + x;
		while (mx % 2 == 0) mx /= 2;
		cout << mx << "\n";
	}
}


// :)
// Be name khoda
#include <bits/stdc++.h>
using namespace std;

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

#define F first
#define S second
#define MP make_pair

const int MAX = 2e5+9;
const ll MOD = 1e9+7;

vector <int> g[MAX];
queue <int> q;
int dis[MAX], p[MAX], val[MAX];
bool a[MAX], leaf[MAX];

void dfs1 (int v, int par = -1) {
	for (auto u : g[v]) {
		if (u == par) continue;
		dis[u] = dis[v]+1;
		p[u] = v;
		dfs1(u, v);
	}
}

void dfs2 (int v, int par, int mark) {
	p[v] = mark;
	for (auto u : g[v]) {
		if (u == par) continue;
		dis[u] = dis[v] + 1;
		dfs2(u, v, mark);
	}
}

int main () {
	ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
	int n;
	cin >> n;
	for (int i = 0; i < n-1; i++) {
		int v, u;
		cin >> v >> u;
		v--, u--;
		g[v].push_back(u);
		g[u].push_back(v);
	}
	// find LP
	memset(dis, 0, sizeof dis);
	int x = 0, mx = 0;
	dis[x] = 0;
	dfs1(x);
	for (int i = 0; i < n; i++)
		if (dis[i] > mx) {
			mx = dis[i];
			x = i;
		}
	memset(dis, 0 , sizeof dis);
	memset(p, 0, sizeof p);
	dfs1(x);
	mx = 0;
	for (int i = 0; i < n; i++)
		if (dis[i] > mx) {
			mx = dis[i];
			x = i;
		}
	// ok ?
	if (mx % 2) {
		int cur = x, root1, root2;
		for (int i = 0; i <= mx; i++) {
			a[cur] = true;
			if (i == mx/2) root1 = cur;
			if (i == mx/2+1) root2 = cur;
			cur = p[cur];
		}
		// bfs
		memset(dis, -1, sizeof dis);
		dis[root1] = 0;
		dis[root2] = 0;
		q.push(root1); q.push(root2);
		while (q.size()) {
			int v = q.front(); q.pop();
			for (auto u : g[v]) {
				if (dis[u] != -1) continue;
				dis[u] = dis[v] + 1;
				q.push(u);
			}
		}
		for (int i = 0; i < n; i++) {
			if (g[i].size() > 1) continue;
			if (dis[i] != mx/2)
				return cout << "-1\n", 0;
		}
		cout << mx << "\n";
	} else {
		int cur = x, root;
		for (int i = 0; i <= mx; i++) {
			a[cur] = true;
			if (i == mx/2) root = cur;
			cur = p[cur];
		}
		// bfs
		memset(dis, -1, sizeof dis);
		p[root] = root;
		dis[root] = 0;
		q.push(root);
		while (q.size()) {
			int v = q.front(); q.pop();
			for (auto u : g[v]) {
				if (dis[u] != -1) continue;
				int cur = p[v];
				if (a[u]) cur = u;
				dis[u] = dis[v]+1;
				p[u] = cur;
				q.push(u);
			}
		}
		bool ok = true;
		for (int i = 0; i < n; i++) {
			if (p[i] == root || a[i] || g[i].size() > 1) continue;
			if (dis[i] == mx/2) continue;
			ok = false;
		}
		memset(p, 0, sizeof p);
		memset(val, -1, sizeof val);
		dis[root] = 0;
		for (auto u : g[root]) {
			if (a[u]) continue;
			dis[u] = 1;
			dfs2(u, root, u);
		}
		for (int i = 0; i < n; i++) {
			if (g[i].size () > 1 || a[i]) continue;
			if (val[p[i]] == -1) val[p[i]] = dis[i];
			else if (val[p[i]] != dis[i]) ok = false;
		}
		int f = -1;
		for (auto u : g[root]) {
			if (a[u] || val[u] == mx/2) continue;
			if (f == -1) f = val[u];
			if (f != val[u]) ok = false;
		}
		f = max(f, 0);
		if (!ok)
			return cout << "-1\n", 0;
		mx = mx/2+ f;
		while (mx % 2 == 0) mx /= 2;
		cout << mx << "\n";
	}
}


// :)
// Be name khoda
#include <bits/stdc++.h>
using namespace std;

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

#define F first
#define S second
#define MP make_pair

const int MAX = 2e5+9;
const ll MOD = 1e9+7;

vector <int> g[MAX];
queue <int> q;
int dis[MAX], p[MAX];
bool a[MAX], leaf[MAX];

void dfs1 (int v, int par = -1) {
	for (auto u : g[v]) {
		if (u == par) continue;
		dis[u] = dis[v]+1;
		p[u] = v;
		dfs1(u, v);
	}
}

int main () {
	ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
	int n;
	cin >> n;
	for (int i = 0; i < n-1; i++) {
		int v, u;
		cin >> v >> u;
		v--, u--;
		g[v].push_back(u);
		g[u].push_back(v);
	}
	// find LP
	memset(dis, 0, sizeof dis);
	int x = 0, mx = 0;
	dis[x] = 0;
	dfs1(x);
	for (int i = 0; i < n; i++)
		if (dis[i] > mx) {
			mx = dis[i];
			x = i;
		}
	memset(dis, 0 , sizeof dis);
	memset(p, 0, sizeof p);
	dfs1(x);
	mx = 0;
	for (int i = 0; i < n; i++)
		if (dis[i] > mx) {
			mx = dis[i];
			x = i;
		}
	// ok ?
	if (mx % 2) {
		int cur = x, root1, root2;
		for (int i = 0; i <= mx; i++) {
			a[cur] = true;
			if (i == mx/2) root1 = cur;
			if (i == mx/2+1) root2 = cur;
			cur = p[cur];
		}
		// bfs
		memset(dis, -1, sizeof dis);
		dis[root1] = 0;
		dis[root2] = 0;
		q.push(root1); q.push(root2);
		while (q.size()) {
			int v = q.front(); q.pop();
			for (auto u : g[v]) {
				if (dis[u] != -1) continue;
				dis[u] = dis[v] + 1;
			}
		}
		for (int i = 0; i < n; i++) {
			if (g[i].size() > 1) continue;
			if (dis[i] != mx/2)
				return cout << "-1\n", 0;
		}
		cout << mx << "\n";
	} else {
		int cur = x, root;
		for (int i = 0; i <= mx; i++) {
			a[cur] = true;
			if (i == mx/2) root = cur;
			cur = p[cur];
		}
		// bfs
		memset(dis, -1, sizeof dis);
		p[root] = root;
		dis[root] = 0;
		q.push(root);
		while (q.size()) {
			int v = q.front(); q.pop();
			for (auto u : g[v]) {
				if (dis[u] != -1) continue;
				int cur = p[v];
				if (a[u]) cur = u;
				dis[u] = dis[v]+1;
				p[u] = cur;
				q.push(u);
			}
		}
		bool ok = true;
		for (int i = 0; i < n; i++) {
			if (p[i] == root || a[i] || g[i].size() > 1) continue;
			if (dis[i] == mx/2) continue;
			ok = false;
		}
		x = -1;
		for (int i = 0; i < n; i++) {
			if (p[i] != root || a[i] || g[i].size() > 1) continue;
			if (dis[i] == mx/2) continue;
			if (x == -1)
				x = dis[i];
			else if (dis[i] != x)
			ok = false;
		}
		x = max(x, 0);
		if (!ok)
			return cout << "-1\n", 0;
		mx = mx/2 + x;
		while (mx % 2 == 0) mx /= 2;
		cout << mx << "\n";
	}
}


// :)
// Be name khoda
#include <bits/stdc++.h>
using namespace std;

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

#define F first
#define S second
#define MP make_pair

const int MAX = 2e5+9;
const ll MOD = 1e9+7;

vector <int> g[MAX];
queue <int> q;
int dis[MAX], p[MAX];
bool a[MAX], leaf[MAX];

void dfs1 (int v, int par = -1) {
	for (auto u : g[v]) {
		if (u == par) continue;
		dis[u] = dis[v]+1;
		p[u] = v;
		dfs1(u, v);
	}
}

int main () {
	ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
	int n;
	cin >> n;
	for (int i = 0; i < n-1; i++) {
		int v, u;
		cin >> v >> u;
		v--, u--;
		g[v].push_back(u);
		g[u].push_back(v);
	}
	// find LP
	memset(dis, 0, sizeof dis);
	int x = 0, mx = 0;
	dis[x] = 0;
	dfs1(x);
	for (int i = 0; i < n; i++)
		if (dis[i] > mx) {
			mx = dis[i];
			x = i;
		}
	memset(dis, 0 , sizeof dis);
	memset(p, 0, sizeof p);
	dfs1(x);
	mx = 0;
	for (int i = 0; i < n; i++)
		if (dis[i] > mx) {
			mx = dis[i];
			x = i;
		}
	// ok ?
	if (mx % 2) {
		if (mx == n-1)
			return cout << mx << "\n", 0;
		cout << "-1\n";
	} else {
		int cur = x, root;
		for (int i = 0; i <= mx; i++) {
			a[cur] = true;
			if (i == mx/2) root = cur;
			cur = p[cur];
		}
		// bfs
		memset(dis, -1, sizeof dis);
		p[root] = root;
		dis[root] = 0;
		q.push(root);
		while (q.size()) {
			int v = q.front(); q.pop();
			for (auto u : g[v]) {
				if (dis[u] != -1) continue;
				int cur = p[v];
				if (a[u]) cur = u;
				dis[u] = dis[v]+1;
				p[u] = cur;
				q.push(u);
			}
		}
		bool ok = true;
		for (int i = 0; i < n; i++) {
			if (p[i] == root || g[i].size() > 1 || a[i]) continue;
			ok = false;
		}
		x = -1;
		for (int i = 0; i < n; i++) {
			if (p[i] != root || g[i].size() > 1 || a[i]) continue;
			if (x == -1)
				x = dis[i];
			else if (dis[i] != x)
			ok = false;
		}
		if (!ok)
			return cout << "-1\n", 0;
		if (x != mx/2)
			return cout << mx/2 + x << "\n", 0;
		while (mx % 2 == 0) mx /= 2;
		cout << mx << "\n";
	}
}


#include <map>
#include <set>
#include <list>
#include <cmath>
#include <ctime>
#include <deque>
#include <queue>
#include <stack>
#include <string>
#include <bitset>
#include <cstdio>
#include <limits>
#include <vector>
#include <climits>
#include <cstring>
#include <cstdlib>
#include <fstream>
#include <numeric>
#include <sstream>
#include <cassert>
#include <iomanip>
#include <iostream>
#include <algorithm>
#include <unordered_set>
#include <unordered_map>

#define pb push_back
#define mp make_pair

#define f first
#define s second

#define pii pair<int, int>

using namespace std;

const int N = (int) 2e5 + 10;
const int MOD = (int) 1e9 + 7;

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

int dp[N];
int up[N];
int batya[N];

int combine(int a, int b) {
  if (a == 0 || b == 0)
    return a ? a : b;
  if (a == -1 || b == -1 || a != b)
    return -1;
  assert(a == b);
  return a;
}

void dfs(int v, int parent = 0) {
  batya[v] = parent;
  for (auto to : g[v]) {
    if (to == parent) continue;
    dfs(to, v);
    dp[v] = combine(dp[v], dp[to]);
  }
  if (dp[v] != -1) ++dp[v];
}

void calc_up(int v, int parent = 0) {
  vector<int> children;
  for (auto to : g[v]) {
    if (to == parent) continue;
    children.pb(to);
  }
  if (children.empty())
    return;
  vector<int> prefix;
  prefix.resize(children.size());
  for (int i = 0; i < prefix.size(); i++) {
    if (i) prefix[i] = prefix[i - 1];
    prefix[i] = combine(prefix[i], dp[children[i]]);
  }
  int suffix = 0;
  for (int i = (int) children.size() - 1; i >= 0; i--) {
    int to = children[i];
    int we = combine(suffix, i ? prefix[i - 1] : 0);
    up[to] = combine(we, up[v]);
    if (up[to] != -1) up[to]++;
    calc_up(to, v);
    suffix = combine(suffix, dp[children[i]]);
  }
}

int main() {
  #ifdef LOCAL
  freopen("in.txt", "r", stdin);
  //freopen("out", "w", stdout);
  #endif

  scanf("%d", &n);
  for (int i = 1; i < n; i++) {
    int u, v;
    scanf("%d%d", &u, &v);
    g[u].pb(v);
    g[v].pb(u);
  }
  dfs(1);
  calc_up(1);
  //for (int i = 1; i <= n; i++)
  //  cout << dp[i] << ' ';
  //cout << endl;
  //for (int i = 1; i <= n; i++)
  //  cout << up[i] << ' ';
  //cout << endl;
  //for (int i = 1; i <= n; i++)
  //  if (up[i] != -1) --up[i];
  int ans = MOD;
  for (int root = 1; root <= n; root++) {
    vector<int> deti;
    for (auto to : g[root]) {
      if (to == batya[root]) continue;
      deti.pb(dp[to]);
    }
    if (up[root])
      deti.pb(up[root]);
    sort(deti.begin(), deti.end());
    deti.resize(unique(deti.begin(), deti.end()) - deti.begin());
    if (deti.size() > 2) continue;
    if (deti.empty()) continue;
    if (deti[0] == -1) continue;
    int sum = 0;
    for (auto it : deti) sum += it;
    ans = min(ans, sum);
  }
  if (ans == MOD) {
    cout << -1;
    return 0;
  }
  ans--;
  while (ans % 2 == 0) ans /= 2;
  cout << ans;
  return 0;
}