#include <bits/stdc++.h>
using namespace std;
struct apple {
  int data, no, has;
} a[2001011 * 2];
int ff[2001011], father[2001011], dian_sum[2001011], bian_sum[2001011],
    maxx[2001011], sub_maxx[2001011], n, m, ans;
bool cmp1(apple aa, apple bb) { return aa.data < bb.data; }
bool cmp2(apple aa, apple bb) { return aa.no < bb.no; }
int find(int u) {
  if (u == father[u]) return u;
  father[u] = find(father[u]);
  return father[u];
}
void put_together(int x, int y) {
  int fa, fb;
  fa = find(x);
  fb = find(y);
  if (fa == fb) {
    bian_sum[fa]++;
    return;
  } else {
    father[fa] = fb;
    dian_sum[fb] += dian_sum[fa];
    bian_sum[fb] += bian_sum[fa] + 1;
    if (maxx[fa] > maxx[fb]) {
      sub_maxx[fb] = maxx[fb];
      maxx[fb] = maxx[fa];
    } else {
      sub_maxx[fa] = maxx[fa];
    }
    if (sub_maxx[fa] > sub_maxx[fb]) {
      sub_maxx[fb] = sub_maxx[fa];
    }
  }
}
int main() {
  int x, y;
  scanf("%d", &m);
  for (int i = 1; i <= m; i++) {
    scanf("%d%d", &a[i * 2 - 1].data, &a[i * 2].data);
    a[i * 2 - 1].no = i * 2 - 1;
    a[i * 2].no = i * 2;
  }
  sort(a + 1, a + m * 2 + 1, cmp1);
  a[0].data = -1;
  for (int i = 1; i <= m * 2; i++) {
    if (a[i].data == a[i - 1].data)
      a[i].has = n;
    else
      a[i].has = ++n;
  }
  sort(a + 1, a + m * 2 + 1, cmp2);
  for (int i = 1; i <= m * 2; i++) {
    ff[a[i].has] = a[i].data;
  }
  for (int i = 1; i <= n; i++) {
    father[i] = i;
    dian_sum[i] = 1;
    maxx[i] = ff[i];
  }
  for (int i = 1; i <= m; i++) {
    x = a[i * 2 - 1].has;
    y = a[i * 2].has;
    put_together(x, y);
  }
  for (int i = 1; i <= n; i++) {
    if (i == father[i]) {
      if (bian_sum[i] > dian_sum[i]) {
        printf("-1");
        return 0;
      }
      if (bian_sum[i] == dian_sum[i]) {
        ans = max(ans, maxx[i]);
      } else {
        ans = max(ans, sub_maxx[i]);
      }
    }
  }
  printf("%d", ans);
  return 0;
}
