#include <bits/stdc++.h>
using namespace std;
inline int gi() {
  int x = 0, f = 1;
  char ch = getchar();
  while (!isdigit(ch)) {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while (isdigit(ch)) {
    x = (x << 3) + (x << 1) + ch - 48;
    ch = getchar();
  }
  return x * f;
}
template <typename T>
inline bool Max(T& a, const T& b) {
  return a < b ? a = b, true : false;
}
template <typename T>
inline bool Min(T& a, const T& b) {
  return a > b ? a = b, true : false;
}
const int N = 1e6 + 7;
int n, len, ans;
int V[N << 1], par[N << 1], cir[N << 1], mx[N << 1], a[N], b[N];
int getpar(int a) { return par[a] == a ? a : par[a] = getpar(par[a]); }
int main() {
  n = gi();
  for (int i = 1; i <= n + n; i++) par[i] = i;
  for (int i = 1; i <= n; i++) V[++len] = a[i] = gi(), V[++len] = b[i] = gi();
  sort(V + 1, V + 1 + len);
  len = unique(V + 1, V + 1 + len) - V - 1;
  for (int i = 1; i <= n; i++) {
    a[i] = lower_bound(V + 1, V + 1 + len, a[i]) - V;
    b[i] = lower_bound(V + 1, V + 1 + len, b[i]) - V;
    int f1 = getpar(a[i]), f2 = getpar(b[i]);
    if (cir[f1] && cir[f2]) return puts("-1"), 0;
    if (f1 > f2) swap(f1, f2);
    Max(ans, V[f1]);
    if (f1 == f2) {
      cir[f2] = 1;
      continue;
    }
    par[f1] = f2, cir[f2] |= cir[f1];
  }
  printf("%d\n", ans);
  return 0;
}
