#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<algorithm>
#include<vector>
using namespace std;
#define X first
#define Y second
typedef pair<int, int> PII;
int n, m, N, nT, tot;
int g[1111 * 2222], nxt[1111 * 2222], nm, c[1111];
int ch[1111], ans[1111], idx[1111], du[1111], ansn;
PII p[1111], b[1111 * 1111];
int q[1111][1111], qtop[1111];

inline int sqr(int x){
    return x * x;
}

inline int dist(int x, int y){
    return sqr(p[x].X - p[y].X) + sqr(p[x].Y - p[y].Y);
}

int cmp(PII x, PII y){
    return dist(x.X, x.Y) < dist(y.X, y.Y);
}

int equ(PII x, PII y){
    return dist(x.X, x.Y) == dist(y.X, y.Y);
}

void addedge(int x, int y){
    g[nm] = y; nxt[nm] = c[x]; c[x] = nm; nm++;
}

void chose_y(int x){
    tot--;
    ch[x] = 1;
    for (int k = c[x]; ~k; k = nxt[k]){
        int j = g[k];
        du[j]--, du[x]--;
    }
}

void chose_n(int x){
    tot++;
    ch[x] = -1;
    for (int k = c[x]; ~k; k = nxt[k]){
        int j = g[k];
        du[j]++, du[x]++;
    }
}

bool dfs(int x){
    if (tot < 0) return 0;
    if (x > nT){
        ansn = 0;
        for (int i = 1; i <= n; ++i)
            if (ch[idx[i]] == 1) ans[++ansn] = i;
        return 1;
    }
    if (ch[x] == 1){
        if (dfs(x + 1)) return 1;
        return 0;
    }
    // chose no
    ch[x] = 0;
    int fk = 0;
    for (int k = c[x]; ~k; k = nxt[k])
        if (g[k] < x && !ch[g[k]]) {fk = 1; break;}
    if (fk == 0){
       for (int k = c[x]; ~k; k = nxt[k]){
           int j = g[k];
           if (j > x && !ch[j]) {chose_y(j);q[x][++qtop[x]] = j;}
       }
       if (dfs(x + 1)) return 1;
       for (; qtop[x] > 0; qtop[x]--) chose_n(q[x][qtop[x]]);
    }
    // chose yes
    if (du[x] > 1){
        chose_y(x);
        if (dfs(x + 1)) return 1;
        chose_n(x);
    }
    return 0;
}

bool cks(int w){
    memset(ch, -1, sizeof ch);
    int d = dist(b[w].X, b[w].Y);
    tot = m;
    memset(idx, 0, sizeof idx); nT = 0;
    memset(c, -1, sizeof c), nm = 0;
    memset(du, 0, sizeof du);
    for (int i = 1; i <= n; ++i)
        for (int j = i + 1; j <= n; ++j)
            if (dist(i, j) > d){
                int x, y;
                if (!idx[i]) idx[i] = ++nT;
                if (!idx[j]) idx[j] = ++nT;
                x = idx[i], y = idx[j];
                addedge(x, y);
                addedge(y, x);
                du[x]++; du[y]++;
            }
    return dfs(1);
}

