#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 5;
int n;
vector<pair<int, int> > V;
bool have[N << 1];
vector<multiset<int> > g(N << 1);
bool f(int m) {
  memset(have, 0, sizeof have);
  g.clear();
  g.resize(N << 1);
  for (int i = n - 1; i > m; --i) {
    if (have[V[i].first] || V[m].second < V[i].first) return false;
    have[V[i].first] = true;
  }
  for (int i = 0; i <= m; ++i)
    g[V[i].first].emplace(V[i].second), g[V[i].second].emplace(V[i].first);
  queue<int> Q;
  for (int i = 0; i < 2 * N; ++i)
    if (have[i] and g[i].size()) {
      for (auto first : g[i]) Q.emplace(first);
      g[i].clear();
    }
  while (!Q.empty()) {
    int now = Q.front();
    Q.pop();
    if (have[now]) return false;
    have[now] = true;
    for (auto first : g[now]) Q.push(first);
    g[now].clear();
  }
  for (int i = 0; i < 2 * N; ++i)
    if (g[i].size() == 1) Q.emplace(i);
  while (!Q.empty()) {
    auto now = Q.front();
    Q.pop();
    if (g[now].size() == 0) continue;
    g[*g[now].begin()].erase(g[*g[now].begin()].find(now));
    if (g[*g[now].begin()].size() == 1) Q.emplace(*g[now].begin());
    g[now].clear();
  }
  for (int i = 0; i < 2 * N; ++i)
    if (g[i].size() != 0 && g[i].size() != 2) return false;
  return true;
}
int main() {
  scanf("%d", &n);
  vector<int> num;
  for (int i = 1, a, b; i <= n; ++i) {
    scanf("%d %d", &a, &b);
    if (n == 1) return !printf("%d\n", a);
    num.emplace_back(a), num.emplace_back(b);
    V.emplace_back(a, b);
  }
  sort(num.begin(), num.end());
  num.resize(unique(num.begin(), num.end()) - num.begin());
  for (auto &first : V) {
    first.first =
        upper_bound(num.begin(), num.end(), first.first) - num.begin();
    first.second =
        upper_bound(num.begin(), num.end(), first.second) - num.begin();
  }
  sort(V.begin(), V.end(),
       [](const pair<int, int> &a, const pair<int, int> &b) {
         return a.second < b.second;
       });
  int l = 0, r = n;
  while (l < r) {
    int m = (l + r) >> 1;
    if (f(m))
      r = m;
    else
      l = m + 1;
  }
  if (r == n) return !printf("%d\n", -1);
  printf("%d\n", num[V[r].second - 1]);
}
