#include <bits/stdc++.h>
using namespace std;
const int N = 3123456;
int vis[N], a[N], b[N], x[N], y[N], usado[N], tem[N];
vector<int> g[N], adj[N];
int edge, vertex;
void dfs(int u) {
  vis[u] = true;
  vertex++;
  edge += g[u].size();
  for (auto v : g[u])
    if (!vis[v]) dfs(v);
}
int main(void) {
  int n;
  scanf("%d", &n);
  for (int i = 0; i < n; i++) scanf("%d %d", a + i, b + i);
  int lo = 0, hi = 1000000000, ans = -1;
  vector<pair<int, int> > evt;
  vector<int> tudo;
  for (int i = 0; i < n; i++) {
    evt.emplace_back(a[i], i * 2);
    evt.emplace_back(b[i], i * 2 + 1);
    tudo.push_back(a[i]);
    tudo.push_back(b[i]);
  }
  sort(tudo.begin(), tudo.end());
  for (int i = 0; i < n; i++) {
    x[i] = lower_bound(tudo.begin(), tudo.end(), a[i]) - tudo.begin();
    y[i] = lower_bound(tudo.begin(), tudo.end(), b[i]) - tudo.begin();
    adj[x[i]].push_back(i);
    adj[y[i]].push_back(i);
  }
  sort(evt.rbegin(), evt.rend());
  while (lo <= hi) {
    int mid = (lo + hi) / 2;
    memset(tem, 0, sizeof tem);
    memset(usado, 0, sizeof usado);
    for (int i = 0; i < 2 * n; i++) g[i].clear();
    memset(vis, 0, sizeof vis);
    bool flag = true;
    for (int i = 0; i < n and flag; i++)
      if (a[i] > mid and b[i] > mid) flag = false;
    queue<int> q;
    for (int i = 0; i < n and flag; i++) {
      int tempo, p;
      tie(tempo, p) = evt[i];
      int id = p & 1;
      p /= 2;
      if (tempo <= mid) break;
      q.push(x[i]);
      if (usado[x[i]]) flag = false;
      usado[x[i]] = true;
      tem[p] = 1;
    }
    while (!q.empty() and flag) {
      int tempo = q.front();
      q.pop();
      for (auto v : adj[tempo]) {
        if (tem[v]) continue;
        if (x[v] == tempo) {
          if (usado[y[v]] or b[v] > mid) flag = false;
          q.push(y[v]);
          tem[v] = true;
          usado[y[v]] = true;
        } else {
          if (usado[x[v]]) flag = false;
          q.push(x[v]);
          tem[v] = true;
          usado[x[v]] = true;
        }
      }
    }
    if (!flag) {
      lo = mid + 1;
      continue;
    }
    for (int i = 0; i < n; i++) {
      if (tem[i]) continue;
      g[x[i]].push_back(y[i]);
      g[y[i]].push_back(x[i]);
    }
    for (int j = 0; j < 2 * n and flag; j++) {
      if (vis[j]) continue;
      edge = 0;
      vertex = 0;
      dfs(j);
      edge /= 2;
      if (edge > vertex) flag = false;
    }
    if (flag) {
      ans = mid;
      hi = mid - 1;
    } else
      lo = mid + 1;
  }
  cout << ans << endl;
  return 0;
}
