#include <bits/stdc++.h>
using namespace std;
const int ML = 2000100;
int fa[ML], ed[ML], po[ML], m1[ML], m2[ML];
void CSH(int p, int pp) {
  fa[p] = p;
  ed[p] = 0;
  po[p] = 1;
  m1[p] = pp;
  m2[p] = 0;
}
int ufind(int p) {
  if (fa[p] == p)
    return p;
  else
    return fa[p] = ufind(fa[p]);
}
map<int, int> f;
bool cmP(int i, int j) { return i > j; }
int main() {
  int n;
  scanf("%d", &n);
  int tp = 0;
  for (int i = 0; i < n; i++) {
    int u, v;
    scanf("%d%d", &u, &v);
    int &pu = f[u], &pv = f[v];
    if (pu == 0) pu = ++tp, CSH(pu, u);
    if (pv == 0) pv = ++tp, CSH(pv, v);
    int ru = ufind(pu), rv = ufind(pv);
    if (ru == rv)
      ed[ru]++;
    else {
      ed[ru] += ed[rv] + 1;
      po[ru] += po[rv];
      fa[rv] = ru;
      int a[4];
      a[0] = m1[ru], a[1] = m2[ru], a[2] = m1[rv], a[3] = m2[rv];
      sort(a + 0, a + 4, cmP);
      m1[ru] = a[0];
      m2[ru] = a[1];
    }
  }
  int ans = 0;
  for (int i = 1; i <= tp; i++)
    if (fa[i] == i) {
      if (ed[i] == po[i]) ans = max(ans, m1[i]);
      if (ed[i] == po[i] - 1) ans = max(ans, m2[i]);
      if (ed[i] > po[i]) {
        ans = -1;
        break;
      }
    }
  cout << ans;
  return 0;
}
