#include <bits/stdc++.h>
#pragma GCC target("avx2")
#pragma GCC optimization("O2")
#pragma GCC optimization("unrolled-loops")
using namespace std;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const long long base = 1e9 + 7;
const long long oo = 1e6 + 10;
const long long gh = 1e3 + 3;
const long long inf = 1e15 + 5;
long long n, m, k, q;
pair<long long, long long> a[oo];
long long ok[oo];
pair<long long, long long> tt[oo];
void input() {
  cin >> n;
  for (long long i = 1; i <= n; i++) {
    cin >> a[i].first >> a[i].second;
  }
}
long long par[oo];
long long tree[oo];
long long fp(long long i) {
  if (par[i] == 0) return i;
  par[i] = fp(par[i]);
  return par[i];
}
long long guess[oo];
long long unite(long long x, long long y) {
  x = fp(x);
  y = fp(y);
  if (x == y) {
    if (tree[x] == guess[x])
      return 1;
    else {
      guess[x]++;
      return 0;
    }
  }
  par[x] = y;
  tree[x]++;
  guess[x]++;
  return 0;
}
bool DSUU(long long Q) {
  for (long long i = 1; i <= 2 * n; i++) {
    tree[i] = 1;
    guess[i] = 0;
    par[i] = 0;
  }
  long long ans = 0;
  for (long long i = 1; i <= n; i++) {
    long long x = a[i].first, y = a[i].second;
    if (x > Q) return 1;
    y = min(y, Q);
    if (unite(x, y)) return 1;
  }
  return 0;
}
void compress(vector<long long> &a) {
  set<long long> s(a.begin(), a.end());
  vector<long long> b(s.begin(), s.end());
  for (long long i = 0; i < a.size(); i++) {
    a[i] = lower_bound(b.begin(), b.end(), a[i]) - b.begin() + 1;
  }
}
long long ori[oo];
void solve() {
  vector<long long> b;
  for (long long i = 1; i <= n; i++) {
    b.push_back(a[i].first);
    b.push_back(a[i].second);
  }
  compress(b);
  long long cur = 0;
  for (long long i = 1; i <= n; i++) {
    ori[b[cur]] = a[i].first;
    a[i].first = b[cur];
    cur++;
    ori[b[cur]] = a[i].second;
    a[i].second = b[cur];
    cur++;
  }
  long long l = 0, r = 2 * n;
  while (l <= r) {
    long long mid = (l + r) / 2;
    if (DSUU(mid) == 0)
      r = mid - 1;
    else
      l = mid + 1;
  }
  if (l == 2 * n + 1)
    cout << -1;
  else
    cout << ori[l];
}
void ouput() {}
int32_t main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  if (ifstream("test.txt")) {
    freopen("test.txt", "r", stdin);
  } else if (ifstream(""
                      ".inp")) {
    freopen(
        ""
        ".inp",
        "r", stdin);
    freopen(
        ""
        ".out",
        "w", stdout);
  }
  long long t = 1;
  while (t--) {
    input();
    solve();
    ouput();
  }
}
