#include <bits/stdc++.h>
using namespace std;
const int kMaxn = 1e6 + 10;
int n, m, ecnt, cnte, cntv;
bool visv[kMaxn << 1], vise[kMaxn << 1];
int hed[kMaxn << 1], b[kMaxn << 1];
pair<int, int> a[kMaxn];
struct Edge {
  int ed, id, nxt;
};
Edge e[kMaxn << 1];
inline void AddEdge(int u, int v, int id) {
  e[ecnt] = (Edge){v, id, hed[u]};
  hed[u] = ecnt++;
}
bool DFS1(int u) {
  if (visv[u]) {
    return 0;
  }
  visv[u] = 1;
  for (int i = hed[u]; ~i; i = e[i].nxt) {
    int v = e[i].ed;
    if (vise[i]) {
      continue;
    }
    vise[i] = vise[i ^ 1] = 1;
    if (!DFS1(v)) {
      return 0;
    }
  }
  return 1;
}
void DFS2(int u) {
  ++cntv;
  visv[u] = 1;
  for (int i = hed[u]; ~i; i = e[i].nxt) {
    int v = e[i].ed;
    if (vise[i]) {
      continue;
    }
    vise[i] = vise[i ^ 1] = 1;
    ++cnte;
    if (!visv[v]) {
      DFS2(v);
    }
  }
}
bool Check(int x) {
  fill(visv + 1, visv + m + 1, 0);
  fill(vise + 0, vise + ecnt, 0);
  for (int i = x + 1; i <= m; ++i) {
    if (visv[i] || !DFS1(i)) {
      return 0;
    }
  }
  for (int i = 1; i <= m; ++i) {
    if (!visv[i]) {
      cnte = 0;
      cntv = 0;
      DFS2(i);
      if (cnte > cntv) {
        return 0;
      }
    }
  }
  return 1;
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i) {
    scanf("%d%d", &a[i].first, &a[i].second);
    b[(i << 1) - 1] = a[i].first;
    b[i << 1] = a[i].second;
  }
  int N = n << 1;
  sort(b + 1, b + N + 1);
  m = unique(b + 1, b + N + 1) - b - 1;
  fill(hed + 1, hed + m + 1, -1);
  ecnt = 0;
  for (int i = 1; i <= n; ++i) {
    a[i].first = lower_bound(b + 1, b + m + 1, a[i].first) - b;
    a[i].second = lower_bound(b + 1, b + m + 1, a[i].second) - b;
    AddEdge(a[i].first, a[i].second, i);
    AddEdge(a[i].second, a[i].first, i);
  }
  b[0] = -1;
  int l = -1;
  int r = m;
  for (; l + 1 < r;) {
    int mid = (l + r) >> 1;
    if (Check(mid)) {
      r = mid;
    } else {
      l = mid;
    }
  }
  printf("%d\n", b[r]);
  return 0;
}
