#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e6 + 5;
int num[maxn << 1], cnt, fa[maxn << 1], a[maxn], b[maxn], mix[maxn << 1],
    sz[maxn << 1], sze[maxn << 1], ci[maxn << 1];
int find(int t) { return fa[t] == t ? fa[t] : find(fa[t]); }
int main() {
  int n, i, j;
  scanf("%d", &n);
  for (i = 1; i <= n; i++) {
    scanf("%d%d", &a[i], &b[i]);
    num[++cnt] = a[i];
    num[++cnt] = b[i];
  }
  for (i = 1; i <= maxn * 2; i++) {
    fa[i] = i;
    sz[i] = 1;
    sze[i] = 0;
    mix[i] = i;
    ci[i] = 0;
  }
  sort(num + 1, num + 1 + cnt);
  cnt = unique(num + 1, num + 1 + cnt) - num - 1;
  for (i = 1; i <= n; i++) {
    a[i] = lower_bound(num + 1, num + 1 + cnt, a[i]) - num;
    b[i] = lower_bound(num + 1, num + 1 + cnt, b[i]) - num;
    int r1 = find(a[i]), r2 = find(b[i]);
    if (r1 == r2)
      sze[r1]++;
    else {
      sze[r1] += sze[r2] + 1;
      sz[r1] += sz[r2];
      fa[r2] = r1;
      if (mix[r2] > mix[r1]) {
        ci[r1] = mix[r1];
        mix[r1] = mix[r2];
      } else if (mix[r2] > ci[r1])
        ci[r1] = mix[r2];
      if (ci[r2] > mix[r1]) {
        ci[r1] = mix[r1];
        mix[r1] = ci[r2];
      } else if (ci[r2] > ci[r1])
        ci[r1] = ci[r2];
    }
  }
  int ans = 0;
  for (i = 1; i <= maxn * 2; i++) {
    if (find(i) == i) {
      if (sz[i] == sze[i] + 1)
        ans = max(ans, num[ci[i]]);
      else if (sz[i] == sze[i])
        ans = max(ans, num[mix[i]]);
      else {
        ans = -1;
        break;
      }
    }
  }
  printf("%d\n", ans);
  return 0;
}
