#include <bits/stdc++.h>
using namespace std;
vector<int> uniqueverts;
int n, mpc;
int u[1000005], v[1000005];
vector<int> e[2000005];
bool w[2000005];
int cvorova, grana, limiter;
void dfs(int x) {
  cvorova++;
  w[x] = 1;
  for (int y : e[x]) {
    if (y > x) grana++;
    if (y <= limiter) {
      if (!w[y]) dfs(y);
    }
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  cerr.tie(nullptr);
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> u[i] >> v[i];
    uniqueverts.push_back(u[i]);
    uniqueverts.push_back(v[i]);
  }
  sort(uniqueverts.begin(), uniqueverts.end());
  int rs = unique(uniqueverts.begin(), uniqueverts.end()) - uniqueverts.begin();
  uniqueverts.resize(rs);
  for (int i = 0; i < n; i++) {
    u[i] = lower_bound(uniqueverts.begin(), uniqueverts.end(), u[i]) -
           uniqueverts.begin();
    v[i] = lower_bound(uniqueverts.begin(), uniqueverts.end(), v[i]) -
           uniqueverts.begin();
    e[u[i]].push_back(v[i]);
    e[v[i]].push_back(u[i]);
  }
  int l = 0, r = rs - 1, o = -1;
  while (l <= r) {
    int m = (l + r) / 2;
    limiter = m;
    fill(w, w + rs, false);
    int mecovano = 0;
    for (int i = 1; i <= limiter; i++)
      if (!w[i]) {
        cvorova = grana = 0;
        dfs(i);
        if (cvorova - 1 == grana) {
          mecovano += cvorova - 1;
        } else {
          mecovano += cvorova;
        }
      }
    if (mecovano == n) {
      o = m;
      r = m - 1;
    } else {
      l = m + 1;
    }
  }
  if (o == -1) return cout << -1, 0;
  cout << uniqueverts[o] << '\n';
}
