10
4 2
7 4
2 6
2 5
4 8
10 3
2 9
9 1
5 10
 10
5 10
7 8
8 3
2 6
3 2
9 7
4 5
10 1
6 4
 4
4 1
4 3
4 2
 3
3 1
1 2
 9
1 2
1 3
1 4
1 5
1 6
6 7
6 8
8 9
 5
1 5
2 3
2 4
1 2
 8
6 1
4 7
4 8
8 5
7 6
4 3
4 2
 7
5 6
5 7
5 1
7 4
6 3
3 2
 3
1 3
3 2
 2
1 2
 5
5 4
4 3
3 1
5 2
 6
1 6
3 1
6 4
5 3
2 5
 11
8 9
2 7
1 11
3 2
9 1
8 5
8 6
5 4
4 10
8 3
 11
11 9
6 7
7 1
8 11
5 6
3 5
9 3
10 8
2 4
4 10
 12
12 6
6 7
8 11
4 8
10 4
12 3
2 10
6 2
12 9
4 1
9 5

-1
 9
 1
 1
 3
 3
 -1
 -1
 1
 1
 1
 5
 1
 5
 -1

#include <bits/stdc++.h>
using namespace std;
template <typename T>
bool chkmax(T &x, T y) {
  return x < y ? x = y, true : false;
}
template <typename T>
bool chkmin(T &x, T y) {
  return x > y ? x = y, true : false;
}
int readint() {
  int x = 0, f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return x * f;
}
int n, tot, ans = 1 << 30;
int v[400005], nxt[400005], h[200005], mxd[200005], can[200005];
void addedge(int x, int y) {
  v[++tot] = y;
  nxt[tot] = h[x];
  h[x] = tot;
  v[++tot] = x;
  nxt[tot] = h[y];
  h[y] = tot;
}
void dfs1(int u, int fa) {
  can[u] = 1;
  int now = 0;
  for (int p = h[u]; p; p = nxt[p]) {
    if (v[p] == fa) continue;
    dfs1(v[p], u);
    if (!can[v[p]]) can[u] = 0;
    if (!now)
      now = mxd[v[p]];
    else if (now != mxd[v[p]])
      can[u] = 0, chkmax(now, mxd[v[p]]);
  }
  mxd[u] = now + 1;
}
void dfs2(int u, int fa) {
  int now1 = 0, now2 = 0, nc = 1, td = 1;
  vector<int> v1(0), v2(0), ct(0);
  for (int p = h[u]; p; p = nxt[p]) {
    if (!can[v[p]]) nc = 0, ct.push_back(v[p]);
    if (!now1)
      now1 = mxd[v[p]];
    else if (now1 != mxd[v[p]]) {
      if (!now2)
        now2 = mxd[v[p]];
      else if (now2 != mxd[v[p]]) {
        nc = 0, td = 0;
        break;
      }
    }
  }
  if (nc) chkmin(ans, now1 + now2);
  if (!td || ct.size() > 1) return;
  for (int p = h[u]; p; p = nxt[p]) {
    if (mxd[v[p]] == now1)
      v1.push_back(v[p]);
    else if (mxd[v[p]] == now2)
      v2.push_back(v[p]);
  }
  for (int p = h[u]; p; p = nxt[p]) {
    if (v[p] == fa) continue;
    if (mxd[v[p]] == now2) swap(now1, now2), swap(v1, v2);
    if (mxd[v[p]] == now1) {
      if (v1.size() == 1) {
        if (ct.size() > 0 && (ct.size() != 1 || ct[0] != v[p])) continue;
        int tmp1 = mxd[u], tmp2 = can[u];
        mxd[u] = now2 + 1, can[u] = 1;
        dfs2(v[p], u);
        mxd[u] = tmp1, can[u] = tmp2;
      } else if (v2.size() == 0) {
        if (ct.size() > 0 && (ct.size() != 1 || ct[0] != v[p])) continue;
        int tmp1 = mxd[u], tmp2 = can[u];
        mxd[u] = now1 + 1, can[u] = 1;
        dfs2(v[p], u);
        mxd[u] = tmp1, can[u] = tmp2;
      }
    }
  }
}
int main() {
  n = readint();
  for (int i = 1; i < n; i++) addedge(readint(), readint());
  dfs1(1, -1);
  dfs2(1, -1);
  if (ans == (1 << 30))
    printf("-1\n");
  else
    printf("%d\n", ans);
  return 0;
}
