#include <bits/stdc++.h>
using namespace std;
int n, a, b, res = 2e9;
map<int, vector<int>> ways;
map<int, char> visited;
bool pos(map<int, vector<int>>::iterator it) {
  if (it == ways.end()) {
    res = min(res, (--visited.end())->first);
    return true;
  }
  if (visited[it->first]) return false;
  int pind = 0;
  for (int i = 1; i < it->second.size(); i++) {
    if (it->second[i] == it->second[i - 1]) {
      if (pind != 0) return false;
      pind = i;
    }
  }
  visited[it->first] = true;
  for (int i = 0; i < it->second.size(); i++) {
    if (i == pind) continue;
    visited[it->second[i]] = true;
  }
  auto ir = it;
  ir++;
  if (pos(ir)) return true;
  if (pind != 0) {
    visited[it->first] = false;
    for (int j = 0; j < it->second.size(); j++) {
      if (j != pind) visited[it->second[j]] = false;
    }
    return false;
  }
  for (int i = 1; i < it->second.size(); i++) {
    visited[it->second[i]] = false;
    visited[it->second[i - 1]] = true;
    if (pos(ir)) return true;
  }
  visited[it->first] = false;
  for (int j = 0; j < it->second.size(); j++) {
    if (j != pind) visited[it->second[j]] = false;
  }
  return false;
}
int main() {
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a >> b;
    ways[a].push_back(b);
  }
  for (auto it = ways.begin(); it != ways.end(); it++)
    sort(it->second.rbegin(), it->second.rend());
  if (pos(ways.begin()))
    cout << res;
  else
    cout << -1;
}
