#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e6 + 11;
int n, a[maxn], b[maxn], par[maxn], cnt[maxn], siz[maxn], ff[maxn], ss[maxn];
vector<int> v;
int find(int p) {
  if (p == par[p]) return p;
  return par[p] = find(par[p]);
}
void merge(int p, int q) {
  int pid = find(p);
  int qid = find(q);
  assert(pid != qid);
  if (siz[pid] < siz[qid]) {
    siz[qid] += siz[pid];
    cnt[qid] += cnt[pid] - 1;
    par[pid] = qid;
  } else {
    siz[pid] += siz[qid];
    cnt[pid] += cnt[qid] - 1;
    par[qid] = pid;
  }
}
void init() {
  iota(par, par + maxn, 0);
  fill(cnt, cnt + maxn, 1);
  fill(siz, siz + maxn, 1);
}
bool check(int mx) {
  init();
  for (int i = 0; i < n; i++) {
    int f = ff[i];
    int s = ss[i];
    if (a[i] > mx) return false;
    if (b[i] > mx) {
      if (cnt[find(f)] == 0) return false;
      cnt[find(f)]--;
      continue;
    }
    if (cnt[find(f)] == 0 and cnt[find(s)] == 0) return false;
    if (find(f) == find(s)) {
      if (cnt[find(f)] == 0) return false;
      cnt[find(f)]--;
      continue;
    }
    merge(f, s);
  }
  return true;
}
int32_t main() {
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i] >> b[i];
    v.emplace_back(a[i]);
    v.emplace_back(b[i]);
  }
  sort(v.begin(), v.end());
  v.resize(unique(v.begin(), v.end()) - v.begin());
  for (int i = 0; i < n; i++) {
    ff[i] = lower_bound(v.begin(), v.end(), a[i]) - v.begin();
    ss[i] = lower_bound(v.begin(), v.end(), b[i]) - v.begin();
  }
  int lo = 0, hi = (int)(v).size();
  int mid;
  if (!check(v.back())) {
    cout << -1 << endl;
    return 0;
  }
  while (hi - lo > 1) {
    mid = hi + lo >> 1;
    if (check(v[mid]))
      hi = mid;
    else
      lo = mid;
  }
  cout << v[hi] << endl;
}
