#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC optimize("no-stack-protector")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("unswitch-loops")
#pragma GCC optimize("fast-math")
#pragma GCC optimize("profile-values")
#pragma GCC optimize("tracer")
#pragma GCC optimize("vpt")
#pragma GCC optimize("rename-registers")
#pragma GCC optimize("move-loop-invariants")
#pragma GCC optimize("function-sections")
#pragma GCC optimize("data-sections")
#pragma GCC optimize("branch-target-load-optimize")
#pragma GCC optimize("branch-target-load-optimize2")
#pragma GCC optimize("btr-bb-exclusive")
#pragma GCC target("sse2")
#pragma GCC target("sse3")
#pragma GCC target("ssse3")
#pragma GCC target("sse4.1")
#pragma GCC target("sse4.2")
#pragma GCC target("avx")
#pragma GCC target("avx2")
#pragma GCC target("popcnt")
#pragma GCC target("abm")
#pragma GCC target("mmx")
#pragma GCC target("tune=native")
using namespace std;
int n;
vector<pair<int, int> > a;
bool check(int m) {
  vector<pair<int, int> > kek(n, pair<int, int>(-1, -1));
  for (int i = 0; i < n; i++) {
    if (a[i].first > m) return false;
    kek[i].first = a[i].first;
    if (a[i].second <= m) kek[i].second = a[i].second;
  }
  vector<pair<int, int> > comp;
  for (int i = 0; i < n; i++) {
    comp.push_back({kek[i].first, i * 2});
    if (kek[i].second != -1) comp.push_back({kek[i].second, i * 2 + 1});
  }
  sort((comp).begin(), (comp).end());
  vector<pair<int, int> > b(n, pair<int, int>(-1, -1));
  int last = 0, cur = n;
  for (int i = 0; i < (int)(comp).size(); i++) {
    if (comp[i].first != comp[last].first) {
      last = i;
      cur++;
    }
    int id = comp[i].second;
    if (id % 2)
      b[id / 2].second = cur;
    else
      b[id / 2].first = cur;
  }
  vector<set<int> > g(cur + 1);
  for (int i = 0; i < n; i++) {
    g[b[i].first].insert(i);
    g[i].insert(b[i].first);
    if (b[i].second == -1) continue;
    g[b[i].second].insert(i);
    g[i].insert(b[i].second);
  }
  queue<int> q;
  for (int i = 0; i < n; i++)
    if ((int)(g[i]).size() == 1) q.push(i);
  vector<bool> used(n, false);
  int ptr = 0;
  while (ptr < n) {
    int v, to;
    if ((int)(q).size()) {
      v = q.front();
      q.pop();
      if ((int)(g[v]).size() == 0) return false;
      to = *g[v].begin();
    } else {
      while (ptr < n && used[ptr]) ptr++;
      if (ptr == n) break;
      v = ptr;
      if ((int)(g[v]).size() == 0) return false;
      to = *g[v].begin();
      ptr++;
    }
    used[v] = true;
    for (int u : g[to]) {
      g[u].erase(to);
      if (!used[u] && (int)(g[u]).size() == 1) q.push(u);
    }
  }
  return (ptr == n);
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  a.resize(n);
  for (int i = 0; i < n; i++) cin >> a[i].first >> a[i].second;
  int l = 0, r = 1e9 + 10;
  while (l != r - 1) {
    int m = (l + r) / 2;
    if (check(m))
      r = m;
    else
      l = m;
  }
  if (r == 1e9 + 10)
    cout << -1;
  else
    cout << r;
  return 0;
}
