#include <bits/stdc++.h>
using namespace std;
int n, m;
int a[1000010], b[1000010];
vector<int> q;
map<int, int> num;
int bnum[2000010];
vector<pair<int, int> > g[2000010];
vector<int> e[2000010];
char used[2000010];
char used_day[2000010];
char used_edge[1000010];
int cnt[1000010];
vector<int> comp;
int ed = 0, ver = 0;
void dfs(int v) {
  used_day[v] = true;
  ++ver;
  for (pair<int, int> to : g[v]) {
    if (!used_day[to.first]) {
      dfs(to.first);
    }
    if (!used_edge[to.second]) {
      ed++;
      used_edge[to.second] = true;
    }
  }
}
bool solve(int v) {
  queue<int> q;
  for (int i = 0; i < n; i++) {
    if (a[i] > v) {
      return false;
    }
    if (v < b[i]) {
      cnt[i] = 1;
      q.push(i);
    } else {
      cnt[i] = 2;
    }
  }
  memset(used, 0, sizeof(used));
  memset(used_day, 0, sizeof(used_day));
  while (!q.empty()) {
    int v = q.front();
    q.pop();
    int t;
    if (used_day[a[v]])
      t = b[v];
    else
      t = a[v];
    used[v] = true;
    used_day[t] = true;
    for (int i : e[t]) {
      if (!used[i]) {
        --cnt[i];
        if (cnt[i] == 0) return false;
        q.push(i);
      }
    }
  }
  memset(used_day, 0, sizeof(used_day));
  memset(used_edge, 0, sizeof(used_edge));
  for (int i = 0; i < m; i++) {
    if (!used_day[i]) {
      ed = 0, ver = 0;
      dfs(i);
      if (ed / 2 > ver) return false;
    }
  }
  return true;
}
int main() {
  scanf("%d", &n);
  for (int i = 0; i < n; i++) scanf("%d %d", a + i, b + i);
  for (int i = 0; i < n; i++) {
    q.push_back(a[i]);
    q.push_back(b[i]);
  }
  sort(q.begin(), q.end());
  q.resize(unique(q.begin(), q.end()) - q.begin());
  for (int u : q) {
    bnum[m] = u;
    num[u] = m++;
  }
  for (int i = 0; i < n; i++) {
    a[i] = num[a[i]];
    b[i] = num[b[i]];
    e[a[i]].push_back(i);
    e[b[i]].push_back(i);
    g[a[i]].emplace_back(b[i], i);
    g[b[i]].emplace_back(a[i], i);
  }
  int lo = 0, hi = m;
  while (lo != hi) {
    int mi = (lo + hi) >> 1;
    if (solve(mi))
      hi = mi;
    else
      lo = mi + 1;
  }
  if (lo == m)
    puts("-1");
  else
    printf("%d\n", bnum[lo]);
  return 0;
}