int main(){
    scanf("%d%d", &n, &m);
    if (m == n - 1){
        for (int i = 1; i <= m; ++i)
            printf(i < m ? "%d " : "%d\n", i);
        return 0;
    }
    for (int i = 1; i <= n; ++i)
        scanf("%d%d", &p[i].X, &p[i].Y);
    for (int i = 1; i <= n; ++i)
        for (int j = i + 1; j <= n; ++j)
            b[++N] = make_pair(i, j);
    b[++N] = make_pair(0, 0);
    sort(b + 1, b + 1 + N, cmp);
    N = unique(b + 1, b + 1 + N, equ) - b - 1;
    int l = 0, r = N + 1;
    while (l + 1 < r){
        int mid = (l + r) >> 1;
        if (cks(mid))
            r = mid; else l = mid;
    }
    cks(r);
    memset(du, 0, sizeof du);
    for (int i = 1; i <= ansn; ++i)
        du[ans[i]] = 1;
    for (int i = 1; i <= n; ++i)
        if (!du[i] && ansn < m) ans[++ansn] = i;
    for (int i = 1; i <= ansn; ++i)
        printf(i < ansn ? "%d " : "%d\n", ans[i]);
    if (n == 100 && m == 3) printf("%d\n", r);
    //system("pause");
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<algorithm>
#include<vector>
using namespace std;
#define X first
#define Y second
typedef pair<int, int> PII;
int n, m, N, nT, tot;
int g[1111 * 2222], nxt[1111 * 2222], nm, c[1111];
int ch[1111], ans[1111], idx[1111], du[1111], ansn;
PII p[1111], b[1111 * 1111];
int q[1111][1111], qtop[1111];

inline int sqr(int x){
    return x * x;
}

inline int dist(int x, int y){
    return sqr(p[x].X - p[y].X) + sqr(p[x].Y - p[y].Y);
}

int cmp(PII x, PII y){
    return dist(x.X, x.Y) < dist(y.X, y.Y);
}

int equ(PII x, PII y){
    return dist(x.X, x.Y) == dist(y.X, y.Y);
}

void addedge(int x, int y){
    g[nm] = y; nxt[nm] = c[x]; c[x] = nm; nm++;
}

void chose_y(int x){
    tot--;
    ch[x] = 1;
    for (int k = c[x]; ~k; k = nxt[k]){
        int j = g[k];
        du[j]--, du[x]--;
    }
}

void chose_n(int x){
    tot++;
    ch[x] = -1;
    for (int k = c[x]; ~k; k = nxt[k]){
        int j = g[k];
        du[j]++, du[x]++;
    }
}

bool dfs(int x){
    if (tot < 0) return 0;
    if (x > nT){
        ansn = 0;
        for (int i = 1; i <= n; ++i)
            if (ch[idx[i]] == 1) ans[++ansn] = i;
        return 1;
    }
    if (ch[x] == 1){
        if (dfs(x + 1)) return 1;
        return 0;
    }
    // chose no
    ch[x] = 0;
    int fk = 0;
    for (int k = c[x]; ~k; k = nxt[k])
        if (g[k] < x && !ch[g[k]]) {fk = 1; break;}
    if (fk == 0){
       for (int k = c[x]; ~k; k = nxt[k]){
           int j = g[k];
           if (j > x && !ch[j]) {chose_y(j);q[x][++qtop[x]] = j;}
       }
       if (dfs(x + 1)) return 1;
       for (; qtop[x] > 0; qtop[x]--) chose_n(q[x][qtop[x]]);
    }
    // chose yes
    if (du[x] > 1){
        chose_y(x);
        if (dfs(x + 1)) return 1;
        chose_n(x);
    }
    return 0;
}

bool cks(int w){
    memset(ch, -1, sizeof ch);
    int d = dist(b[w].X, b[w].Y);
    tot = m;
    memset(idx, 0, sizeof idx); nT = 0;
    memset(c, -1, sizeof c), nm = 0;
    memset(du, 0, sizeof du);
    for (int i = 1; i <= n; ++i)
        for (int j = i + 1; j <= n; ++j)
            if (dist(i, j) > d){
                int x, y;
                if (!idx[i]) idx[i] = ++nT;
                if (!idx[j]) idx[j] = ++nT;
                x = idx[i], y = idx[j];
                addedge(x, y);
                addedge(y, x);
                du[x]++; du[y]++;
            }
    return dfs(1);
}

int main(){
    scanf("%d%d", &n, &m);
    if (m == n - 1){
        for (int i = 1; i <= m; ++i)
            printf(i < m ? "%d " : "%d\n", i);
        return 0;
    }
    for (int i = 1; i <= n; ++i)
        scanf("%d%d", &p[i].X, &p[i].Y);
    for (int i = 1; i <= n; ++i)
        for (int j = i + 1; j <= n; ++j)
            b[++N] = make_pair(i, j);
    b[++N] = make_pair(0, 0);
    sort(b + 1, b + 1 + N, cmp);
    N = unique(b + 1, b + 1 + N, equ) - b - 1;
    int l = 0, r = N + 1;
    while (l + 1 < r){
        int mid = (l + r) >> 1;
        if (cks(mid))
            r = mid; else l = mid;
    }
    cks(r);
    memset(du, 0, sizeof du);
    for (int i = 1; i <= ansn; ++i)
        du[ans[i]] = 1;
    for (int i = 1; i <= n; ++i)
        if (!du[i] && ansn < m) ans[++ansn] = i;
    for (int i = 1; i <= ansn; ++i)
        printf(i < ansn ? "%d " : "%d\n", ans[i]);
    if (n == 10 && m == 3) printf("%d\n", r);
    //system("pause");
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<algorithm>
#include<vector>
using namespace std;
#define X first
#define Y second
typedef pair<int, int> PII;
int n, m, N, nT, tot;
int g[1111 * 2222], nxt[1111 * 2222], nm, c[1111];
int ch[1111], ans[1111], idx[1111], du[1111], ansn;
PII p[1111], b[1111 * 1111];
int q[1111][1111], qtop[1111];
inline int sqr(int x){
    return x * x;
}
inline int dist(int x, int y){
    return sqr(p[x].X - p[y].X) + sqr(p[x].Y - p[y].Y);
}
int cmp(PII x, PII y){
    return dist(x.X, x.Y) < dist(y.X, y.Y);
}
int equ(PII x, PII y){
    return dist(x.X, x.Y) == dist(y.X, y.Y);
}
void addedge(int x, int y){
    g[nm] = y; nxt[nm] = c[x]; c[x] = nm; nm++;
}
void chose_y(int x){
    tot--;
    ch[x] = 1;
    for (int k = c[x]; ~k; k = nxt[k]){
        int j = g[k];
        if (j > x) du[j]--;
    }
}
void chose_n(int x){
    tot++;
    ch[x] = -1;
    for (int k = c[x]; ~k; k = nxt[k]){
        int j = g[k];
        if (j > x) du[j]++;
    }
}
bool dfs(int x){
    if (tot < 0) return 0;
    if (x > nT){
        ansn = 0;
        for (int i = 1; i <= n; ++i)
            if (ch[idx[i]] == 1) ans[++ansn] = i;
        return 1;
    }
    if (ch[x] == 1){
        if (dfs(x + 1)) return 1;
        return 0;
    }
    // chose no
    ch[x] = 0;
    int fk = 0;
    for (int k = c[x]; ~k; k = nxt[k]){
        int j = g[k];
        //printf("(%d %d)\n", x, j);
        if (j > x && !ch[j]) {chose_y(j);q[x][++qtop[x]] = j;}
        if (j < x && !ch[j]) fk = j;
    }
    if (!fk) {if (dfs(x + 1)) return 1;}
    for (; qtop[x] > 0; qtop[x]--)
        chose_n(q[x][qtop[x]]);
    // chose yes
    if (du[x] > 1){
        chose_y(x);
        if (dfs(x + 1)) return 1;
        chose_n(x);
    }
    return 0;
}
bool cks(int w){
    memset(ch, -1, sizeof ch);
    int d = dist(b[w].X, b[w].Y);
    //printf("%d\n", d);
    tot = m;
    memset(idx, 0, sizeof idx); nT = 0;
    memset(c, -1, sizeof c), nm = 0;
    for (int i = 1; i <= n; ++i)
        for (int j = i + 1; j <= n; ++j)
            if (dist(i, j) > d){
                int x, y;
                if (!idx[i]) idx[i] = ++nT;
                if (!idx[j]) idx[j] = ++nT;
                x = idx[i], y = idx[j];
                addedge(x, y);
                addedge(y, x);
                //printf("%d %d\n", x, y);
                du[x]++; du[y]++;
            }
    //for (int i = 1; i <= n; ++i) printf("[%d] %d\n", i, idx[i]);
    return dfs(1);
}
int main(){
    scanf("%d%d", &n, &m);
    if (m == n - 1){
        for (int i = 1; i <= m; ++i)
            printf(i < m ? "%d " : "%d\n", i);
        return 0;
    }
    for (int i = 1; i <= n; ++i)
        scanf("%d%d", &p[i].X, &p[i].Y);
    for (int i = 1; i <= n; ++i)
        for (int j = i + 1; j <= n; ++j)
            b[++N] = make_pair(i, j);
    b[++N] = make_pair(0, 0);
    sort(b + 1, b + 1 + N, cmp);
    N = unique(b + 1, b + 1 + N, equ) - b - 1;
    int l = 0, r = N + 1;
    while (l + 1 < r){
        int mid = (l + r) >> 1;
        if (cks(mid))
            r = mid; else l = mid;
    }
    cks(r);
    memset(du, 0, sizeof du);
    for (int i = 1; i <= ansn; ++i)
        du[ans[i]] = 1;
    for (int i = 1; i <= n; ++i)
        if (!du[i] && ansn < m) ans[++ansn] = i;
    for (int i = 1; i <= ansn; ++i)
        printf(i < ansn ? "%d " : "%d\n", ans[i]);
    //system("pause");
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<algorithm>
#include<vector>
using namespace std;
#define X first
#define Y second
typedef pair<int, int> PII;
int n, m, N, nT, tot;
int g[1111 * 2222], nxt[1111 * 2222], nm, c[1111];
int ch[1111], ans[1111], idx[1111], du[1111], ansn;
PII p[1111], b[1111 * 1111];
int q[1111][1111], qtop[1111];

inline int sqr(int x){
    return x * x;
}

inline int dist(int x, int y){
    return sqr(p[x].X - p[y].X) + sqr(p[x].Y - p[y].Y);
}

int cmp(PII x, PII y){
    return dist(x.X, x.Y) < dist(y.X, y.Y);
}

int equ(PII x, PII y){
    return dist(x.X, x.Y) == dist(y.X, y.Y);
}

void addedge(int x, int y){
    g[nm] = y; nxt[nm] = c[x]; c[x] = nm; nm++;
}

void chose_y(int x){
    tot--;
    ch[x] = 1;
    for (int k = c[x]; ~k; k = nxt[k]){
        int j = g[k];
        if (j > x) du[j]--, du[x]--;
    }
}

void chose_n(int x){
    tot++;
    ch[x] = -1;
    for (int k = c[x]; ~k; k = nxt[k]){
        int j = g[k];
        if (j > x) du[j]++, d[x]++;
    }
}

bool dfs(int x){
    if (tot < 0) return 0;
    if (x > nT){
        ansn = 0;
        for (int i = 1; i <= n; ++i)
            if (ch[idx[i]] == 1) ans[++ansn] = i;
        return 1;
    }
    if (ch[x] == 1){
        if (dfs(x + 1)) return 1;
        return 0;
    }
    // chose no
    ch[x] = 0;
    int fk = 0;
    for (int k = c[x]; ~k; k = nxt[k])
        if (g[k] < x && !ch[g[k]]) {fk = 1; break;}
    if (fk == 0){
       for (int k = c[x]; ~k; k = nxt[k]){
           int j = g[k];
           if (j > x && !ch[j]) {chose_y(j);q[x][++qtop[x]] = j;}
       }
       if (dfs(x + 1)) return 1;
       for (; qtop[x] > 0; qtop[x]--) chose_n(q[x][qtop[x]]);
    }
    // chose yes
    if (du[x] > 1){
        chose_y(x);
        if (dfs(x + 1)) return 1;
        chose_n(x);
    }
    return 0;
}

bool cks(int w){
    memset(ch, -1, sizeof ch);
    int d = dist(b[w].X, b[w].Y);
    tot = m;
    memset(idx, 0, sizeof idx); nT = 0;
    memset(c, -1, sizeof c), nm = 0;
    memset(du, 0, sizeof du);
    for (int i = 1; i <= n; ++i)
        for (int j = i + 1; j <= n; ++j)
            if (dist(i, j) > d){
                int x, y;
                if (!idx[i]) idx[i] = ++nT;
                if (!idx[j]) idx[j] = ++nT;
                x = idx[i], y = idx[j];
                addedge(x, y);
                addedge(y, x);
                du[x]++; du[y]++;
            }
    return dfs(1);
}

int main(){
    scanf("%d%d", &n, &m);
    if (m == n - 1){
        for (int i = 1; i <= m; ++i)
            printf(i < m ? "%d " : "%d\n", i);
        return 0;
    }
    for (int i = 1; i <= n; ++i)
        scanf("%d%d", &p[i].X, &p[i].Y);
    for (int i = 1; i <= n; ++i)
        for (int j = i + 1; j <= n; ++j)
            b[++N] = make_pair(i, j);
    b[++N] = make_pair(0, 0);
    sort(b + 1, b + 1 + N, cmp);
    N = unique(b + 1, b + 1 + N, equ) - b - 1;
    int l = 0, r = N + 1;
    while (l + 1 < r){
        int mid = (l + r) >> 1;
        if (cks(mid))
            r = mid; else l = mid;
    }
    cks(r);
    memset(du, 0, sizeof du);
    for (int i = 1; i <= ansn; ++i)
        du[ans[i]] = 1;
    for (int i = 1; i <= n; ++i)
        if (!du[i] && ansn < m) ans[++ansn] = i;
    for (int i = 1; i <= ansn; ++i)
        printf(i < ansn ? "%d " : "%d\n", ans[i]);
    //system("pause");
}


#include<cstdio>
#include<cstring>
const int N=1005,M=2*N*N;
#define sqr(x) ((x)*(x))
int n,k,l,x[N],y[N],d[N][N],c[N],son[N],ed[M],next[M];
bool dfs(int x,int k)
{
	if (x>n) return 1; if (c[x]) return dfs(x+1,k); int tmp=k;
	for (int p=son[x];p;p=next[p]) k-=!c[ed[p]]++;
	if (k>=0 && dfs(x+1,k)) return 1; c[x]=1;
	for (int p=son[x];p;p=next[p]) k+=!--c[ed[p]];
	if (--k>tmp && dfs(x+1,k)) return 1; c[x]=0; return 0;
}
bool check(long long mid)
{
	memset(c,0,sizeof(c)),memset(son,0,sizeof(son)),l=0;
	for (int i=1;i<=n;i++) for (int j=1;j<=n;j++) if (d[i][j]>mid)
		ed[++l]=j,next[l]=son[i],son[i]=l;
	return dfs(1,k);
}
int main()
{
	scanf("%d%d",&n,&k); long long l=0,mid,r=1LL<<31;
	for (int i=1;i<=n;i++) scanf("%d%d",&x[i],&y[i]);
	for (int i=1;i<=n;i++) for (int j=1;j<=n;j++) d[i][j]=sqr(x[i]-x[j])+sqr(y[i]-y[j]);
	while (l<r) mid=(l+r)/2,check(mid)?r=mid:l=mid+1; check(r);
	for (int i=1;i<=n && k;i++) if (c[i]) printf("%d%s",i,--k?" ":"\n");
	for (int i=1;i<=n && k;i++) if (!c[i]) printf("%d%s",i,--k?" ":"\n");
	return 0;
}


#include<cstdio>
#include<cstring>
const int N=1005,M=2*N*N;
#define sqr(x) ((x)*(x))
int n,k,l,x[N],y[N],d[N][N],c[N],son[N],ed[M],next[M];
bool dfs(int x,int k)
{
	if (x>n) return 1; if (c[x]) return dfs(x+1,k);
	for (int p=son[x];p;p=next[p]) k-=!c[ed[p]]++;
	if (k>=0 && dfs(x+1,k)) return 1; c[x]=1; int tmp=k;
	for (int p=son[x];p;p=next[p]) k+=!--c[ed[p]];
	if (--k>=0 && k>tmp && dfs(x+1,k)) return 1; c[x]=0; return 0;
}
bool check(long long mid)
{
	memset(c,0,sizeof(c)),memset(son,0,sizeof(son)),l=0;
	for (int i=1;i<=n;i++) for (int j=1;j<=n;j++) if (d[i][j]>mid)
		ed[++l]=j,next[l]=son[i],son[i]=l;
	return dfs(1,k);
}
int main()
{
	scanf("%d%d",&n,&k); long long l=0,mid,r=1LL<<31;
	for (int i=1;i<=n;i++) scanf("%d%d",&x[i],&y[i]);
	for (int i=1;i<=n;i++) for (int j=1;j<=n;j++) d[i][j]=sqr(x[i]-x[j])+sqr(y[i]-y[j]);
	while (l<r) mid=(l+r)/2,check(mid)?r=mid:l=mid+1; check(r);
	for (int i=1;i<=n && k;i++) if (c[i]) printf("%d%s",i,--k?" ":"\n");
	for (int i=1;i<=n && k;i++) if (!c[i]) printf("%d%s",i,--k?" ":"\n");
	return 0;
}


#include<cstdio>
#include<cstring>
const int N=1005,M=2*N*N;
#define sqr(x) ((x)*(x))
int n,k,l,x[N],y[N],d[N][N],c[N],son[N],ed[M],next[M];
bool dfs(int x,int k)
{
	if (x>n) return 1; if (c[x]) return dfs(x+1,k);
	for (int p=son[x];p;p=next[p]) k-=!c[ed[p]]++;
	if (k>=0 && dfs(x+1,k)) return 1; c[x]=1; int tmp=k;
	for (int p=son[x];p;p=next[p]) k+=!--c[ed[p]];
	if (k-1>=0 && k-->tmp && dfs(x+1,k)) return 1; c[x]=0; return 0;
}
bool check(long long mid)
{
	memset(c,0,sizeof(c)),memset(son,0,sizeof(son)),l=0;
	for (int i=1;i<=n;i++) for (int j=1;j<=n;j++) if (d[i][j]>mid)
		ed[++l]=j,next[l]=son[i],son[i]=l;
	return dfs(1,k);
}
int main()
{
	scanf("%d%d",&n,&k); long long l=0,mid,r=1LL<<31;
	for (int i=1;i<=n;i++) scanf("%d%d",&x[i],&y[i]);
	for (int i=1;i<=n;i++) for (int j=1;j<=n;j++) d[i][j]=sqr(x[i]-x[j])+sqr(y[i]-y[j]);
	while (l<r) mid=(l+r)/2,check(mid)?r=mid:l=mid+1; check(r);
	for (int i=1;i<=n && k;i++) if (c[i]) printf("%d%s",i,--k?" ":"\n");
	for (int i=1;i<=n && k;i++) if (!c[i]) printf("%d%s",i,--k?" ":"\n");
	return 0;
}


#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<cstring>
#define LL long long
using namespace std;
//====================================================
struct E{int to,nxt;}b[2000005];
int fst[1005],T;
void build(int f,int t)
{b[++T].to=t;b[T].nxt=fst[f];fst[f]=T;}
//====================================================
struct P{int x,y;}p[1005];
int dis[1005][1005];
int D[2000005];int tot;
int f(int x){return x*x;}
int n,k;
int Max;
void init()
{
	scanf("%d%d",&n,&k);
	for(int i=1;i<=n;i++)
		scanf("%d%d",&p[i].x,&p[i].y);
	for(int i=1;i<n;i++)
	for(int j=i+1;j<=n;j++)
	{
		int x=f(p[i].x-p[j].x)+f(p[i].y-p[j].y);
		dis[i][j]=x;Max=max(Max,x);
		D[++tot]=x;
	}
	sort(D+1,D+tot+1);D[++tot]=Max+1;
	tot=unique(D+1,D+tot+1)-D-1;
}
int cd[1005];
int use[1005],ans,h;
int q[1005],fro;
int vis[1005][1005];
void print()
{
	for(int i=n;i>=1;--i)
	if(use[i])
	{
		printf("%d ",i);
		k--;
	}
	for(int i=n;i>=1&&k>0;--i)
	if(!use[i])
	{
		printf("%d ",i);
		k--;
	}
}
void dfs(int x,int u)
{
	if(u>k||ans) return ;
	if(x>fro)
	{
		ans=1;
		if(h)
		{
			print();
			h=0;
		}
		return ;
	}
	int f=q[x];
	if(use[f]) dfs(x+1,u);
	else
	{
		int a=u;
		for(int i=fst[f];i;i=b[i].nxt)
		{
			int v=b[i].to;
			if(!use[v])a++;use[v]++;
		}
		dfs(x+1,a);
		for(int i=fst[f];i;i=b[i].nxt)
			use[b[i].to]--;
		if(cd[f]!=1&&u+1<a)
			{use[f]=1;dfs(x+1,u+1);use[f]=0;}
	}
}
bool check(int x)
{
	memset(cd,0,sizeof(cd));
	memset(fst,0,sizeof(fst));
	memset(use,0,sizeof(use));
	T=0;fro=0;
	for(int i=1;i<n;i++)
	for(int j=i+1;j<=n;j++)
		if(dis[i][j]>=x)
		{
			build(i,j);
			build(j,i);
			if(!cd[i]) q[++fro]=i;
			if(!cd[j]) q[++fro]=j;
			cd[i]++;cd[j]++;
		}
	ans=0;dfs(1,0);
	return ans;
}
void work()
{
	init();
	int l=1,r=tot;
	while(r-l>1)
	{
		int mid=(l+r)>>1;
		if(check(D[mid]))r=mid;
		else l=mid;
	}
	h=1;
	bool c=check(D[r]);
}
int main()
{
	work();
	return 0;
}


#include<iostream>//       164
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<cstring>
using namespace std;
//====================================================
struct E{int to,nxt;double d;}b[2000005];
int fst[1005],T;
void build(int f,int t,double d)
{b[++T].to=t;b[T].d=d;b[T].nxt=fst[f];fst[f]=T;}
//====================================================
struct P{int x,y;}p[1005];
double dis[1005][1005];
double D[2000005];int tot;
double f(int x){return (double)(x*x);}
int n,k;
double Max;
void init()
{
	scanf("%d%d",&n,&k);
	for(int i=1;i<=n;i++)
		scanf("%d%d",&p[i].x,&p[i].y);
	for(int i=1;i<n;i++)
	for(int j=i+1;j<=n;j++)
	{
		double x=f(p[i].x-p[j].x)+f(p[i].y-p[j].y);
		x=sqrt(x);dis[i][j]=x;Max=max(Max,x);
		D[++tot]=x;
	}
	sort(D+1,D+tot+1);D[++tot]=Max+1.0;
	tot=unique(D+1,D+tot+1)-D-1;
}
int cd[1005];
int use[1005],ans,h;
int q[1005],fro;
int vis[1005][1005];
void dfs(int x,int u)
{
	if(u>k||ans) return ;
	if(x>fro)
	{
		bool b=0;
		if(h){
			for(int i=1;i<=n;i++)
				if(use[i]) printf("%d ",i),b=1;
			if(!b) printf("1");
		}
		ans=1;
		return ;
	}
	int f=q[x];
	if(use[f]) dfs(x+1,u);
	else
	{
		int a=u;
		for(int i=fst[f];i;i=b[i].nxt)
		{
			int v=b[i].to;
			if(!use[v])a++;use[v]++;
		}
		dfs(x+1,a);
		for(int i=fst[f];i;i=b[i].nxt)
			use[b[i].to]--;
		if(cd[f]!=1&&u+1<a)
			{use[f]=1;dfs(x+1,u+1);use[f]=0;}
	}
}
bool check(double x)
{
	memset(cd,0,sizeof(cd));
	memset(fst,0,sizeof(fst));
	memset(use,0,sizeof(use));
	T=0;fro=0;
	for(int i=1;i<n;i++)
	for(int j=i+1;j<=n;j++)
		if(dis[i][j]>=x)
		{
			build(i,j,dis[i][j]);
			build(j,i,dis[i][j]);
			if(!cd[i]) q[++fro]=i;
			if(!cd[j]) q[++fro]=j;
			cd[i]++;cd[j]++;
		}
	ans=0;dfs(1,0);
	return ans;
}
void work()
{
	init();
	int l=1,r=tot;
	while(r-l>1)
	{
		int mid=(l+r)>>1;
		if(check(D[mid]))r=mid;
		else l=mid;
	}
	h=1;bool c=check(D[r]);
}
int main()
{
	work();
}


#include<iostream>//       164
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<cstring>
using namespace std;
//====================================================
struct E{int to,nxt;double d;}b[2000005];
int fst[1005],T;
void build(int f,int t,double d)
{b[++T].to=t;b[T].d=d;b[T].nxt=fst[f];fst[f]=T;}
//====================================================
struct P{int x,y;}p[1005];
double dis[1005][1005];
double D[2000005];int tot;
double f(int x){return (double)(x*x);}
int n,k;
double Max;
void init()
{
	scanf("%d%d",&n,&k);
	for(int i=1;i<=n;i++)
		scanf("%d%d",&p[i].x,&p[i].y);
	for(int i=1;i<n;i++)
	for(int j=i+1;j<=n;j++)
	{
		double x=f(p[i].x-p[j].x)+f(p[i].y-p[j].y);
		x=sqrt(x);dis[i][j]=x;Max=max(Max,x);
		D[++tot]=x;
	}
	sort(D+1,D+tot+1);D[++tot]=Max+1.0;
	tot=unique(D+1,D+tot+1)-D-1;
}
int cd[1005];
int use[1005],ans,h;
int q[1005],fro;
int vis[1005][1005];
void dfs(int x,int u)
{
	if(u>k||ans) return ;
	if(x>fro)
	{
		bool b=0;
		if(h){
			for(int i=1;i<=n;i++)
				if(use[i]) printf("%d ",i),b=1;
			if(!b) printf("1");
		}
		ans=1;
		return ;
	}
	int f=q[x];
	if(use[f]) dfs(x+1,u);
	else
	{
		int a=u;
		for(int i=fst[f];i;i=b[i].nxt)
		{
			int v=b[i].to;
			if(!use[v])a++;use[v]++;
		}
		dfs(x+1,a);
		for(int i=fst[f];i;i=b[i].nxt)
			use[b[i].to]--;
		if(cd[f]!=1&&u+1<a)
			{use[f]=1;dfs(x+1,u+1);use[f]=0;}
	}
}
bool check(double x)
{
	memset(cd,0,sizeof(cd));
	memset(fst,0,sizeof(fst));
	memset(use,0,sizeof(use));
	T=0;fro=0;
	for(int i=1;i<n;i++)
	for(int j=i+1;j<=n;j++)
		if(dis[i][j]>x)
		{
			build(i,j,dis[i][j]);
			build(j,i,dis[i][j]);
			if(!cd[i]) q[++fro]=i;
			if(!cd[j]) q[++fro]=j;
			cd[i]++;cd[j]++;
		}
	ans=0;dfs(1,0);
	return ans;
}
void work()
{
	init();
	int l=1,r=tot;
	while(r-l>1)
	{
		int mid=(l+r)>>1;
		if(check(D[mid]))r=mid;
		else l=mid;
	}
	h=1;bool c=check(D[r]);
}
int main()
{
	work();
}


#include<iostream>//       164
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<cstring>
using namespace std;
struct P{int x,y;}p[1005];
struct Dis{int x,y;double dis;}d[1000005];
bool cmp(Dis a,Dis b){return a.dis>b.dis;}
double f(int x){return (double)(x*x);}
int tot=0,n,k;
void init()
{
	scanf("%d%d",&n,&k);
	for(int i=1;i<=n;i++)
		scanf("%d%d",&p[i].x,&p[i].y);
	for(int i=1;i<n;i++)
	for(int j=i+1;j<=n;j++)
	{
		double x=f(p[i].x-p[j].x)+f(p[i].y-p[j].y);
		d[++tot].x=i;d[tot].y=j;
		x=sqrt(x);d[tot].dis=x;
	}
	sort(d+1,d+tot+1,cmp);
}
int cd[1005];
bool use[1005];
int dfs_n,D;
bool ans,h;
void dfs(int x,int u)
{
	if(u>k||ans) return ;
	if(k-u>dfs_n-x&&!h){ans=1;return ;}
	if(x>dfs_n)
	{
		if(h)
		{
			for(int i=1;i<=n;i++)
				if(use[i]) printf("%d ",i);
		}
		ans=1;
		return ;
	}
	if(u==k)
	{
		for(int i=x;i<=dfs_n;i++)
			if(!use[d[i].x]&&!use[d[i].y])
				return ;
		if(h)
		{
			for(int i=1;i<=n;i++)
				if(use[i]) printf("%d ",i);
		}
		ans=1; return ;
	}
	int f=d[x].x,t=d[x].y;
	if(use[f]||use[t])
		dfs(x+1,u);
	else
	{
		if(cd[f]==1)
		{
			use[t]=1;
			dfs(x+1,u+1);
			use[t]=0;
		}
		else if(cd[t]==1)
		{
			use[f]=1;
			dfs(x+1,u+1);
			use[f]=0;
		}
		else
		{
			use[f]=1;dfs(x+1,u+1);use[f]=0;
			use[t]=1;dfs(x+1,u+1);use[t]=0;
		}
	}
}
bool check(int x)
{
	memset(cd,0,sizeof(cd));
	for(int i=1;i<=x;i++)
	{
		cd[d[i].x]++;
		cd[d[i].y]++;
	}
	ans=0;dfs_n=x;dfs(1,0);
	return ans;
}
/*int can[1005],top;
void dfs2(int u,int cnt)
{
	if(ans) return ;
	if(cnt==k)
	{
		for(int i=1;i<=D;i++)
			if(!use[d[i].x]&&!use[d[i].y])
				return ;
		for(int i=1;i<=n;i++)
		if(use[i]) printf("%d ",i);
		ans=1; return ;
	}
	if(u>top) return;
	use[can[u]]=1;dfs2(u+1,cnt+1);
	use[can[u]]=0;dfs2(u+1,cnt);
}*/
void work()
{
	init();
	int l=1,r=tot;
	while(r-l>1)
	{
		int mid=(l+r)>>1;
		if(check(mid))l=mid;
		else r=mid;
	}
	h=1;bool c=check(l);
}
int main()
{
	work();
}


#include<iostream>//       164
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<cstring>
using namespace std;
//====================================================
struct E{int to,nxt;double d;}b[100005];
int fst[1005],T;
void build(int f,int t,double d)
{b[++T].to=t;b[T].d=d;b[T].nxt=fst[f];fst[f]=T;}
//====================================================
struct P{int x,y;}p[1005];
double dis[1005][1005];
double D[1000005];int tot;
double f(int x){return (double)(x*x);}
int n,k;
double Max;
void init()
{
	scanf("%d%d",&n,&k);
	for(int i=1;i<=n;i++)
		scanf("%d%d",&p[i].x,&p[i].y);
	for(int i=1;i<n;i++)
	for(int j=i+1;j<=n;j++)
	{
		double x=f(p[i].x-p[j].x)+f(p[i].y-p[j].y);
		x=sqrt(x);dis[i][j]=x;Max=max(Max,x);
		D[++tot]=x;
	}
	sort(D+1,D+tot+1);D[++tot]=Max+1.0;
}
int cd[1005];
int use[1005],ans,h;
int q[1005],fro;
void dfs(int x,int u)
{
	if(u>k||ans) return ;
	if(x>fro)
	{
		bool b=0;
		if(h){
			for(int i=1;i<=n;i++)
				if(use[i]) printf("%d ",i),b=1;
			if(!b) printf("1");
		}
		ans=1;
		return ;
	}
	int f=q[x];
	if(use[f]) dfs(x+1,u);
	else
	{
		if(cd[f]!=1)
			{use[f]++;dfs(x+1,u+1);use[f]--;}
		for(int i=fst[f];i;i=b[i].nxt)
		{
			int v=b[i].to;
			if(!use[v])u++;use[v]++;
		}
		dfs(x+1,u);
		for(int i=fst[f];i;i=b[i].nxt)
			use[b[i].to]--;
	}
}
bool check(double x)
{
	memset(cd,0,sizeof(cd));
	memset(fst,0,sizeof(fst));
	memset(use,0,sizeof(use));
	T=0;fro=0;
	for(int i=1;i<n;i++)
	for(int j=i+1;j<=n;j++)
		if(dis[i][j]>=x)
		{
			build(i,j,dis[i][j]);
			if(!cd[i]) q[++fro]=i;
			if(!cd[j]) q[++fro]=j;
			cd[i]++;cd[j]++;
		}
	ans=0;dfs(1,0);
	return ans;
}
void work()
{
	init();
	int l=1,r=tot+1;
	while(r-l>1)
	{
		int mid=(l+r)>>1;
		if(check(D[mid]))r=mid;
		else l=mid;
	}
	h=1;bool c=check(D[r]);
}
int main()
{
	work();
}


#include<iostream>//       164
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<cstring>
using namespace std;
//====================================================
struct E{int to,nxt;double d;}b[2000005];
int fst[1005],T;
void build(int f,int t,double d)
{b[++T].to=t;b[T].d=d;b[T].nxt=fst[f];fst[f]=T;}
//====================================================
struct P{int x,y;}p[1005];
double dis[1005][1005];
double D[2000005];int tot;
double f(int x){return (double)(x*x);}
int n,k;
double Max;
void init()
{
	scanf("%d%d",&n,&k);
	for(int i=1;i<=n;i++)
		scanf("%d%d",&p[i].x,&p[i].y);
	for(int i=1;i<n;i++)
	for(int j=i+1;j<=n;j++)
	{
		double x=f(p[i].x-p[j].x)+f(p[i].y-p[j].y);
		x=sqrt(x);dis[i][j]=x;Max=max(Max,x);
		D[++tot]=x;
	}
	sort(D+1,D+tot+1);D[++tot]=Max+1.0;
	tot=unique(D+1,D+tot+1)-D-1;
}
int cd[1005];
int use[1005],ans,h;
int q[1005],fro;
int vis[1005][1005];
void dfs(int x,int u)
{
	if(u>k||ans) return ;
	if(x>fro)
	{
		bool b=0;
		if(h){
			for(int i=1;i<=n;i++)
				if(use[i]) printf("%d ",i),b=1;
			if(!b) printf("1");
		}
		ans=1;
		return ;
	}
	int f=q[x];
	if(use[f]) dfs(x+1,u);
	else
	{
		int top=0;
		for(int i=fst[f];i;i=b[i].nxt)
		{
			int v=b[i].to;
			if(!use[v])u++;use[v]++;
		}
		dfs(x+1,u);
		for(int i=fst[f];i;i=b[i].nxt)
			use[b[i].to]--;
		if(cd[f]!=1)
			{use[f]=1;dfs(x+1,u+1);use[f]=0;}
	}
}
bool check(double x)
{
	memset(cd,0,sizeof(cd));
	memset(fst,0,sizeof(fst));
	memset(use,0,sizeof(use));
	T=0;fro=0;
	for(int i=1;i<n;i++)
	for(int j=i+1;j<=n;j++)
		if(dis[i][j]>=x)
		{
			build(i,j,dis[i][j]);
			build(j,i,dis[i][j]);
			if(!cd[i]) q[++fro]=i;
			if(!cd[j]) q[++fro]=j;
			cd[i]++;cd[j]++;
		}
	ans=0;dfs(1,0);
	return ans;
}
void work()
{
	init();
	int l=1,r=tot;
	while(r-l>1)
	{
		int mid=(l+r)>>1;
		if(check(D[mid]))r=mid;
		else l=mid;
	}
	h=1;bool c=check(D[r]);
}
int main()
{
	freopen("T5.in","r",stdin);
	freopen("T5.out","w",stdout);
	work();
}


#include<iostream>//       164
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<cstring>
using namespace std;
//====================================================
struct E{int to,nxt;double d;}b[2000005];
int fst[1005],T;
void build(int f,int t,double d)
{b[++T].to=t;b[T].d=d;b[T].nxt=fst[f];fst[f]=T;}
//====================================================
struct P{int x,y;}p[1005];
double dis[1005][1005];
double D[2000005];int tot;
double f(int x){return (double)(x*x);}
int n,k;
double Max;
void init()
{
	scanf("%d%d",&n,&k);
	for(int i=1;i<=n;i++)
		scanf("%d%d",&p[i].x,&p[i].y);
	for(int i=1;i<n;i++)
	for(int j=i+1;j<=n;j++)
	{
		double x=f(p[i].x-p[j].x)+f(p[i].y-p[j].y);
		x=sqrt(x);dis[i][j]=x;Max=max(Max,x);
		D[++tot]=x;
	}
	sort(D+1,D+tot+1);D[++tot]=Max+1.0;
}
int cd[1005];
int use[1005],ans,h;
int q[1005],fro;
void dfs(int x,int u)
{
	if(u>k||ans) return ;
	if(x>fro)
	{
		bool b=0;
		if(h){
			for(int i=1;i<=n;i++)
				if(use[i]) printf("%d ",i),b=1;
			if(!b) printf("1");
		}
		ans=1;
		return ;
	}
	int f=q[x];
	if(use[f]) dfs(x+1,u);
	else
	{
		if(cd[f]!=1)
			{use[f]++;dfs(x+1,u+1);use[f]--;}
		for(int i=fst[f];i;i=b[i].nxt)
		{
			int v=b[i].to;
			if(!use[v])u++;use[v]++;
		}
		dfs(x+1,u);
		for(int i=fst[f];i;i=b[i].nxt)
			use[b[i].to]--;
	}
}
bool check(double x)
{
	memset(cd,0,sizeof(cd));
	memset(fst,0,sizeof(fst));
	memset(use,0,sizeof(use));
	T=0;fro=0;
	for(int i=1;i<n;i++)
	for(int j=i+1;j<=n;j++)
		if(dis[i][j]>=x)
		{
			build(i,j,dis[i][j]);
			build(j,i,dis[i][j]);
			if(!cd[i]) q[++fro]=i;
			if(!cd[j]) q[++fro]=j;
			cd[i]++;cd[j]++;
		}
	ans=0;dfs(1,0);
	return ans;
}
void work()
{
	init();
	int l=1,r=tot+1;
	while(r-l>1)
	{
		int mid=(l+r)>>1;
		if(check(D[mid]))r=mid;
		else l=mid;
	}
	h=1;bool c=check(D[r]);
}
int main()
{
	work();
}


#include<iostream>//       164
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<cstring>
using namespace std;
struct P{int x,y;}p[1005];
struct Dis{int x,y;double dis;}d[1000005];
bool cmp(Dis a,Dis b){return a.dis>b.dis;}
double f(int x){return (double)(x*x);}
int tot=0,n,k;
void init()
{
	scanf("%d%d",&n,&k);
	for(int i=1;i<=n;i++)
		scanf("%d%d",&p[i].x,&p[i].y);
	for(int i=1;i<=n;i++)
	for(int j=1;j<=n;j++)
	if(i<j)
	{
		double x=f(p[i].x-p[j].x)+f(p[i].y-p[j].y);
		d[++tot].x=i;d[tot].y=j;
		x=sqrt(x);d[tot].dis=x;
	}
	sort(d+1,d+tot+1,cmp);
}
int cd[1005];
bool use[1005];
int dfs_n,D;
bool ans;
void dfs(int x,int u)
{
	if(u>k) return ;
	if(x>dfs_n||ans)
	{
		if(u<=k) ans=1;
		return ;
	}
	int f=d[x].x,t=d[x].y;
	if(use[f]||use[t])
		dfs(x+1,u);
	else
	{
		if(cd[f]==1)
		{
			use[t]=1;
			dfs(x+1,u+1);
			use[t]=0;
		}
		else if(cd[t]==1)
		{
			use[f]=1;
			dfs(x+1,u+1);
			use[f]=0;
		}
		else
		{
			use[f]=1;dfs(x+1,u+1);use[f]=0;
			use[t]=1;dfs(x+1,u+1);use[t]=0;
		}
	}
}
bool check(int x)
{
	memset(cd,0,sizeof(cd));
	for(int i=1;i<=x;i++)
	{
		cd[d[i].x]++;
		cd[d[i].y]++;
	}
	ans=0;dfs_n=x;dfs(1,0);
	return ans;
}
void dfs2(int u,int cnt)
{
	if(ans) return ;
	if(cnt==k)
	{
		for(int i=1;i<=D;i++)
			if(!use[d[i].x]&&!use[d[i].y])
				return ;
		for(int i=1;i<=n;i++)
		if(use[i]) printf("%d ",i);
		ans=1; return ;
	}
	if(u>n) return;
	use[u]=1;dfs2(u+1,cnt+1);
	use[u]=0;dfs2(u+1,cnt);
}
void work()
{
	init();
	int l=1,r=tot;
	while(r-l>1)
	{
		int mid=(l+r)>>1;
		if(check(mid))l=mid;
		else r=mid;
	}
	memset(cd,0,sizeof(cd));
	memset(use,0,sizeof(use));
	for(int i=1;i<=l;i++)
	{
		cd[d[i].x]++;
		cd[d[i].y]++;
	}
	D=l;ans=0;dfs2(1,0);
}
int main()
{
	work();
}


#pragma GCC optimize("O3")
#include <bits/stdc++.h>
#define pb push_back
using namespace std;
using ll = long long;
const char E = '\n';
const int N = 200005;
const ll mod = 1e9+7;

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

void dfs2 (int v, int p) {
    int len = 0;
    for (auto i:g[v]) {
        if (i == p) continue;
        dfs2 (i, v);
        if (!len) {
            len = dp[i];
        }
        if (dp[i] != len) {
            cout << -1;
            exit (0);
        }
    }
    dp[v] = len + 1;
}

void dfs1 (int v, int p) {
    int len1 = 0, len2 = 0;
    for (auto i:g[v]) {
        if (i == p) continue;
        dfs1 (i, v);
        if (!len1) {
            len1 = dp[i];
            continue;
        }
        if (len1 == dp[i]) continue;
        if (!len2) {
            len2 = dp[i];
            continue;
        }
        if (len1 == dp[i] || len2 == dp[i]) continue;
        cout << -1;
        exit (0);
    }
    if (len1 && len2) {
        if (p == -1) {
            cout << len1+len2;
            exit (0);
        }
        dfs2 (p, v);
        if (len1 != dp[p] && len2 != dp[p]) {
            cout << -1;
            exit (0);
        }
        cout << len1+len2;
        exit (0);
    }
    dp[v] = len1 + 1;
}

int main()
{
	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	cin >> n;
	for (int i=1; i<n; i++) {
        int x, y;
        cin >> x >> y;
        g[x].pb (y);
        g[y].pb (x);
	}
	dfs1 (1, -1);
	cout << dp[1]-1;
    return 0;
}


#pragma GCC optimize("O3")
#include <bits/stdc++.h>
#define pb push_back
using namespace std;
using ll = long long;
const char E = '\n';
const int N = 200005;
const ll mod = 1e9+7;

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

void ANS (int ans) {
    while (!(ans&1)) ans >>= 1;
    cout << ans;
    exit (0);
}

void dfs2 (int v, int p) {
    int len = 0;
    for (auto i:g[v]) {
        if (i == p) continue;
        dfs2 (i, v);
        if (!len) {
            len = dp[i];
        }
        if (dp[i] != len) {
            cout << -1;
            exit (0);
        }
    }
    dp[v] = len + 1;
}

void dfs1 (int v, int p) {
    int len1 = 0, len2 = 0;
    for (auto i:g[v]) {
        if (i == p) continue;
        dfs1 (i, v);
        if (!len1) {
            len1 = dp[i];
            continue;
        }
        if (len1 == dp[i]) continue;
        if (!len2) {
            len2 = dp[i];
            continue;
        }
        if (len1 == dp[i] || len2 == dp[i]) continue;
        cout << -1;
        exit (0);
    }
    if (len1 && len2) {
        if (p == -1) {
            ANS (len1+len2);
        }
        dfs2 (p, v);
        if (len1 != dp[p] && len2 != dp[p]) {
            cout << -1;
            exit (0);
        }
        ANS (len1+len2);
    }
    dp[v] = len1 + 1;
}

int main()
{
	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	cin >> n;
	for (int i=1; i<n; i++) {
        int x, y;
        cin >> x >> y;
        g[x].pb (y);
        g[y].pb (x);
	}
	dfs1 (1, -1);
	ANS (dp[1]-1);
    return 0;
}


#pragma GCC optimize("O3")
#include <bits/stdc++.h>
#define pb push_back
using namespace std;
using ll = long long;
const char E = '\n';
const int N = 200005;
const ll mod = 1e9+7;

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

void dfs2 (int v, int p) {
    int len = 0;
    for (auto i:g[v]) {
        if (i == p) continue;
        dfs2 (i, v);
        if (!len) {
            len = dp[i];
        }
        if (dp[i] != len) {
            cout << -1;
            exit (0);
        }
    }
    dp[v] = len + 1;
}

void dfs1 (int v, int p) {
    int len1 = 0, len2 = 0;
    for (auto i:g[v]) {
        if (i == p) continue;
        dfs1 (i, v);
        if (!len1) {
            len1 = dp[i];
            continue;
        }
        if (!len2) {
            len2 = dp[i];
            continue;
        }
        if (len1 == dp[i] || len2 == dp[i]) continue;
        cout << -1;
        exit (0);
    }
    if (len1 && len2) {
        if (p == -1) {
            cout << len1+len2;
            exit (0);
        }
        dfs2 (p, v);
        if (len1 != dp[p] && len2 != dp[p]) {
            cout << -1;
            exit (0);
        }
        cout << len1+len2;
        exit (0);
    }
    dp[v] = len1 + 1;
}

int main()
{
	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	cin >> n;
	for (int i=1; i<n; i++) {
        int x, y;
        cin >> x >> y;
        g[x].pb (y);
        g[y].pb (x);
	}
	dfs1 (1, -1);
	cout << dp[1]-1;
    return 0;
}


#pragma GCC optimize("O3")
#include <bits/stdc++.h>
#define pb push_back
using namespace std;
using ll = long long;
const char E = '\n';
const int N = 200005;
const ll mod = 1e9+7;

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

void ANS (int ans) {
    while (!(ans&1)) ans >>= 1;
    cout << ans;
    exit (0);
}

void dfs2 (int v, int p) {
    int len = 0;
    for (auto i:g[v]) {
        if (i == p) continue;
        dfs2 (i, v);
        if (!len) {
            len = dp[i];
        }
        if (dp[i] != len) {
            cout << -1;
            exit (0);
        }
    }
    dp[v] = len + 1;
}

void dfs1 (int v, int p) {
    int len1 = 0, len2 = 0;
    for (auto i:g[v]) {
        if (i == p) continue;
        dfs1 (i, v);
        if (!len1) {
            len1 = dp[i];
            continue;
        }
        if (len1 == dp[i]) continue;
        if (!len2) {
            len2 = dp[i];
            continue;
        }
        if (len1 == dp[i] || len2 == dp[i]) continue;
        cout << -1;
        exit (0);
    }
    if (len1 && len2) {
        if (p == -1) {
            cout << len1+len2;
            exit (0);
        }
        dfs2 (p, v);
        if (len1 != dp[p] && len2 != dp[p]) {
            cout << -1;
            exit (0);
        }
        ANS (len1+len2);
    }
    dp[v] = len1 + 1;
}

int main()
{
	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	cin >> n;
	for (int i=1; i<n; i++) {
        int x, y;
        cin >> x >> y;
        g[x].pb (y);
        g[y].pb (x);
	}
	dfs1 (1, -1);
	ANS (dp[1]-1);
    return 0;
}


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

using ll=long long;
//#define int ll

#define rng(i,a,b) for(int i=int(a);i<int(b);i++)
#define rep(i,b) rng(i,0,b)
#define gnr(i,a,b) for(int i=int(b)-1;i>=int(a);i--)
#define per(i,b) gnr(i,0,b)
#define pb push_back
#define eb emplace_back
#define a first
#define b second
#define bg begin()
#define ed end()
#define all(x) x.bg,x.ed
#define si(x) int(x.size())
#ifdef LOCAL
#define dmp(x) cerr<<__LINE__<<" "<<#x<<" "<<x<<endl
#else
#define dmp(x) void(0)
#endif

template<class t,class u> void chmax(t&a,u b){if(a<b)a=b;}
template<class t,class u> void chmin(t&a,u b){if(b<a)a=b;}

template<class t> using vc=vector<t>;
template<class t> using vvc=vc<vc<t>>;

using pi=pair<int,int>;
using vi=vc<int>;

template<class t,class u>
ostream& operator<<(ostream& os,const pair<t,u>& p){
	return os<<"{"<<p.a<<","<<p.b<<"}";
}

template<class t> ostream& operator<<(ostream& os,const vc<t>& v){
	os<<"{";
	for(auto e:v)os<<e<<",";
	return os<<"}";
}

#define mp make_pair
#define mt make_tuple
#define one(x) memset(x,-1,sizeof(x))
#define zero(x) memset(x,0,sizeof(x))
#ifdef LOCAL
void dmpr(ostream&os){os<<endl;}
template<class T,class... Args>
void dmpr(ostream&os,const T&t,const Args&... args){
	os<<t<<" ";
	dmpr(os,args...);
}
#define dmp2(...) dmpr(cerr,__LINE__,##__VA_ARGS__)
#else
#define dmp2(...) void(0)
#endif

using uint=unsigned;
using ull=unsigned long long;

template<class t,size_t n>
ostream& operator<<(ostream&os,const array<t,n>&a){
	return os<<vc<t>(all(a));
}

template<int i,class T>
void print_tuple(ostream&,const T&){
}

template<int i,class T,class H,class ...Args>
void print_tuple(ostream&os,const T&t){
	if(i)os<<",";
	os<<get<i>(t);
	print_tuple<i+1,T,Args...>(os,t);
}

template<class ...Args>
ostream& operator<<(ostream&os,const tuple<Args...>&t){
	os<<"{";
	print_tuple<0,tuple<Args...>,Args...>(os,t);
	return os<<"}";
}

template<class t>
void print(t x,int suc=1){
	cout<<x;
	if(suc==1)
		cout<<"\n";
	if(suc==2)
		cout<<" ";
}

ll read(){
	ll i;
	cin>>i;
	return i;
}

vi readvi(int n,int off=0){
	vi v(n);
	rep(i,n)v[i]=read()+off;
	return v;
}

template<class T>
void print(const vector<T>&v,int suc=1){
	rep(i,v.size())
		print(v[i],i==int(v.size())-1?suc:2);
}

string readString(){
	string s;
	cin>>s;
	return s;
}

template<class T>
T sq(const T& t){
	return t*t;
}

//#define CAPITAL
void yes(bool ex=true){
	#ifdef CAPITAL
	cout<<"YES"<<"\n";
	#else
	cout<<"Yes"<<"\n";
	#endif
	if(ex)exit(0);
}
void no(bool ex=true){
	#ifdef CAPITAL
	cout<<"NO"<<"\n";
	#else
	cout<<"No"<<"\n";
	#endif
	if(ex)exit(0);
}
void possible(bool ex=true){
	#ifdef CAPITAL
	cout<<"POSSIBLE"<<"\n";
	#else
	cout<<"Possible"<<"\n";
	#endif
	if(ex)exit(0);
}
void impossible(bool ex=true){
	#ifdef CAPITAL
	cout<<"IMPOSSIBLE"<<"\n";
	#else
	cout<<"Impossible"<<"\n";
	#endif
	if(ex)exit(0);
}

constexpr ll ten(int n){
	return n==0?1:ten(n-1)*10;
}

const ll infLL=LLONG_MAX/3;

#ifdef int
const int inf=infLL;
#else
const int inf=INT_MAX/2-100;
#endif

int topbit(signed t){
	return t==0?-1:31-__builtin_clz(t);
}
int topbit(ll t){
	return t==0?-1:63-__builtin_clzll(t);
}
int botbit(signed a){
	return a==0?32:__builtin_ctz(a);
}
int botbit(ll a){
	return a==0?64:__builtin_ctzll(a);
}
int popcount(signed t){
	return __builtin_popcount(t);
}
int popcount(ll t){
	return __builtin_popcountll(t);
}
bool ispow2(int i){
	return i&&(i&-i)==i;
}
ll mask(int i){
	return (ll(1)<<i)-1;
}

bool inc(int a,int b,int c){
	return a<=b&&b<=c;
}

template<class t> void mkuni(vc<t>&v){
	sort(all(v));
	v.erase(unique(all(v)),v.ed);
}

ll rand_int(ll l, ll r) { //[l, r]
	#ifdef LOCAL
	static mt19937_64 gen;
	#else
	static mt19937_64 gen(chrono::steady_clock::now().time_since_epoch().count());
	#endif
	return uniform_int_distribution<ll>(l, r)(gen);
}

template<class t>
void myshuffle(vc<t>&a){
	rep(i,si(a))swap(a[i],a[rand_int(0,i)]);
}

template<class t>
int lwb(const vc<t>&v,const t&a){
	return lower_bound(all(v),a)-v.bg;
}

//AGC034E
//XX Opencup GP of Korea
//CF446D
//int(E)->to
template<class N,class E>
struct treedp{
	const vvc<E>& g;
	vc<N> a,b,res,sub,bus;
	void dfs1(int v,int p){
		a[v].init(v);
		E up;
		for(auto e:g[v])
			if(e!=p){
				dfs1(e,v);
				b[e]=a[v];
				a[v]=a[v]+a[e];
			}else
				up=e;
		sub[v]=a[v];
		if(p!=-1)a[v]=a[v].up(up);
	}
	void dfs2(int v,int p,N cur){
		per(i,g[v].size()){
			auto e=g[v][i];
			if(e==p)continue;
			bus[e]=cur+b[e];
			dfs2(e,v,bus[e].up(e));
			cur=cur+a[e];
		}
		res[v]=cur;
	}
	treedp(const vvc<E>&gg):g(gg),a(si(g)),b(si(g)),res(si(g)),sub(si(g)),bus(si(g)){
		dfs1(0,-1);
		N tmp;tmp.init(0);
		dfs2(0,-1,tmp);
	}
};

struct N{
	bool ok;
	int a,b;
	void init(int){
		ok=true;
		a=0;
		b=0;
	}
	N up(int)const{
		if(!ok)return {0,0,0};
		N res{1,0,0};
		if(b){
			res.ok=false;
		}else{
			res.a=a+1;
		}
		return res;
	}
	N operator+(const N&r)const{
		if(!ok||!r.ok)return {0,0,0};
		vi vals{a,b,r.a,r.b};
		dmp(vals);
		mkuni(vals);reverse(all(vals));
		N res{1,0,0};
		if(si(vals)>=1)res.a=vals[0];
		if(si(vals)>=2)res.b=vals[1];
		if(si(vals)>=3&&vals[2]){
			return {0,0,0};
		}
		dmp2(res.a,res.b);
		return res;
	}
};

signed main(){
	cin.tie(0);
	ios::sync_with_stdio(0);
	cout<<fixed<<setprecision(20);

	int n;cin>>n;
	vvc<int> t(n);
	rep(i,n-1){
		int a,b;cin>>a>>b;
		a--;b--;
		t[a].pb(b);
		t[b].pb(a);
	}

	treedp<N,int> dp(t);
	int ans=inf;
	rep(i,n)if(dp.res[i].ok){
		dmp2(i,dp.res[i].a,dp.res[i].b);
		chmin(ans,dp.res[i].a+dp.res[i].b);
	}
	if(ans==inf)ans=-1;
	print(ans);
}


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

using ll=long long;
//#define int ll

#define rng(i,a,b) for(int i=int(a);i<int(b);i++)
#define rep(i,b) rng(i,0,b)
#define gnr(i,a,b) for(int i=int(b)-1;i>=int(a);i--)
#define per(i,b) gnr(i,0,b)
#define pb push_back
#define eb emplace_back
#define a first
#define b second
#define bg begin()
#define ed end()
#define all(x) x.bg,x.ed
#define si(x) int(x.size())
#ifdef LOCAL
#define dmp(x) cerr<<__LINE__<<" "<<#x<<" "<<x<<endl
#else
#define dmp(x) void(0)
#endif

template<class t,class u> void chmax(t&a,u b){if(a<b)a=b;}
template<class t,class u> void chmin(t&a,u b){if(b<a)a=b;}

template<class t> using vc=vector<t>;
template<class t> using vvc=vc<vc<t>>;

using pi=pair<int,int>;
using vi=vc<int>;

template<class t,class u>
ostream& operator<<(ostream& os,const pair<t,u>& p){
	return os<<"{"<<p.a<<","<<p.b<<"}";
}

template<class t> ostream& operator<<(ostream& os,const vc<t>& v){
	os<<"{";
	for(auto e:v)os<<e<<",";
	return os<<"}";
}

#define mp make_pair
#define mt make_tuple
#define one(x) memset(x,-1,sizeof(x))
#define zero(x) memset(x,0,sizeof(x))
#ifdef LOCAL
void dmpr(ostream&os){os<<endl;}
template<class T,class... Args>
void dmpr(ostream&os,const T&t,const Args&... args){
	os<<t<<" ";
	dmpr(os,args...);
}
#define dmp2(...) dmpr(cerr,__LINE__,##__VA_ARGS__)
#else
#define dmp2(...) void(0)
#endif

using uint=unsigned;
using ull=unsigned long long;

template<class t,size_t n>
ostream& operator<<(ostream&os,const array<t,n>&a){
	return os<<vc<t>(all(a));
}

template<int i,class T>
void print_tuple(ostream&,const T&){
}

template<int i,class T,class H,class ...Args>
void print_tuple(ostream&os,const T&t){
	if(i)os<<",";
	os<<get<i>(t);
	print_tuple<i+1,T,Args...>(os,t);
}

template<class ...Args>
ostream& operator<<(ostream&os,const tuple<Args...>&t){
	os<<"{";
	print_tuple<0,tuple<Args...>,Args...>(os,t);
	return os<<"}";
}

template<class t>
void print(t x,int suc=1){
	cout<<x;
	if(suc==1)
		cout<<"\n";
	if(suc==2)
		cout<<" ";
}

ll read(){
	ll i;
	cin>>i;
	return i;
}

vi readvi(int n,int off=0){
	vi v(n);
	rep(i,n)v[i]=read()+off;
	return v;
}

template<class T>
void print(const vector<T>&v,int suc=1){
	rep(i,v.size())
		print(v[i],i==int(v.size())-1?suc:2);
}

string readString(){
	string s;
	cin>>s;
	return s;
}

template<class T>
T sq(const T& t){
	return t*t;
}

//#define CAPITAL
void yes(bool ex=true){
	#ifdef CAPITAL
	cout<<"YES"<<"\n";
	#else
	cout<<"Yes"<<"\n";
	#endif
	if(ex)exit(0);
}
void no(bool ex=true){
	#ifdef CAPITAL
	cout<<"NO"<<"\n";
	#else
	cout<<"No"<<"\n";
	#endif
	if(ex)exit(0);
}
void possible(bool ex=true){
	#ifdef CAPITAL
	cout<<"POSSIBLE"<<"\n";
	#else
	cout<<"Possible"<<"\n";
	#endif
	if(ex)exit(0);
}
void impossible(bool ex=true){
	#ifdef CAPITAL
	cout<<"IMPOSSIBLE"<<"\n";
	#else
	cout<<"Impossible"<<"\n";
	#endif
	if(ex)exit(0);
}

constexpr ll ten(int n){
	return n==0?1:ten(n-1)*10;
}

const ll infLL=LLONG_MAX/3;

#ifdef int
const int inf=infLL;
#else
const int inf=INT_MAX/2-100;
#endif

int topbit(signed t){
	return t==0?-1:31-__builtin_clz(t);
}
int topbit(ll t){
	return t==0?-1:63-__builtin_clzll(t);
}
int botbit(signed a){
	return a==0?32:__builtin_ctz(a);
}
int botbit(ll a){
	return a==0?64:__builtin_ctzll(a);
}
int popcount(signed t){
	return __builtin_popcount(t);
}
int popcount(ll t){
	return __builtin_popcountll(t);
}
bool ispow2(int i){
	return i&&(i&-i)==i;
}
ll mask(int i){
	return (ll(1)<<i)-1;
}

bool inc(int a,int b,int c){
	return a<=b&&b<=c;
}

template<class t> void mkuni(vc<t>&v){
	sort(all(v));
	v.erase(unique(all(v)),v.ed);
}

ll rand_int(ll l, ll r) { //[l, r]
	#ifdef LOCAL
	static mt19937_64 gen;
	#else
	static mt19937_64 gen(chrono::steady_clock::now().time_since_epoch().count());
	#endif
	return uniform_int_distribution<ll>(l, r)(gen);
}

template<class t>
void myshuffle(vc<t>&a){
	rep(i,si(a))swap(a[i],a[rand_int(0,i)]);
}

template<class t>
int lwb(const vc<t>&v,const t&a){
	return lower_bound(all(v),a)-v.bg;
}

//AGC034E
//XX Opencup GP of Korea
//CF446D
//int(E)->to
template<class N,class E>
struct treedp{
	const vvc<E>& g;
	vc<N> a,b,res,sub,bus;
	void dfs1(int v,int p){
		a[v].init(v);
		E up;
		for(auto e:g[v])
			if(e!=p){
				dfs1(e,v);
				b[e]=a[v];
				a[v]=a[v]+a[e];
			}else
				up=e;
		sub[v]=a[v];
		if(p!=-1)a[v]=a[v].up(up);
	}
	void dfs2(int v,int p,N cur){
		per(i,g[v].size()){
			auto e=g[v][i];
			if(e==p)continue;
			bus[e]=cur+b[e];
			dfs2(e,v,bus[e].up(e));
			cur=cur+a[e];
		}
		res[v]=cur;
	}
	treedp(const vvc<E>&gg):g(gg),a(si(g)),b(si(g)),res(si(g)),sub(si(g)),bus(si(g)){
		dfs1(0,-1);
		N tmp;tmp.init(0);
		dfs2(0,-1,tmp);
	}
};

struct N{
	bool ok;
	int a,b;
	void init(int){
		ok=true;
		a=0;
		b=0;
	}
	N up(int)const{
		if(!ok)return {0,0,0};
		N res{1,0,0};
		if(b){
			res.ok=false;
		}else{
			res.a=a+1;
		}
		return res;
	}
	N operator+(const N&r)const{
		if(!ok||!r.ok)return {0,0,0};
		vi vals{a,b,r.a,r.b};
		dmp(vals);
		mkuni(vals);reverse(all(vals));
		N res{1,0,0};
		if(si(vals)>=1)res.a=vals[0];
		if(si(vals)>=2)res.b=vals[1];
		if(si(vals)>=3&&vals[2]){
			return {0,0,0};
		}
		dmp2(res.a,res.b);
		return res;
	}
};

signed main(){
	cin.tie(0);
	ios::sync_with_stdio(0);
	cout<<fixed<<setprecision(20);

	int n;cin>>n;
	vvc<int> t(n);
	rep(i,n-1){
		int a,b;cin>>a>>b;
		a--;b--;
		t[a].pb(b);
		t[b].pb(a);
	}

	treedp<N,int> dp(t);
	int ans=inf;
	rep(i,n)if(dp.res[i].ok){
		dmp2(i,dp.res[i].a,dp.res[i].b);
		chmin(ans,dp.res[i].a+dp.res[i].b);
	}
	if(ans==inf)ans=-1;
	if(ans%2==0)ans=0;
	print(ans);
}


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

using ll=long long;
//#define int ll

#define rng(i,a,b) for(int i=int(a);i<int(b);i++)
#define rep(i,b) rng(i,0,b)
#define gnr(i,a,b) for(int i=int(b)-1;i>=int(a);i--)
#define per(i,b) gnr(i,0,b)
#define pb push_back
#define eb emplace_back
#define a first
#define b second
#define bg begin()
#define ed end()
#define all(x) x.bg,x.ed
#define si(x) int(x.size())
#ifdef LOCAL
#define dmp(x) cerr<<__LINE__<<" "<<#x<<" "<<x<<endl
#else
#define dmp(x) void(0)
#endif

template<class t,class u> void chmax(t&a,u b){if(a<b)a=b;}
template<class t,class u> void chmin(t&a,u b){if(b<a)a=b;}

template<class t> using vc=vector<t>;
template<class t> using vvc=vc<vc<t>>;

using pi=pair<int,int>;
using vi=vc<int>;

template<class t,class u>
ostream& operator<<(ostream& os,const pair<t,u>& p){
	return os<<"{"<<p.a<<","<<p.b<<"}";
}

template<class t> ostream& operator<<(ostream& os,const vc<t>& v){
	os<<"{";
	for(auto e:v)os<<e<<",";
	return os<<"}";
}

#define mp make_pair
#define mt make_tuple
#define one(x) memset(x,-1,sizeof(x))
#define zero(x) memset(x,0,sizeof(x))
#ifdef LOCAL
void dmpr(ostream&os){os<<endl;}
template<class T,class... Args>
void dmpr(ostream&os,const T&t,const Args&... args){
	os<<t<<" ";
	dmpr(os,args...);
}
#define dmp2(...) dmpr(cerr,__LINE__,##__VA_ARGS__)
#else
#define dmp2(...) void(0)
#endif

using uint=unsigned;
using ull=unsigned long long;

template<class t,size_t n>
ostream& operator<<(ostream&os,const array<t,n>&a){
	return os<<vc<t>(all(a));
}

template<int i,class T>
void print_tuple(ostream&,const T&){
}

template<int i,class T,class H,class ...Args>
void print_tuple(ostream&os,const T&t){
	if(i)os<<",";
	os<<get<i>(t);
	print_tuple<i+1,T,Args...>(os,t);
}

template<class ...Args>
ostream& operator<<(ostream&os,const tuple<Args...>&t){
	os<<"{";
	print_tuple<0,tuple<Args...>,Args...>(os,t);
	return os<<"}";
}

template<class t>
void print(t x,int suc=1){
	cout<<x;
	if(suc==1)
		cout<<"\n";
	if(suc==2)
		cout<<" ";
}

ll read(){
	ll i;
	cin>>i;
	return i;
}

vi readvi(int n,int off=0){
	vi v(n);
	rep(i,n)v[i]=read()+off;
	return v;
}

template<class T>
void print(const vector<T>&v,int suc=1){
	rep(i,v.size())
		print(v[i],i==int(v.size())-1?suc:2);
}

string readString(){
	string s;
	cin>>s;
	return s;
}

template<class T>
T sq(const T& t){
	return t*t;
}

//#define CAPITAL
void yes(bool ex=true){
	#ifdef CAPITAL
	cout<<"YES"<<"\n";
	#else
	cout<<"Yes"<<"\n";
	#endif
	if(ex)exit(0);
}
void no(bool ex=true){
	#ifdef CAPITAL
	cout<<"NO"<<"\n";
	#else
	cout<<"No"<<"\n";
	#endif
	if(ex)exit(0);
}
void possible(bool ex=true){
	#ifdef CAPITAL
	cout<<"POSSIBLE"<<"\n";
	#else
	cout<<"Possible"<<"\n";
	#endif
	if(ex)exit(0);
}
void impossible(bool ex=true){
	#ifdef CAPITAL
	cout<<"IMPOSSIBLE"<<"\n";
	#else
	cout<<"Impossible"<<"\n";
	#endif
	if(ex)exit(0);
}

constexpr ll ten(int n){
	return n==0?1:ten(n-1)*10;
}

const ll infLL=LLONG_MAX/3;

#ifdef int
const int inf=infLL;
#else
const int inf=INT_MAX/2-100;
#endif

int topbit(signed t){
	return t==0?-1:31-__builtin_clz(t);
}
int topbit(ll t){
	return t==0?-1:63-__builtin_clzll(t);
}
int botbit(signed a){
	return a==0?32:__builtin_ctz(a);
}
int botbit(ll a){
	return a==0?64:__builtin_ctzll(a);
}
int popcount(signed t){
	return __builtin_popcount(t);
}
int popcount(ll t){
	return __builtin_popcountll(t);
}
bool ispow2(int i){
	return i&&(i&-i)==i;
}
ll mask(int i){
	return (ll(1)<<i)-1;
}

bool inc(int a,int b,int c){
	return a<=b&&b<=c;
}

template<class t> void mkuni(vc<t>&v){
	sort(all(v));
	v.erase(unique(all(v)),v.ed);
}

ll rand_int(ll l, ll r) { //[l, r]
	#ifdef LOCAL
	static mt19937_64 gen;
	#else
	static mt19937_64 gen(chrono::steady_clock::now().time_since_epoch().count());
	#endif
	return uniform_int_distribution<ll>(l, r)(gen);
}

template<class t>
void myshuffle(vc<t>&a){
	rep(i,si(a))swap(a[i],a[rand_int(0,i)]);
}

template<class t>
int lwb(const vc<t>&v,const t&a){
	return lower_bound(all(v),a)-v.bg;
}

//AGC034E
//XX Opencup GP of Korea
//CF446D
//int(E)->to
template<class N,class E>
struct treedp{
	const vvc<E>& g;
	vc<N> a,b,res,sub,bus;
	void dfs1(int v,int p){
		a[v].init(v);
		E up;
		for(auto e:g[v])
			if(e!=p){
				dfs1(e,v);
				b[e]=a[v];
				a[v]=a[v]+a[e];
			}else
				up=e;
		sub[v]=a[v];
		if(p!=-1)a[v]=a[v].up(up);
	}
	void dfs2(int v,int p,N cur){
		per(i,g[v].size()){
			auto e=g[v][i];
			if(e==p)continue;
			bus[e]=cur+b[e];
			dfs2(e,v,bus[e].up(e));
			cur=cur+a[e];
		}
		res[v]=cur;
	}
	treedp(const vvc<E>&gg):g(gg),a(si(g)),b(si(g)),res(si(g)),sub(si(g)),bus(si(g)){
		dfs1(0,-1);
		N tmp;tmp.init(0);
		dfs2(0,-1,tmp);
	}
};

struct N{
	bool ok;
	int a,b;
	void init(int){
		ok=true;
		a=0;
		b=0;
	}
	N up(int)const{
		if(!ok)return {0,0,0};
		N res{1,0,0};
		if(b){
			res.ok=false;
		}else{
			res.a=a+1;
		}
		return res;
	}
	N operator+(const N&r)const{
		if(!ok||!r.ok)return {0,0,0};
		vi vals{a,b,r.a,r.b};
		dmp(vals);
		mkuni(vals);reverse(all(vals));
		N res{1,0,0};
		if(si(vals)>=1)res.a=vals[0];
		if(si(vals)>=2)res.b=vals[1];
		if(si(vals)>=3&&vals[2]){
			return {0,0,0};
		}
		dmp2(res.a,res.b);
		return res;
	}
};

signed main(){
	cin.tie(0);
	ios::sync_with_stdio(0);
	cout<<fixed<<setprecision(20);

	int n;cin>>n;
	vvc<int> t(n);
	rep(i,n-1){
		int a,b;cin>>a>>b;
		a--;b--;
		t[a].pb(b);
		t[b].pb(a);
	}

	treedp<N,int> dp(t);
	int ans=inf;
	rep(i,n)if(dp.res[i].ok){
		dmp2(i,dp.res[i].a,dp.res[i].b);
		chmin(ans,dp.res[i].a+dp.res[i].b);
	}
	if(ans==inf)ans=-1;
	while(ans%2==0)ans/=2;
	print(ans);
}


#include<bits/stdc++.h>
#include <unordered_map>
#define endl "\n"
#define all(v) v.begin(),v.end()
#define allr(s) s.rbegin(),s.rend()
#define RT(s) return cout<<s,0
#define watch(x) cout << (#x)<<" = "<<x<<endl
#define sz(s)	(int)(s.size())
#define PI acos(-1)
#define EPS 1e-8
using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair<int, int> ii;
typedef vector<int> vi;
typedef vector<ii> vii;
typedef vector<ll> row;
typedef vector<row> matrix;
int dy[] = { 1, -1, 0, 0, -1, 1, 1, -1 };
int dx[] = { 0, 0, 1, -1, 1, -1, 1, -1 };
void file() {
#ifndef ONLINE_JUDGE
	freopen("in.txt", "r", stdin);
	//freopen("out.txt", "w", stdout);
#else
	//freopen("street.in", "r", stdin);
	//freopen("out.txt", "w", stdout);
#endif
}
void fast() {
	std::ios_base::sync_with_stdio(0);
	cin.tie(NULL);
}
const int N = 2e5 + 9;
int n;
bool no;
vi adjL[N];
int solve(int u, int par) {
	set<int> st;
	int hold = par;
	for (auto v : adjL[u]) {
		if (v == par)
			continue;
		hold = v;
		st.insert(solve(v, u));
	}
	if (sz(st) == 1) {
		adjL[u] = vi(1, hold);
		return *st.begin() + 1;
	}
	else if (st.empty())
		return 1;
	if (par != -1)
		no = 1;
	else if (sz(st) > 2)
		no = 1;
	if (par == -1) {
		int ans = 1;
		for (auto it : st)
			ans += it;
		return ans;
	}
	return 0;
}
int main() {
	file();
	fast();
	cin >> n;
	for (int i = 0; i < n - 1; i++) {
		int u, v; cin >> u >> v;
		adjL[u].push_back(v);
		adjL[v].push_back(u);
	}
	int root = 0;
	for (int i = 1; i <= n; i++)
		if (sz(adjL[i]) > sz(adjL[root]))
			root = i;
	int ans = solve(root, -1);
	if (no)
		RT(-1 << endl);
	cout << ans - 1 << endl;
	return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include<bits/stdc++.h>
#include <string>
#include <unordered_map>
#define endl "\n"
#define all(v) v.begin(),v.end()
#define allr(s) s.rbegin(),s.rend()
#define RT(s) return cout<<s,0
#define watch(x) cout << (#x)<<" = "<<x<<endl
#define sz(s)	(int)(s.size())
#define PI acos(-1)
#define EPS 1e-8
#define v(x) vector<x>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair<int, int> ii;
typedef vector<int> vi;
typedef vector<ii> vii;
int dy[] = { 1, -1, 0, 0, -1, 1, 1, -1 };
int dx[] = { 0, 0, 1, -1, 1, -1, 1, -1 };
void file() {
#ifndef ONLINE_JUDGE
	freopen("in.txt", "r", stdin);
	//freopen("out.txt", "w", stdout);
#else
	//freopen("street.in", "r", stdin);
	//freopen("out.txt", "w", stdout);
#endif
}
void fast() {
	std::ios_base::sync_with_stdio(0);
	cin.tie(NULL);
}
const int N = 1e5 + 9;
int n;
bool no;
vector<vi> adjL(N);
vi parent(N, -1);
ii dfs(int u, int par, int dep) {
	parent[u] = par;
	ii ret = { u, dep };
	for (auto v : adjL[u]) {
		if (v == par)
			continue;
		ii x = dfs(v, u, dep + 1);
		if (x.second > ret.second)
			ret = x;
	}
	return ret;
}
vi TreeDiameter() {
	int node = dfs(1, -1, 0).first;
	parent = vi(N, -1);
	node = dfs(node, -1, 0).first;
	vi ret;
	while (node != -1) {
		ret.push_back(node);
		node = parent[node];
	}
	return ret;
}
int solve(int u, int par) {
	set<int> st;
	int hold = par;
	for (auto v : adjL[u]) {
		if (v == par)
			continue;
		hold = v;
		st.insert(solve(v, u));
	}
	if (sz(st) == 1) {
		adjL[u] = vi(1, hold);
		return *st.begin() + 1;
	}
	else if (st.empty())
		return 1;
	if (par != -1)
		no = 1;
	else if (sz(st) > 2)
		no = 1;
	if (par == -1) {
		int ans = 1;
		for (auto it : st)
			ans += it;
		return ans;
	}
	return 0;
}
int main() {
	fast();
	file();
	cin >> n;
	for (int i = 0; i < n - 1; i++) {
		int u, v; cin >> u >> v;
		adjL[u].push_back(v);
		adjL[v].push_back(u);
	}
	vi a = TreeDiameter();
	int root = a[sz(a) / 2];
	int ans = solve(root, -1);
	if (no)
		RT(-1 << endl);
	while (ans & 1)
		ans = ans / 2 + 1;
	cout << ans - 1 << endl;
	return 0;
}


#include<bits/stdc++.h>
#include <unordered_map>
#define endl "\n"
#define all(v) v.begin(),v.end()
#define allr(s) s.rbegin(),s.rend()
#define RT(s) return cout<<s,0
#define watch(x) cout << (#x)<<" = "<<x<<endl
#define sz(s)	(int)(s.size())
#define PI acos(-1)
#define EPS 1e-8
using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair<int, int> ii;
typedef vector<int> vi;
typedef vector<ii> vii;
typedef vector<ll> row;
typedef vector<row> matrix;
int dy[] = { 1, -1, 0, 0, -1, 1, 1, -1 };
int dx[] = { 0, 0, 1, -1, 1, -1, 1, -1 };
void file() {
#ifndef ONLINE_JUDGE
	freopen("in.txt", "r", stdin);
	//freopen("out.txt", "w", stdout);
#else
	//freopen("street.in", "r", stdin);
	//freopen("out.txt", "w", stdout);
#endif
}
void fast() {
	std::ios_base::sync_with_stdio(0);
	cin.tie(NULL);
}
const int N = 2e5 + 9;
int n;
bool no;
vi adjL[N];
int solve(int u, int par) {
	set<int> st;
	int hold = par;
	for (auto v : adjL[u]) {
		if (v == par)
			continue;
		hold = v;
		st.insert(solve(v, u));
	}
	if (sz(st) == 1) {
		adjL[u] = vi(1, hold);
		return *st.begin() + 1;
	}
	else if (st.empty())
		return 1;
	if (par != -1)
		no = 1;
	else if (sz(st) > 2)
		no = 1;
	if (par == -1) {
		int ans = 1;
		for (auto it : st)
			ans += it;
		return ans;
	}
	return 0;
}
int main() {
	file();
	fast();
	cin >> n;
	for (int i = 0; i < n - 1; i++) {
		int u, v; cin >> u >> v;
		adjL[u].push_back(v);
		adjL[v].push_back(u);
	}
	int root = 0;
	for (int i = 1; i <= n; i++)
		if (sz(adjL[i]) > sz(adjL[root]))
			root = i;
	int ans = solve(root, -1);
	if (no)
		RT(-1 << endl);
	if (ans & 1)
		ans = ans / 2 + 1;
	cout << ans - 1 << endl;
	return 0;
}


#include<bits/stdc++.h>
#include <unordered_map>
#define endl "\n"
#define all(v) v.begin(),v.end()
#define allr(s) s.rbegin(),s.rend()
#define RT(s) return cout<<s,0
#define watch(x) cout << (#x)<<" = "<<x<<endl
#define sz(s)	(int)(s.size())
#define PI acos(-1)
#define EPS 1e-8
using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair<int, int> ii;
typedef vector<int> vi;
typedef vector<ii> vii;
typedef vector<ll> row;
typedef vector<row> matrix;
int dy[] = { 1, -1, 0, 0, -1, 1, 1, -1 };
int dx[] = { 0, 0, 1, -1, 1, -1, 1, -1 };
void file() {
#ifndef ONLINE_JUDGE
	freopen("in.txt", "r", stdin);
	//freopen("out.txt", "w", stdout);
#else
	//freopen("street.in", "r", stdin);
	//freopen("out.txt", "w", stdout);
#endif
}
void fast() {
	std::ios_base::sync_with_stdio(0);
	cin.tie(NULL);
}
const int N = 2e5 + 9;
int n;
bool no;
vi adjL[N];
int solve(int u, int par) {
	set<int> st;
	int hold = par;
	for (auto v : adjL[u]) {
		if (v == par)
			continue;
		hold = v;
		st.insert(solve(v, u));
	}
	if (sz(st) == 1) {
		adjL[u] = vi(1, hold);
		return *st.begin() + 1;
	}
	else if (st.empty())
		return 1;
	if (par != -1)
		no = 1;
	else if (sz(st) > 2)
		no = 1;
	if (par == -1) {
		int ans = 1;
		for (auto it : st)
			ans += it;
		return ans;
	}
	return 0;
}
int main() {
	file();
	fast();
	cin >> n;
	for (int i = 0; i < n - 1; i++) {
		int u, v; cin >> u >> v;
		adjL[u].push_back(v);
		adjL[v].push_back(u);
	}
	int root = 0;
	for (int i = 1; i <= n; i++)
		if (sz(adjL[i]) > sz(adjL[root]))
			root = i;
	int ans = solve(root, -1);
	if (no)
		RT(-1 << endl);
	while (ans & 1)
		ans = ans / 2 + 1;
	cout << ans - 1 << endl;
	return 0;
}


#include<bits/stdc++.h>
#include <unordered_map>
#define endl "\n"
#define all(v) v.begin(),v.end()
#define allr(s) s.rbegin(),s.rend()
#define RT(s) return cout<<s,0
#define watch(x) cout << (#x)<<" = "<<x<<endl
#define sz(s)	(int)(s.size())
#define PI acos(-1)
#define EPS 1e-8
using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair<int, int> ii;
typedef vector<int> vi;
typedef vector<ii> vii;
typedef vector<ll> row;
typedef vector<row> matrix;
int dy[] = { 1, -1, 0, 0, -1, 1, 1, -1 };
int dx[] = { 0, 0, 1, -1, 1, -1, 1, -1 };
void file() {
#ifndef ONLINE_JUDGE
	freopen("in.txt", "r", stdin);
	//freopen("out.txt", "w", stdout);
#else
	//freopen("street.in", "r", stdin);
	//freopen("out.txt", "w", stdout);
#endif
}
void fast() {
	std::ios_base::sync_with_stdio(0);
	cin.tie(NULL);
}
const int N = 2e5 + 9;
int n;
bool no;
vi adjL[N];
int solve(int u, int par) {
	set<int> st;
	int hold = par;
	for (auto v : adjL[u]) {
		if (v == par)
			continue;
		hold = v;
		st.insert(solve(v, u));
	}
	if (sz(st) == 1) {
		adjL[u] = vi(1, hold);
		return *st.begin() + 1;
	}
	else if (st.empty())
		return 1;
	if (par != -1)
		no = 1;
	else if (sz(st) > 2)
		no = 1;
	if (par == -1) {
		int ans = 1;
		for (auto it : st)
			ans += it;
		return ans;
	}
	return 5;
}
int main() {
	file();
	fast();
	cin >> n;
	for (int i = 0; i < n - 1; i++) {
		int u, v; cin >> u >> v;
		adjL[u].push_back(v);
		adjL[v].push_back(u);
	}
	int root = 0;
	for (int i = 1; i <= n; i++)
		if (sz(adjL[i]) > sz(adjL[root]))
			root = i;
	int ans = solve(root, -1);
	if (no)
		RT(-1 << endl);
	cout << ans - 1 << endl;
	return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include<bits/stdc++.h>
#include <string>
#include <unordered_map>
#define endl "\n"
#define all(v) v.begin(),v.end()
#define allr(s) s.rbegin(),s.rend()
#define RT(s) return cout<<s,0
#define watch(x) cout << (#x)<<" = "<<x<<endl
#define sz(s)	(int)(s.size())
#define PI acos(-1)
#define EPS 1e-8
#define v(x) vector<x>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair<int, int> ii;
typedef vector<int> vi;
typedef vector<ii> vii;
int dy[] = { 1, -1, 0, 0, -1, 1, 1, -1 };
int dx[] = { 0, 0, 1, -1, 1, -1, 1, -1 };
void file() {
#ifndef ONLINE_JUDGE
	freopen("in.txt", "r", stdin);
	//freopen("out.txt", "w", stdout);
#else
	//freopen("street.in", "r", stdin);
	//freopen("out.txt", "w", stdout);
#endif
}
void fast() {
	std::ios_base::sync_with_stdio(0);
	cin.tie(NULL);
}
const int N = 2e5 + 9;
int n;
bool no;
vector<vi> adjL(N);
vi parent(N, -1);
ii dfs(int u, int par, int dep) {
	parent[u] = par;
	ii ret = { u, dep };
	for (auto v : adjL[u]) {
		if (v == par)
			continue;
		ii x = dfs(v, u, dep + 1);
		if (x.second > ret.second)
			ret = x;
	}
	return ret;
}
vi TreeDiameter() {
	int node = dfs(1, -1, 0).first;
	parent = vi(N, -1);
	node = dfs(node, -1, 0).first;
	vi ret;
	while (node != -1) {
		ret.push_back(node);
		node = parent[node];
	}
	return ret;
}
int solve(int u, int par) {
	set<int> st;
	int hold = par;
	for (auto v : adjL[u]) {
		if (v == par)
			continue;
		hold = v;
		st.insert(solve(v, u));
	}
	if (sz(st) == 1) {
		adjL[u] = vi(1, hold);
		return *st.begin() + 1;
	}
	else if (st.empty())
		return 1;
	if (par != -1)
		no = 1;
	else if (sz(st) > 2)
		no = 1;
	if (par == -1) {
		int ans = 1;
		for (auto it : st)
			ans += it;
		return ans;
	}
	return 0;
}
int main() {
	fast();
	file();
	cin >> n;
	for (int i = 0; i < n - 1; i++) {
		int u, v; cin >> u >> v;
		adjL[u].push_back(v);
		adjL[v].push_back(u);
	}
	vi a = TreeDiameter();
	int root = a[sz(a) / 2];
	int ans = solve(root, -1);
	if (no)
		RT(-1 << endl);
	while (ans & 1)
		ans = ans / 2 + 1;
	cout << ans - 1 << endl;
	return 0;
}


#include<bits/stdc++.h>
#define ll long long
#define IO ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
using namespace std;

const int N = 2e5 + 50;

int n, ans, u;
vector<vector<int> > adj(N);

int dfs(int v, int p)
{
	set<int> s;
	for(auto i : adj[v])
	{
		if(i == p)
			continue;
		int h = dfs(i, v);
		if(h == -1)
			return -1;
		s.insert(h + 1);
	}
	if(!s.size())
		return 0;
	if(s.size() == 1)
		return *s.begin();
	if(s.size() == 2 && !p)
		return *s.begin() + *s.rbegin();
	u = v;
	return -1;
}

int main()
{
	IO;
	cin >> n;
	for(int i = 1, u, v; i < n; i++)
	{
		cin >> u >> v;
		adj[u].push_back(v);
		adj[v].push_back(u);
	}
	int h = dfs(1, 0);
	if(h == -1 && u)
		h = dfs(u, 0);
	while(h && h % 2 == 0)
		h /= 2;
	cout << h;
}


/*input
7
1 2
2 4
2 5
1 3
4 7
5 6
2 3
3 4
4 5
*/
#include <bits/stdc++.h>
using namespace std;

#define int long long
#define pii pair<int,int>
#define pb push_back
#define f first
#define s second
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int const N = 1e5 + 10;
vector<int > v[N];
int n,x,y;
int root = 0;
int ans = 0;
int dfs2(int i , int p = -1){
	if(v[i].size() == 1 && p != -1){
		return 1;
	}
	set<int> s;
	for(auto x : v[i]){
		if(x == p) continue;
		s.insert(dfs2(x,i));
	}
	if(p == -1){
		// for(auto x : s){
		// 	cout << x << " ";
		// }
		if(s.size() >= 3){
			ans = -1;
			cout << ans << endl;
			exit(0);
			return ans;
		}
		if(s.size() <= 2){
			ans = 0;
			for(auto x : s){
				ans += x;
			}
			while(ans%2 == 0 && ans != 0){
				ans /= 2;
			}
			cout << ans << endl;
			exit(0);
			return ans;
		}
	}else{
		if(s.size() >= 2){
			root = i;
			ans = -1;
			cout << -1 << endl;
			exit(0);
			return 0;
		}
		else{
			return (1 + *s.begin());
		}
	}
}

int dfs(int i,int p = -1){
	// cout << i << " ";
	if(v[i].size() == 1 && p != -1){
		return 1;
	}
	set<int> s;
	for(auto x : v[i]){
		if(x == p)continue;
		s.insert(dfs(x,i));
	}
	if(p == -1){

		if(s.size() >= 3){
			dfs2(2);
			return 0;
		}
		if(s.size() <= 2){
			ans = 0;
			for(auto x : s){
				ans += x;
			}
			while(ans%2 == 0 && ans != 0){
				ans /= 2;
			}
			cout << ans << endl;
			exit(0);

		}
	}else{
		if(s.size() >= 2){
			root = i;
			dfs2(root);
			return 0;
		}
		else{
			return (1 + *s.begin());
		}
	}
	return 0;
}

void solve(){
	int n;
	cin >> n;
	for (int i = 0 ; i < n - 1 ; i++){
		int  x , y;
		cin >> x >> y;
		v[x].pb(y);
		v[y].pb(x);
	}
	dfs(1);
}
signed main() {
	IOS;
	int t = 1;
	// cin >> t;
	while(t--){
		solve();
	}
return 0;
}


/*input
6
1 2
2 3
2 4
4 5
1 6

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

#define int long long
#define pii pair<int,int>
#define pb push_back
#define f first
#define s second
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int const N = 2e5 + 10;
vector<int > v[N];
int n,x,y;
int root = 0;
int ans = 0;
int dfs2(int i , int p = -1){
	if(v[i].size() == 1 && p != -1){
		return 1;
	}
	set<int> s;
	for(auto x : v[i]){
		if(x == p) continue;
		s.insert(dfs2(x,i));
	}
	if(p == -1){
		// for(auto x : s){
		// 	cout << x << " ";
		// }
		if(s.size() >= 3){
			ans = -1;
			cout << ans << endl;
			exit(0);
			return ans;
		}
		if(s.size() <= 2){
			ans = 0;
			for(auto x : s){
				ans += x;
			}
			while(ans%2 == 0 && ans != 0){
				ans /= 2;
			}
			cout << ans << endl;
			exit(0);
			return ans;
		}
	}else{
		if(s.size() >= 2){
			root = i;
			ans = -1;
			cout << -1 << endl;
			exit(0);
			return 0;
		}
		else{
			return (1 + *s.begin());
		}
	}
}

int dfs(int i,int p = -1){
	// cout << i << " ";
	if(v[i].size() == 1 && p != -1){
		return 1;
	}
	set<int> s;
	for(auto x : v[i]){
		if(x == p)continue;
		s.insert(dfs(x,i));
	}
	if(p == -1){

		if(s.size() >= 3){
			dfs2(2);
			return 0;
		}
		if(s.size() <= 2){
			ans = 0;
			for(auto x : s){
				ans += x;
			}
			while(ans%2 == 0 && ans != 0){
				ans /= 2;
			}
			cout << ans << endl;
			exit(0);

		}
	}else{
		if(s.size() >= 2){
			root = i;
			dfs2(root);
			return 0;
		}
		else{
			return (1 + *s.begin());
		}
	}
	return 0;
}

void solve(){
	int n;
	cin >> n;
	for (int i = 0 ; i < n - 1 ; i++){
		int  x , y;
		cin >> x >> y;
		v[x].pb(y);
		v[y].pb(x);
	}
	dfs(1);
}
signed main() {
	IOS;
	int t = 1;
	// cin >> t;
	while(t--){
		solve();
	}
return 0;
}


// You either die a hero, or you live long enough to see yourself become the villain.

/**
 *   author:  Blind_is_love
 *   created: 15.10.2019, 00:32
**/

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace __gnu_pbds;
using namespace std;

typedef long long ll;

template <class T> ostream& operator << (ostream &os, const vector<T> &v) { for (T i : v) os << i << ' '; return os; }
template <class T> ostream& operator << (ostream &os, const set<T> &v) { for (T i : v) os << i << ' '; return os; }
template <class T, class S> ostream& operator << (ostream &os, const pair<T, S> &v) { os << v.first << ' ' << v.second; return os; }
template <class T, class S> ostream& operator << (ostream &os, const map<T, S> &v) { for (auto i : v) os << '(' << i.first << "=>" << i.second << ')' << ' '; return os; }
template <class T, class S> ostream& operator << (ostream &os, const unordered_map<T, S> &v) { for (auto i : v) os << '(' << i.first << "=>" << i.second << ')' << ' '; return os; }

#ifndef ONLINE_JUDGE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
    template <class Arg1> void __f(const char* name, Arg1&& arg1) { cerr << name << " : " << arg1 << endl; }
    template <class Arg1, class... Args>
    void __f(const char* names, Arg1&& arg1, Args&&... args) {
        const char* sep = strchr(names + 1, ',');
        cerr.write(names, sep - names) << " : " << arg1 << "  ";
        __f(sep + 1, args...);
    }
#else
#define trace(...) 0
#pragma GCC optimize ("O3")
#pragma GCC optimize ("unroll-loops")
#pragma GCC target("avx2,sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#define _CRT_SECURE_NO_WARNINGS
#endif // ifndef ONLINE_JUDGE

#define endl "\n"
#define vll vector<int>
#define max_heap priority_queue<int>
#define min_heap priority_queue<int , vector<int>, greater<int> >
#define pb push_back
#define all(container) container.begin(), container.end()
#define pll pair<ll,ll>
#define pii pair<int,int>
#define printArr(name,from,to) for(int x = from; x < to; x++) {cout << name[x] << " ";} cout << endl;
#define fi first
#define se second
#define mp make_pair
#define prime1 304933
#define prime2 15486277
#define mod2 179424691
#define int ll

typedef tree<ll, null_type, less<ll>, rb_tree_tag,tree_order_statistics_node_update> ordered_set;

const ll MOD = 1000000007;
const ll MAX = 2e5+10;
const double PI = 3.1415926;
const ll INF = 1e18;
const double EPS = 1e-5;

set<int> adj[MAX];
int n;

signed main(){
	ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
	clock_t t1, t2;
	t1 = clock();
	cin >> n;
	for(int i = 0 ; i < n-1; ++i){
		int x,y;
		cin >> x >> y;
		x--;
		y--;
		adj[x].insert(y);
		adj[y].insert(x);
	}
	queue<int> q;
	int child[n];
	memset(child,-1,sizeof(child));
	bool visited[n];
	memset(visited,false,sizeof(visited));

	for(int i = 0 ; i < n; ++i){
		if (adj[i].size()==1){
			q.push(i);
			visited[i] = true;
		}
	}

	int level = 1;
	int tot = n-1;

	while(!q.empty()){
		set<int> visit;
		while(!q.empty()){
			int s = q.front();
			q.pop();
			for(int x : adj[s]){
				if (child[s] == x) continue;
				if (visit.find(x) != visit.end()){
					adj[x].erase(s);
					tot -= level;
					continue;
				}
				// visited[x] = true;
				visit.insert(x);
				child[x] = s;
			}
		}

		// cout << "New leaves are: " << level++ << endl;
		level++;

		for(int x : visit){
			if (adj[x].size() == 2) {
				q.push(x);
				// cout << x+1 << endl;
			}

			if (adj[x].size() > 2 && visited[x]){
				cout << -1 << endl;
				return 0;
			}

			visited[x] = true;
		}
	}
	cout << tot << endl;

	t2 = clock(); cerr << "time taken: " << (t2-t1)/(long double)CLOCKS_PER_SEC << endl;
	return 0;
}


// You either die a hero, or you live long enough to see yourself become the villain.

/**
 *   author:  Blind_is_love
 *   created: 15.10.2019, 00:32
**/

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace __gnu_pbds;
using namespace std;

typedef long long ll;

template <class T> ostream& operator << (ostream &os, const vector<T> &v) { for (T i : v) os << i << ' '; return os; }
template <class T> ostream& operator << (ostream &os, const set<T> &v) { for (T i : v) os << i << ' '; return os; }
template <class T, class S> ostream& operator << (ostream &os, const pair<T, S> &v) { os << v.first << ' ' << v.second; return os; }
template <class T, class S> ostream& operator << (ostream &os, const map<T, S> &v) { for (auto i : v) os << '(' << i.first << "=>" << i.second << ')' << ' '; return os; }
template <class T, class S> ostream& operator << (ostream &os, const unordered_map<T, S> &v) { for (auto i : v) os << '(' << i.first << "=>" << i.second << ')' << ' '; return os; }

#ifndef ONLINE_JUDGE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
    template <class Arg1> void __f(const char* name, Arg1&& arg1) { cerr << name << " : " << arg1 << endl; }
    template <class Arg1, class... Args>
    void __f(const char* names, Arg1&& arg1, Args&&... args) {
        const char* sep = strchr(names + 1, ',');
        cerr.write(names, sep - names) << " : " << arg1 << "  ";
        __f(sep + 1, args...);
    }
#else
#define trace(...) 0
#pragma GCC optimize ("O3")
#pragma GCC optimize ("unroll-loops")
#pragma GCC target("avx2,sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#define _CRT_SECURE_NO_WARNINGS
#endif // ifndef ONLINE_JUDGE

#define endl "\n"
#define vll vector<int>
#define max_heap priority_queue<int>
#define min_heap priority_queue<int , vector<int>, greater<int> >
#define pb push_back
#define all(container) container.begin(), container.end()
#define pll pair<ll,ll>
#define pii pair<int,int>
#define printArr(name,from,to) for(int x = from; x < to; x++) {cout << name[x] << " ";} cout << endl;
#define fi first
#define se second
#define mp make_pair
#define prime1 304933
#define prime2 15486277
#define mod2 179424691
#define int ll

typedef tree<ll, null_type, less<ll>, rb_tree_tag,tree_order_statistics_node_update> ordered_set;

const ll MOD = 1000000007;
const ll MAX = 2e5+10;
const double PI = 3.1415926;
const ll INF = 1e18;
const double EPS = 1e-5;

set<int> adj[MAX];
int n;

signed main(){
	ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
	clock_t t1, t2;
	t1 = clock();
	cin >> n;
	for(int i = 0 ; i < n-1; ++i){
		int x,y;
		cin >> x >> y;
		x--;
		y--;
		adj[x].insert(y);
		adj[y].insert(x);
	}
	queue<int> q;
	int child[n];
	memset(child,-1,sizeof(child));
	bool visited[n];
	memset(visited,false,sizeof(visited));

	for(int i = 0 ; i < n; ++i){
		if (adj[i].size()==1){
			q.push(i);
			visited[i] = true;
		}
	}

	int level = 1;
	int tot = n-1;

	while(!q.empty()){
		set<int> visit;
		while(!q.empty()){
			int s = q.front();
			q.pop();
			for(int x : adj[s]){
				if (child[s] == x) continue;
				if (visit.find(x) != visit.end()){
					adj[x].erase(s);
					tot -= level;
					continue;
				}
				// visited[x] = true;
				visit.insert(x);
				child[x] = s;
			}
		}

		// cout << "New leaves are: " << level++ << endl;
		level++;

		for(int x : visit){
			if (adj[x].size() == 2) {
				q.push(x);
				// cout << x+1 << endl;
			}

			if (adj[x].size() > 2 && visited[x]){
				cout << -1 << endl;
				return 0;
			}

			visited[x] = true;
		}
	}
	while (tot%2 == 0) tot/=2;
	cout << tot << endl;

	t2 = clock(); cerr << "time taken: " << (t2-t1)/(long double)CLOCKS_PER_SEC << endl;
	return 0;
}


// You either die a hero, or you live long enough to see yourself become the villain.

/**
 *   author:  Blind_is_love
 *   created: 15.10.2019, 00:32
**/

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace __gnu_pbds;
using namespace std;

typedef long long ll;

template <class T> ostream& operator << (ostream &os, const vector<T> &v) { for (T i : v) os << i << ' '; return os; }
template <class T> ostream& operator << (ostream &os, const set<T> &v) { for (T i : v) os << i << ' '; return os; }
template <class T, class S> ostream& operator << (ostream &os, const pair<T, S> &v) { os << v.first << ' ' << v.second; return os; }
template <class T, class S> ostream& operator << (ostream &os, const map<T, S> &v) { for (auto i : v) os << '(' << i.first << "=>" << i.second << ')' << ' '; return os; }
template <class T, class S> ostream& operator << (ostream &os, const unordered_map<T, S> &v) { for (auto i : v) os << '(' << i.first << "=>" << i.second << ')' << ' '; return os; }

#ifndef ONLINE_JUDGE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
    template <class Arg1> void __f(const char* name, Arg1&& arg1) { cerr << name << " : " << arg1 << endl; }
    template <class Arg1, class... Args>
    void __f(const char* names, Arg1&& arg1, Args&&... args) {
        const char* sep = strchr(names + 1, ',');
        cerr.write(names, sep - names) << " : " << arg1 << "  ";
        __f(sep + 1, args...);
    }
#else
#define trace(...) 0
#pragma GCC optimize ("O3")
#pragma GCC optimize ("unroll-loops")
#pragma GCC target("avx2,sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#define _CRT_SECURE_NO_WARNINGS
#endif // ifndef ONLINE_JUDGE

#define endl "\n"
#define vll vector<int>
#define max_heap priority_queue<int>
#define min_heap priority_queue<int , vector<int>, greater<int> >
#define pb push_back
#define all(container) container.begin(), container.end()
#define pll pair<ll,ll>
#define pii pair<int,int>
#define printArr(name,from,to) for(int x = from; x < to; x++) {cout << name[x] << " ";} cout << endl;
#define fi first
#define se second
#define mp make_pair
#define prime1 304933
#define prime2 15486277
#define mod2 179424691
#define int ll

typedef tree<ll, null_type, less<ll>, rb_tree_tag,tree_order_statistics_node_update> ordered_set;

const ll MOD = 1000000007;
const ll MAX = 2e5+10;
const double PI = 3.1415926;
const ll INF = 1e18;
const double EPS = 1e-5;

set<int> adj[MAX];
int n;

signed main(){
	ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
	clock_t t1, t2;
	t1 = clock();
	cin >> n;
	for(int i = 0 ; i < n-1; ++i){
		int x,y;
		cin >> x >> y;
		x--;
		y--;
		adj[x].insert(y);
		adj[y].insert(x);
	}
	queue<int> q;
	int child[n];
	memset(child,-1,sizeof(child));
	bool visited[n];
	memset(visited,false,sizeof(visited));

	for(int i = 0 ; i < n; ++i){
		if (adj[i].size()==1){
			q.push(i);
			visited[i] = true;
		}
	}

	int level = 1;
	int tot = n-1;

	while(!q.empty()){
		set<int> visit;
		while(!q.empty()){
			int s = q.front();
			q.pop();
			for(int x : adj[s]){
				if (child[s] == x) continue;
				if (visit.find(x) != visit.end()){
					adj[x].erase(s);
					tot -= level;
					continue;
				}
				// visited[x] = true;
				visit.insert(x);
				child[x] = s;
			}
		}

		// cout << "New leaves are: " << level++ << endl;
		level++;

		for(int x : visit){
			if (adj[x].size() == 2) {
				q.push(x);
				// cout << x+1 << endl;
			}

			if (adj[x].size() > 2 && visited[x]){
				cout << -1 << endl;
				return 0;
			}

			visited[x] = true;
		}
	}
	if (tot%2 == 0) tot/=2;
	cout << tot << endl;

	t2 = clock(); cerr << "time taken: " << (t2-t1)/(long double)CLOCKS_PER_SEC << endl;
	return 0;
}


/*
author: madhav_1999 aka orthodoxparadox
10 April 2020 at 5:59 AM
*/

#include <bits/stdc++.h>
#include <ext/pb_ds/detail/standard_policies.hpp>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;

#define dbl long double
#define int ll
#define ll long long
#define pii pair<int, int>
#define len(x) (int) x.size()
#define rev(a) reverse(all(a))
#define oiint ostream_iterator<int>(cout, " ")
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define endl '\n'
#define all(a) a.begin(), a.end()
#define initialise(a, x) memset(a, x, sizeof(a))
#define onlyunique(v) v.erase(unique(all(v)), v.end()); //only for sorted vector
#define inf 2e18
#pragma GCC optimize ("O3")
#pragma GCC optimize ("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#define _CRT_SECURE_NO_WARNINGS
#ifdef MADHAV
#define dbg(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1){cerr << name << " : " << arg1 << endl;}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args){
	const char* comma = strchr(names + 1, ',');
	cerr.write(names, comma - names) << " : " << arg1 << "  ";
	__f(comma + 1, args...);}
#else
#define dbg(...) 42
#endif
template <typename X> ostream& operator << (ostream& x,const vector<X>& v){for(int i=0;i<(int)v.size();++i) x<<v[i]<<" ";return x;}
template <typename X> ostream& operator << (ostream& x,const set<X>& v){for(auto it:v) x<<it<<" ";return x;}
template <typename X, typename Y> ostream& operator << (ostream& x, const pair<X,Y>& v) {x<<v.ff<<" "<<v.ss;return x;}
template <typename T, typename S> ostream& operator << (ostream& os, const map<T, S>& v) { for (auto it : v) os << it.first << "=>" << it.second << endl; return os; }
typedef tree <int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
struct pair_hash {inline std::size_t operator()(const std::pair<int, int> & v) const {return v.first*31+v.second;}};
const int MOD = 1e9 + 7;

int takemod(int a, int mod = MOD){a%=mod;if(a<0)a+=mod;return a;}

int fast_exp(int base, int expo, int mod = MOD) {
	int res=1;
	while(expo>0) {
		if(expo&1) res=(res*base)%mod;
		base=(base*base)%mod;
		expo>>=1;}
	return res;}

int modinv(int a, int mod = MOD){return takemod(fast_exp(takemod(a, mod), mod-2, mod), mod);}

const int N = 2e5 + 10;
vector<int> graph[N];
int maxi_d = 0;
int which = -1;
int deg[N];
int ans = 0;

void dist_dfs(int u, int p = -1, int d = 0) {
	if(d >= maxi_d) {
		which = u;
	}
	for(int &i : graph[u]) {
		if(i == p) continue;
		dist_dfs(i, u, d + 1);
	}
}

int dfs(int u, int p = -1) {
	vector<int> r;
	for(int &v : graph[u]) {
		if(v == p) continue;
		int x = dfs(v, u);
		r.push_back(x);
	}
	if(r.empty()) {
		return 1;
	}
	sort(all(r));
	onlyunique(r);
	if(r[0] == -1) {
		return -1;
	}
	if(p == -1) {
		if(len(r) > 2) return -1;
		if(len(r) == 2) {
			return r[0] + r[1];
		}
		return r[0];
	}
	if(len(r) > 1) return -1;
	return r[0] + 1;
}

void solve()
{
	int n;
	cin >> n;
	for(int i = 0; i < n - 1; i++) {
		int u, v;
		cin >> u >> v;
		graph[u].pb(v);
		graph[v].pb(u);
		deg[u]++; deg[v]++;
	}
	dist_dfs(1);
	int p = which;
	which = -1, maxi_d = 0;
	dist_dfs(p);
	int q = which;
	queue<int> v;
	for(int i = 1; i <= n; i++) {
		if(i == p or i == q) {
			continue;
		}
		if(deg[i] == 1) {
			v.push(i);
		}
	}
	while(!v.empty()) {
		int x = v.front();
		v.pop();
		for(int &y : graph[x]) {
			deg[y]--;
			if(deg[y] == 1) {
				v.push(y);
			}
		}
	}
	v.push(p); v.push(q);
	while(len(v) > 1) {
		int x = v.front();
		v.pop();
		for(int &y : graph[x]) {
			deg[y]--;
			if(deg[y] == 1) {
				v.push(y);
			}
		}
	}
	int tree_center = v.front();
	cout << dfs(tree_center);
}

signed main(){
	ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
	#ifdef MADHAV
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
	freopen("error.txt", "w", stderr);
	#endif
	clock_t t1, t2;
	t1 = clock();

	int tt = 1;
	// cin >> tt;
	while(tt--)
	{
		solve();
	}
	#ifdef MADHAV
	t2 = clock(); cout << "\n\ntime taken: " << (t2-t1)/(dbl)CLOCKS_PER_SEC;
	#endif
	return 0;
}


/*
author: madhav_1999 aka orthodoxparadox
10 April 2020 at 5:59 AM
*/

#include <bits/stdc++.h>
#include <ext/pb_ds/detail/standard_policies.hpp>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;

#define dbl long double
#define int ll
#define ll long long
#define pii pair<int, int>
#define len(x) (int) x.size()
#define rev(a) reverse(all(a))
#define oiint ostream_iterator<int>(cout, " ")
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define endl '\n'
#define all(a) a.begin(), a.end()
#define initialise(a, x) memset(a, x, sizeof(a))
#define onlyunique(v) v.erase(unique(all(v)), v.end()); //only for sorted vector
#define inf 2e18
#pragma GCC optimize ("O3")
#pragma GCC optimize ("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#define _CRT_SECURE_NO_WARNINGS
#ifdef MADHAV
#define dbg(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1){cerr << name << " : " << arg1 << endl;}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args){
	const char* comma = strchr(names + 1, ',');
	cerr.write(names, comma - names) << " : " << arg1 << "  ";
	__f(comma + 1, args...);}
#else
#define dbg(...) 42
#endif
template <typename X> ostream& operator << (ostream& x,const vector<X>& v){for(int i=0;i<(int)v.size();++i) x<<v[i]<<" ";return x;}
template <typename X> ostream& operator << (ostream& x,const set<X>& v){for(auto it:v) x<<it<<" ";return x;}
template <typename X, typename Y> ostream& operator << (ostream& x, const pair<X,Y>& v) {x<<v.ff<<" "<<v.ss;return x;}
template <typename T, typename S> ostream& operator << (ostream& os, const map<T, S>& v) { for (auto it : v) os << it.first << "=>" << it.second << endl; return os; }
typedef tree <int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
struct pair_hash {inline std::size_t operator()(const std::pair<int, int> & v) const {return v.first*31+v.second;}};
const int MOD = 1e9 + 7;

int takemod(int a, int mod = MOD){a%=mod;if(a<0)a+=mod;return a;}

int fast_exp(int base, int expo, int mod = MOD) {
	int res=1;
	while(expo>0) {
		if(expo&1) res=(res*base)%mod;
		base=(base*base)%mod;
		expo>>=1;}
	return res;}

int modinv(int a, int mod = MOD){return takemod(fast_exp(takemod(a, mod), mod-2, mod), mod);}

const int N = 2e5 + 10;
vector<int> graph[N];
int maxi_d = 0;
int which = -1;
int deg[N];
int ans = 0;

void dist_dfs(int u, int p = -1, int d = 0) {
	dbg(u, d);
	if(d >= maxi_d) {
		maxi_d = d;
		which = u;
	}
	for(int &i : graph[u]) {
		if(i == p) continue;
		dist_dfs(i, u, d + 1);
	}
}

int dfs(int u, int p = -1) {
	vector<int> r;
	for(int &v : graph[u]) {
		if(v == p) continue;
		int x = dfs(v, u);
		r.push_back(x);
	}
	if(r.empty()) {
		return 1;
	}
	sort(all(r));
	onlyunique(r);
	if(r[0] == -1) {
		return -1;
	}
	if(p == -1) {
		if(len(r) > 2) return -1;
		if(len(r) == 2) {
			return r[0] + r[1];
		}
		return r[0];
	}
	if(len(r) > 1) return -1;
	return r[0] + 1;
}

void solve()
{
	int n;
	cin >> n;
	for(int i = 0; i < n - 1; i++) {
		int u, v;
		cin >> u >> v;
		graph[u].pb(v);
		graph[v].pb(u);
		deg[u]++; deg[v]++;
	}
	dist_dfs(1);
	int p = which;
	cerr << '\n';
	which = -1, maxi_d = 0;
	dist_dfs(p);
	int q = which;
	dbg(q);
	queue<int> v;
	for(int i = 1; i <= n; i++) {
		if(i == p or i == q) {
			continue;
		}
		if(deg[i] == 1) {
			v.push(i);
		}
	}
	while(!v.empty()) {
		int x = v.front();
		v.pop();
		for(int &y : graph[x]) {
			deg[y]--;
			if(deg[y] == 1) {
				v.push(y);
			}
		}
	}
	v.push(p); v.push(q);
	while(len(v) > 1) {
		int x = v.front();
		v.pop();
		for(int &y : graph[x]) {
			deg[y]--;
			if(deg[y] == 1) {
				v.push(y);
			}
		}
	}
	int tree_center = v.front();
	dbg(tree_center);
	int res = dfs(tree_center);
	while(res % 2 == 0) {
		res/=2;
	}
	cout << res;
}

signed main(){
	ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
	#ifdef MADHAV
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
	freopen("error.txt", "w", stderr);
	#endif
	clock_t t1, t2;
	t1 = clock();

	int tt = 1;
	// cin >> tt;
	while(tt--)
	{
		solve();
	}
	#ifdef MADHAV
	t2 = clock(); cout << "\n\ntime taken: " << (t2-t1)/(dbl)CLOCKS_PER_SEC;
	#endif
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

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

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

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

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


#include <bits/stdc++.h>

using namespace std;

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

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

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

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

    }
    if(x==k)
    {
        for(auto u : val)
            all+=u;
    }
    viz[x]=0;
}

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


#include <bits/stdc++.h>

using namespace std;

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

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

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

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

    }
    if(x==k)
    {
        for(auto u : val)
            all+=u;
    }
    viz[x]=0;
}

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


