#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 233;
void read(int &x) {
  int f = 1;
  x = 0;
  char ch = getchar();
  while (!isdigit(ch)) (ch == '-' ? f = -1 : 0), ch = getchar();
  while (isdigit(ch)) x = x * 10 + ch - '0', ch = getchar();
  x *= f;
  return;
}
struct xx {
  int l, r;
  xx() {}
  xx(int a, int b) { l = a, r = b; }
  int operator<(const xx &A) const { return l > A.l || l == A.l && r > A.r; }
  int operator==(const xx &A) const { return l == A.l && r == A.r; }
};
priority_queue<xx> q;
int n;
map<int, int> g;
int ans, l[N], r[N];
signed main() {
  read(n);
  for (int i = 1; i <= n; ++i) read(l[i]), read(r[i]), q.push((xx){l[i], r[i]});
  while (!q.empty()) {
    xx t1 = q.top();
    q.pop();
    if (q.empty()) {
      if (g.count(t1.l)) {
        if (g.count(t1.r)) return printf("-1"), 0;
        ans = t1.r;
      } else {
        ans = t1.l;
      }
      break;
    }
    xx t2 = q.top();
    q.pop();
    if (t1 == t2) {
      if (g.count(t1.l)) return printf("-1"), 0;
      if (g.count(t1.r)) return printf("-1"), 0;
      g[t1.l] = 1;
      g[ans = t1.r] = 1;
      continue;
    }
    if (t1.l == t2.l) {
      if (g.count(t1.l)) return printf("-1"), 0;
      g[ans = t1.l] = 1;
      int x = min(t1.r, t2.r);
      int y = max(t1.r, t2.r);
      q.push(xx(x, y));
      continue;
    }
    if (g.count(t1.l)) return printf("-1"), 0;
    q.push(t2);
    g[t1.l] = 1;
    ans = t1.l;
  }
  printf("%d", ans);
  return 0;
}
