#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse2")
using namespace std;
long long n;
long long a[1000010];
long long b[1000010];
long long par[1000010];
long long cnt[1000010];
vector<long long> v;
void create_set() {
  for (long long i = 0; i < 1000010; i++) {
    par[i] = i;
    cnt[i] = 0;
  }
}
long long get_par(long long x) {
  return (par[x] == x ? x : par[x] = get_par(par[x]));
}
void compression() {
  stable_sort(v.begin(), v.end());
  v.resize(unique(v.begin(), v.end()) - v.begin());
  for (long long i = 0; i < n; i++) {
    a[i] = lower_bound(v.begin(), v.end(), a[i]) - v.begin();
    b[i] = lower_bound(v.begin(), v.end(), b[i]) - v.begin();
  }
}
bool can(long long md) {
  create_set();
  for (long long i = 0; i < n; i++) {
    if (md < a[i]) return 0;
    if (md < b[i]) {
      if (++cnt[get_par(a[i])] > 1) return 0;
    } else {
      if (get_par(a[i]) == get_par(b[i])) {
        if (++cnt[get_par(a[i])] > 1) return 0;
        continue;
      }
      cnt[get_par(b[i])] += cnt[get_par(a[i])];
      par[get_par(a[i])] = get_par(b[i]);
      if (cnt[get_par(b[i])] > 1) return 0;
    }
  }
  return 1;
}
signed main() {
  cin >> n;
  for (long long i = 0; i < n; i++) {
    cin >> a[i] >> b[i];
    v.push_back(a[i]);
    v.push_back(b[i]);
  }
  compression();
  long long lo = -1, hi = v.size();
  while (lo + 1 < hi) {
    long long md = (lo + hi) >> 1;
    if (can(md))
      hi = md;
    else
      lo = md;
  }
  v.push_back(-1);
  cout << v[hi] << endl;
  return 0;
}
