#include <bits/stdc++.h>
using namespace std;
const int N = 1e6;
const int oo = 1e9 + 5;
int du[] = {-1, 0, 0, 1};
int dv[] = {0, -1, 1, 0};
const long long mod = 1e9 + 7;
long long sqr(long long x) { return x * x; }
int getbit(int state, int i) { return ((state >> i) & 1); }
int n, m, a[N + 5], b[N + 5], id1[N + 5], id2[N + 5], nedge, nvertex,
    dad[2 * N + 5];
bool used[2 * N + 5], ok[N + 5];
vector<int> d1;
vector<pair<int, int> > g[2 * N + 5];
void dfs(int u) {
  nvertex++;
  for (int i = 0; i < g[u].size(); i++) {
    int id = g[u][i].first, v = g[u][i].second;
    if (ok[i]) continue;
    ok[i] = true;
    nedge++;
    if (!used[v]) dfs(v);
  }
}
bool check(int x) {
  for (int i = 1; i <= n; i++)
    if (a[i] > x) return false;
  for (int i = 1; i <= n; i++) ok[i] = false;
  for (int i = 1; i <= m; i++) used[i] = false;
  int u, v, id;
  queue<int> q;
  for (int i = 1; i <= n; i++)
    if (a[i] <= x && b[i] > x) {
      if (used[id1[i]]) return false;
      ok[i] = true;
      used[id1[i]] = true;
      q.push(id1[i]);
    }
  while (!q.empty()) {
    int u = q.front();
    q.pop();
    for (int i = 0; i < g[u].size(); i++) {
      v = g[u][i].second;
      id = g[u][i].first;
      if (ok[id]) continue;
      if (used[v]) return false;
      used[v] = true;
      ok[id] = true;
      q.push(v);
    }
  }
  for (int i = 1; i <= n; i++)
    if (!ok[i]) {
      nedge = nvertex = 0;
      dfs(i);
      if (nedge > nvertex) return false;
    }
  return true;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i] >> b[i];
    d1.push_back(a[i]);
    d1.push_back(b[i]);
  }
  set<int> b1(d1.begin(), d1.end());
  vector<int> c1(b1.begin(), b1.end());
  for (int i = 1; i <= n; i++) {
    id1[i] = lower_bound(c1.begin(), c1.end(), a[i]) - c1.begin() + 1;
    id2[i] = lower_bound(c1.begin(), c1.end(), b[i]) - c1.begin() + 1;
    g[id1[i]].push_back(pair<int, int>(i, id2[i]));
    g[id2[i]].push_back(pair<int, int>(i, id1[i]));
  }
  m = c1.size();
  int l = c1[0], r = c1.back();
  while (l <= r) {
    int mid = (l + r) / 2;
    if (check(mid))
      r = mid - 1;
    else
      l = mid + 1;
  }
  if (l > c1.back())
    cout << -1;
  else
    cout << l;
  return 0;
}