#include <bits/stdc++.h>

using namespace std;

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

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

void topo()
{
    while(!st.empty())
    {
        int tp = st.front().first;
        int lvl = st.front().second;
        vis[tp]=1;
        st.pop();
        for(auto u : adj[tp])
        {
            if(!vis[u])
            {
                in[u]--;
                if( in[u]==1)
                {
                    if( met[u].second==-1 && (met[u].first==-1||met[u].first==lvl) )
                     {
                            st.push({u,lvl+1});
                            if(lvl==met[u].first)
                                all-=lvl;
                     }
                    else
                    {
                            if(met[u].first==-1)
                                met[u].first=lvl;
                            else if(met[u].second==-1)
                            {
                                met[u].second=lvl;
                                q++;
                            }
                            else if(met[u].first==lvl||met[u].second==lvl)
                                    all-=lvl;
                            else
                            {
                                printf("-1");
                                exit(0);
                            }
                    }
                }
                else if(in[u]==0)
                {
                    if(met[u].first==lvl||met[u].second==lvl)
                        all-=lvl;
                    else if(met[u].first==-1&&met[u].second==-1){}
                    else all=-1;
                    return;
                }
                else
                {
                    if(met[u].first==-1)
                        met[u].first=lvl;
                    else if(met[u].second==-1)
                    {
                        met[u].second=lvl;
                        q++;
                    }
                    else if(met[u].first==lvl||met[u].second==lvl)
                        all-=lvl;
                    else
                    {
                        printf("-1");
                        exit(0);
                    }
                }
            }
        }
    }
}

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