#include <bits/stdc++.h>
using namespace std;
const int nmax = 2e6 + 5;
const long long mod = 1e9 + 7;
unordered_map<int, int> zip;
vector<int> g[nmax];
int n, hui, R[nmax], unzip[nmax], cycles, X, nods, edgs, deg[nmax];
bool vis[nmax];
vector<pair<int, int> > v;
vector<pair<int, int> > L;
vector<int> curnodes;
void dfs(int u, int par) {
  if (vis[u]) return;
  vis[u] = true, nods++, curnodes.push_back(u), edgs += deg[u];
  for (auto v : g[u])
    if (v != par) dfs(v, u);
}
int secondlargest(vector<int> &v) {
  int x = *max_element(v.begin(), v.end());
  for (int i = 0; i < v.size(); i++)
    if (v[i] == x) v[i] = 0;
  return *max_element(v.begin(), v.end());
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> n;
  zip.reserve(2 * n);
  zip.max_load_factor(0.25);
  L.resize(n);
  for (int i = 0; i < n; i++) {
    cin >> L[i].first >> R[i];
    L[i].second = i;
    v.push_back({L[i].first, L[i].second});
    v.push_back({R[i], -1});
  }
  sort(v.begin(), v.end());
  sort(L.begin(), L.end());
  for (int i = 0; i < 2 * n; i++) {
    X = v[i].first;
    if (zip.find(X) == zip.end()) zip[X] = ++hui, unzip[hui] = X;
  }
  for (int i = 0; i < n; i++) {
    int idx = L[i].second;
    int u = zip[L[i].first], v = zip[R[L[i].second]];
    g[u].push_back(v);
    g[v].push_back(u);
    deg[u]++, deg[v]++;
  }
  int ans = 0;
  for (int i = 1; i <= hui; i++) {
    if (!vis[i]) {
      nods = 0, edgs = 0, curnodes.clear();
      dfs(i, 0);
      edgs /= 2;
      int pera;
      if (nods < edgs) return cout << "-1", 0;
      if (nods == edgs)
        pera = *max_element(curnodes.begin(), curnodes.end());
      else
        pera = secondlargest(curnodes);
      ans = max(ans, pera);
    }
  }
  cout << unzip[ans];
  return 0;
}
