#include <bits/stdc++.h>
using namespace std;
inline int read() {
  int ch = getchar(), f = 1, ans = 0;
  for (; ch < '0' || ch > '9'; ch = getchar())
    if (ch == '-') f = 0;
  for (; ch >= '0' && ch <= '9'; ch = getchar())
    ans = (((ans << 2) + ans) << 1) + (ch ^ '0');
  return f ? ans : -ans;
}
const int maxn = 2000050;
int n, cnt;
int val[maxn], fa[maxn];
int a[maxn], b[maxn];
int cntv[maxn], cnte[maxn], maxval[maxn];
long long sum = 0, subsum = 0;
int max2val[maxn];
int va[maxn], vb[maxn];
int Getfa(int x) { return (fa[x] == x) ? (x) : (fa[x] = Getfa(fa[x])); }
void Union(int x, int y) {
  if (Getfa(x) == Getfa(y)) return;
  fa[fa[x]] = fa[y];
}
int ans;
int main() {
  n = read();
  for (int i = 1; i <= n; i++) {
    a[i] = read();
    b[i] = read();
    val[++cnt] = a[i];
    val[++cnt] = b[i];
  }
  sort(val + 1, val + 1 + cnt);
  cnt = unique(val + 1, val + 1 + cnt) - val - 1;
  for (int i = 1; i <= cnt; ++i) fa[i] = i;
  for (int i = 1; i <= n; ++i) {
    a[i] = lower_bound(val + 1, val + 1 + cnt, a[i]) - val;
    b[i] = lower_bound(val + 1, val + 1 + cnt, b[i]) - val;
    Union(a[i], b[i]);
  }
  for (int i = 1; i <= cnt; i++) {
    Getfa(i);
    cntv[fa[i]]++;
    if (val[i] > maxval[fa[i]]) {
      max2val[fa[i]] = maxval[fa[i]];
      maxval[fa[i]] = val[i];
    } else if (val[i] > max2val[fa[i]]) {
      max2val[fa[i]] = val[i];
    }
  }
  for (int i = 1; i <= n; i++) {
    cnte[fa[a[i]]]++;
  }
  for (int i = 1; i <= cnt; i++) {
    if (fa[i] == i) {
      if (cnte[i] < cntv[i])
        ans = max(ans, max2val[i]);
      else if (cnte[i] == cntv[i])
        ans = max(ans, maxval[i]);
      else {
        cout << -1 << endl;
        return 0;
      }
    }
  }
  cout << ans << endl;
  return 0;
}
