#include <bits/stdc++.h>
using namespace std;
char gc() {
  static char buf[100000], *p1 = buf, *p2 = buf;
  return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 100000, stdin), p1 == p2)
             ? EOF
             : *p1++;
}
long long read() {
  char ch = getchar();
  long long x = 0;
  int op = 1;
  for (; !isdigit(ch); ch = getchar())
    if (ch == '-') op = -1;
  for (; isdigit(ch); ch = getchar()) x = (x << 1) + (x << 3) + ch - '0';
  return x * op;
}
int n, sl, q[2000005], nq[2000005], t[2000005];
struct node {
  int x, y;
} a[2000005];
map<pair<int, int>, bool> mp;
bool check(int lim) {
  for (int i = (1); i <= (n); i++)
    if (a[i].x > lim) return 0;
  bool flag = 1;
  for (int i = (1); i <= (n); i++)
    if (a[i].y > lim) {
      if (t[a[i].x]) flag = 0;
      t[a[i].x]++;
    }
  if (!flag) {
    for (int i = (1); i <= (sl); i++) t[i] = 0;
    return 0;
  }
  for (int i = (1); i <= (n); i++)
    if (a[i].y <= lim) {
      if (t[a[i].x] && t[a[i].y])
        flag = 0;
      else if (t[a[i].x])
        t[a[i].y]++;
      else if (t[a[i].y])
        t[a[i].x]++;
    }
  if (!flag) {
    for (int i = (1); i <= (sl); i++) t[i] = 0;
    return 0;
  }
  return 1;
}
int main() {
  n = read();
  int x, y;
  for (int i = (1); i <= (n); i++) {
    q[++sl] = a[i].x = read(), q[++sl] = a[i].y = read();
    if (a[i].x > a[i].y) swap(a[i].x, a[i].y);
    if (mp.count(pair<int, int>(a[i].x, a[i].y)))
      return puts("-1"), 0;
    else
      mp[pair<int, int>(a[i].x, a[i].y)] = 1;
  }
  sort(&q[1], &q[sl + 1]);
  sl = unique(&q[1], &q[sl + 1]) - q - 1;
  for (int i = (1); i <= (n); i++) {
    a[i].x = lower_bound(&q[1], &q[sl + 1], a[i].x) - q;
    a[i].y = lower_bound(&q[1], &q[sl + 1], a[i].y) - q;
  }
  int l = 1, r = sl, mid;
  while (l <= r) {
    mid = l + r >> 1;
    if (check(mid))
      r = mid - 1;
    else
      l = mid + 1;
  }
  printf("%d\n", q[r + 1]);
  return 0;
}
