#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e6 + 10;
int n, m;
int l[maxn], r[maxn];
vector<int> grafo[maxn];
multiset<int> deltas;
struct DSU {
  int pai[maxn], peso[maxn];
  set<int> st[maxn];
  void init(void) {
    for (int i = 1; i <= m; i++) {
      pai[i] = i, peso[i] = 1;
      for (auto v : grafo[i]) st[i].insert(v);
      deltas.insert(1 - st[i].size());
    }
  }
  int Find(int x) {
    if (pai[x] == x) return x;
    return pai[x] = Find(pai[x]);
  }
  void Merge(int x, int y) {
    if (st[x].size() < st[y].size()) {
      for (auto v : st[x]) st[y].insert(v);
      swap(st[x], st[y]);
    } else {
      for (auto v : st[y]) st[x].insert(v);
    }
  }
  void Join(int x, int y) {
    x = Find(x), y = Find(y);
    if (x == y) return;
    deltas.erase(deltas.find(peso[y] - st[y].size()));
    deltas.erase(deltas.find(peso[x] - st[x].size()));
    if (peso[x] < peso[y]) swap(x, y);
    pai[y] = x, peso[x] += peso[y];
    Merge(x, y);
    deltas.insert(peso[x] - st[x].size());
  }
} dsu;
int back[maxn];
void compress(void) {
  map<int, int> mp;
  for (int i = 1; i <= n; i++) mp[l[i]] = 0, mp[r[i]] = 0;
  for (auto &x : mp) x.second = ++m;
  for (int i = 1; i <= n; i++) {
    back[mp[l[i]]] = l[i];
    l[i] = mp[l[i]];
    back[mp[r[i]]] = r[i];
    r[i] = mp[r[i]];
  }
}
int main(void) {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) scanf("%d %d", &l[i], &r[i]);
  compress();
  for (int i = 1; i <= n; i++) {
    grafo[l[i]].push_back(i);
    grafo[r[i]].push_back(i);
  }
  dsu.init();
  for (int i = 1; i <= m; i++) {
    for (auto v : grafo[i])
      if (l[v] < i) dsu.Join(i, l[v]);
    if (*deltas.begin() >= 0) {
      printf("%d\n", back[i]);
      return 0;
    }
  }
  printf("-1\n");
}
