#include <bits/stdc++.h>
int n, a[1000005], b[1000005], h[1000005 * 2], p[1000005], q[1000005], tl = 0;
namespace dsu {
int fa[1000005] = {0}, vis[1000005] = {0};
void reset() {
  std::memset(fa, 0, sizeof(fa));
  std::memset(vis, 0, sizeof(vis));
}
int getf(int x) { return fa[x] ? fa[x] = getf(fa[x]) : x; }
int Union(int x, int y) {
  int f1 = getf(x), f2 = getf(y);
  if (f1 != f2) {
    fa[f2] = f1;
    vis[f1] |= vis[f2];
  } else {
    if (vis[f1]) return 0;
    vis[f1] = 1;
  }
}
}  // namespace dsu
int check(int x) {
  dsu::reset();
  for (int i = 1; i <= n; ++i) {
    if (x < a[i])
      return 0;
    else if (x < b[i]) {
      if (!dsu::Union(p[i], p[i])) return 0;
    } else {
      if (!dsu::Union(p[i], q[i])) return 0;
    }
  }
  return 1;
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i) {
    scanf("%d%d", &a[i], &b[i]);
    if (a[i] > b[i]) std::swap(a[i], b[i]);
    h[++tl] = a[i];
    h[++tl] = b[i];
  }
  std::sort(h + 1, h + tl + 1);
  tl = std::unique(h + 1, h + tl + 1) - (h + 1);
  for (int i = 1; i <= n; ++i) {
    p[i] = std::lower_bound(h + 1, h + tl + 1, a[i]) - (h + 1) + 1;
    q[i] = std::lower_bound(h + 1, h + tl + 1, b[i]) - (h + 1) + 1;
  }
  int l = 1, r = tl, ans = -1;
  while (l <= r) {
    int mid = (l + r) >> 1;
    if (check(h[mid])) {
      ans = mid;
      r = mid - 1;
    } else
      l = mid + 1;
  }
  printf("%d", ans != -1 ? h[ans] : ans);
  return 0;
}
