#include <bits/stdc++.h>
using namespace std;
struct node {
  int l, r;
} e[1000100];
int f[2000100];
struct ls {
  int x;
  int ix;
  int ty;
  friend bool operator<(ls a, ls b) { return a.x < b.x; }
} t[2000100];
int deg[2000100];
int siz[2000100];
int max_val[2000100];
int sub_val[2000100];
int tot_deg[2000100];
int fy[2000100];
int ff(int x) {
  if (f[x] == x) {
    return x;
  }
  while (f[x] != x) {
    f[x] = ff(f[x]);
    return f[x];
  }
}
void un(int x, int y) {
  int fx = ff(x);
  int fy = ff(y);
  if (fx == fy) return;
  f[fy] = fx;
}
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d%d", &e[i].l, &e[i].r);
    t[i * 2 - 1].ix = i;
    t[i * 2 - 1].x = e[i].l;
    t[i * 2 - 1].ty = 0;
    t[i * 2].ix = i;
    t[i * 2].x = e[i].r;
    t[i * 2].ty = 1;
  }
  sort(t + 1, t + 1 + n * 2);
  int ct = 0;
  for (int i = 1; i <= n * 2; i++) {
    if (t[i - 1].x != t[i].x) {
      ct++;
    }
    fy[ct] = t[i].x;
    if (t[i].ty) {
      e[t[i].ix].r = ct;
    } else {
      e[t[i].ix].l = ct;
    }
  }
  for (int i = 1; i <= ct; i++) {
    f[i] = i;
  }
  for (int i = 1; i <= n; i++) {
    un(e[i].l, e[i].r);
    deg[e[i].l]++;
    deg[e[i].r]++;
  }
  int ltc = 0;
  for (int i = 1; i <= ct; i++) {
    int g = ff(i);
    sub_val[g] = max_val[g];
    max_val[g] = fy[i];
    siz[g]++;
    tot_deg[g] += deg[i];
  }
  int ans = 0;
  for (int i = 1; i <= ct; i++) {
    if (siz[i] > 0) {
      ltc++;
      int ec = tot_deg[i] / 2;
      if (ec > siz[i]) {
        printf("-1\n");
        return 0;
      } else if (ec == siz[i]) {
        ans = max(ans, max_val[i]);
      } else {
        ans = max(ans, sub_val[i]);
      }
    }
  }
  printf("%d\n", ans);
  return 0;
}
