#include <bits/stdc++.h>
using namespace std;
int n;
struct data {
  int rv, v, id;
} dt[2000005];
int cmp(data q, data w) { return q.rv < w.rv; }
int cmpb(data q, data w) {
  if (q.id == w.id) return q.v < w.v;
  return q.id < w.id;
}
int rw[2000005];
int f[2000005];
int p[2000005][2];
int findfa(int x) {
  if (f[x] == x) return x;
  f[x] = findfa(f[x]);
  return f[x];
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d%d", &dt[2 * i - 1].rv, &dt[2 * i].rv);
    dt[2 * i - 1].id = dt[2 * i].id = i;
  }
  sort(dt + 1, dt + 2 * n + 1, cmp);
  for (int i = 1; i <= 2 * n; i++) {
    if (dt[i].rv == dt[i - 1].rv)
      dt[i].v = dt[i - 1].v;
    else
      dt[i].v = dt[i - 1].v + 1, rw[dt[i].v] = dt[i].rv;
  }
  for (int i = 1; i <= dt[2 * n].v; i++) f[i] = i;
  sort(dt + 1, dt + 2 * n + 1, cmpb);
  for (int i = 1; i <= 2 * n; i++) p[(i + 1) / 2][!(i & 1)] = dt[i].v;
  int ans = -1;
  for (int i = 1; i <= n; i++) {
    int f0 = findfa(p[i][0]), f1 = findfa(p[i][1]);
    if ((!f0) && (!f1)) {
      printf("-1");
      return 0;
    }
    if (f0 > f1) swap(f0, f1);
    if ((f0 == f1) || (!f0) || (!f1)) {
      ans = max(ans, rw[f0]);
      f[f0] = f[f1] = 0;
    } else {
      ans = max(ans, rw[f0]);
      f[f0] = f1;
    }
  }
  printf("%d", ans);
  return 0;
}
