#include <bits/stdc++.h>
using namespace std;
template <class T>
bool ckmax(T& x, T y) {
  return x < y ? x = y, 1 : 0;
}
template <class T>
bool ckmin(T& x, T y) {
  return x > y ? x = y, 1 : 0;
}
inline int read() {
  int x = 0, f = 1;
  char ch = getchar();
  while (!isdigit(ch)) {
    if (ch == '-') f = 0;
    ch = getchar();
  }
  while (isdigit(ch)) x = x * 10 + ch - '0', ch = getchar();
  return f ? x : -x;
}
const int N = 1000006;
const int M = N << 1;
int n, lsh[M], len, a[N], b[N], F[M];
bool vis[M];
inline int anc(int x) { return x == F[x] ? x : F[x] = anc(F[x]); }
inline bool merge(int x, int y) {
  x = anc(x), y = anc(y);
  if (x != y)
    return F[x] = y, vis[y] |= vis[x], 1;
  else if (!vis[x])
    return vis[x] = 1, 1;
  return 0;
}
inline bool check(int o) {
  for (int i = 1, iend = o; i <= iend; ++i) F[i] = i;
  int res = 0;
  for (int i = 1, iend = n; i <= iend; ++i) {
    if (b[i] <= o) {
      if (merge(a[i], b[i])) ++res;
    } else if (merge(a[i], a[i]))
      ++res;
  }
  return res == n;
}
signed main() {
  int l = 0, r, res = -1;
  n = read();
  for (int i = 1, iend = n; i <= iend; ++i)
    a[i] = read(), b[i] = read(), lsh[++len] = a[i], lsh[++len] = b[i],
    ckmax(l, a[i]);
  sort(lsh + 1, lsh + len + 1), len = unique(lsh + 1, lsh + len + 1) - lsh - 1;
  for (int i = 1, iend = n; i <= iend; ++i)
    a[i] = lower_bound(lsh + 1, lsh + len + 1, a[i]) - lsh,
    b[i] = lower_bound(lsh + 1, lsh + len + 1, b[i]) - lsh;
  l = lower_bound(lsh + 1, lsh + len + 1, l) - lsh;
  ckmax(l, n);
  r = len;
  while (l <= r) {
    int mid = (l + r) >> 1;
    if (check(mid))
      r = mid - 1, res = mid;
    else
      l = mid + 1;
  }
  res = res == -1 ? -1 : lsh[res];
  printf("%d\n", res);
}
