#include <bits/stdc++.h>
using namespace std;
map<int, int> par;
map<int, int> edges;
map<int, vector<int> > V;
string s = "   ";
vector<pair<int, int> > uu;
int find(int u) {
  if (par[u] == u)
    return u;
  else
    return par[u] = find(par[u]);
}
void add(int a, int b) {
  if (V[a].size() < V[b].size()) swap(a, b);
  a = find(a);
  b = find(b);
  edges[a]++;
  if (a == b)
    return;
  else {
    par[b] = par[a];
    edges[a] += edges[b];
    for (int i = 0; i < V[b].size(); i++) {
      if (find(V[a].begin(), V[a].end(), V[b][i]) == V[a].end()) {
        V[a].push_back(V[b][i]);
      }
    }
  }
}
map<int, bool> vis;
map<int, int> tt;
int main() {
  int n, a, b;
  int mx = 0;
  cin >> n;
  for (int i = 0; i < n; i++) {
    scanf("%d%d", &a, &b);
    uu.push_back(make_pair(a, b));
    edges[a] = 0;
    edges[b] = 0;
    if (par[a] == 0) V[a].push_back(a);
    if (par[b] == 0) V[b].push_back(b);
    par[a] = a;
    par[b] = b;
  }
  sort(uu.begin(), uu.end());
  for (int i = 2; i < n; i++) {
    if (uu[i].first == uu[i - 2].first && uu[i].second == uu[i - 2].second) {
      cout << -1;
      return 0;
    }
  }
  for (int i = 1; i < n; i++) {
    if (uu[i].first == uu[i - 1].first && uu[i].second == uu[i - 1].second) {
      tt[uu[i].first] += 1;
      tt[uu[i].second] += 1;
      if (tt[uu[i].second] > 1 || tt[uu[i].first] > 1) {
        cout << -1;
        return 0;
      }
      mx = max(mx, uu[i].second);
      edges[uu[i].first] = 1;
      edges[uu[i].second] = 1;
      uu[i].first = -1;
      uu[i].second = -1;
      uu[i - 1].first = -1;
      uu[i - 1].second = -1;
    }
  }
  for (int i = 0; i < n; i++) {
    add(uu[i].first, uu[i].second);
  }
  bool k = 1;
  for (int i = 0; i < n; i++) {
    int a = uu[i].first;
    int b = uu[i].second;
    if (a == -1) continue;
    int aa = find(a);
    int bb = find(b);
    if (!vis[aa]) {
      vis[aa] = 1;
      if (edges[aa] > V[aa].size()) {
        k = 0;
        break;
      }
      mx = max(mx, V[aa][edges[aa] - 1]);
    }
  }
  if (k) {
    cout << mx;
  } else {
    cout << -1;
  }
  return 0;
}
