#include <bits/stdc++.h>
using namespace std;
const int N = 1000002;
int n;
vector<int> g[N * 2];
int E[N][2];
vector<int> L;
struct TDSU {
  int f[N * 2], c[N * 2], m1[N * 2], m2[N * 2];
  void init() {
    for (int i = 0; i < n + n; ++i) {
      f[i] = -1;
      m1[i] = L[i];
      m2[i] = 0;
    }
  }
  int root(int u) {
    if (f[u] < 0) return u;
    return f[u] = root(f[u]);
  }
  bool merge(int u, int v) {
    u = root(u);
    v = root(v);
    if (u == v) {
      ++c[u];
      return false;
    }
    if (f[u] > f[v]) swap(u, v);
    f[u] += f[v];
    f[v] = u;
    c[u] = c[u] + c[v] + 1;
    if (m1[v] > m1[u]) {
      m2[u] = max(m1[u], m2[v]);
      m1[u] = m1[v];
    } else {
      m2[u] = max(m2[u], m1[v]);
    }
    return true;
  }
} dsu;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  L.reserve(2 * n);
  for (int i = 1; i <= n; ++i) {
    cin >> E[i][0] >> E[i][1];
    L.push_back(E[i][0]);
    L.push_back(E[i][1]);
  }
  sort(L.begin(), L.end());
  dsu.init();
  for (int i = 1; i <= n; ++i) {
    E[i][0] = lower_bound(L.begin(), L.end(), E[i][0]) - L.begin();
    E[i][1] = lower_bound(L.begin(), L.end(), E[i][1]) - L.begin();
    dsu.merge(E[i][0], E[i][1]);
  }
  int ret = 0;
  bool possible = true;
  for (int i = 0; i < n + n; ++i) {
    if (dsu.root(i) == i) {
      if ((-dsu.f[i]) - 1 == dsu.c[i])
        ret = max(ret, dsu.m2[i]);
      else if (-dsu.f[i] == dsu.c[i])
        ret = max(ret, dsu.m1[i]);
      else
        possible = false;
    }
  }
  if (possible)
    cout << ret;
  else
    cout << -1;
}
