#include <bits/stdc++.h>
using namespace std;
int n, tot, cnt;
int fa[1000001], bj[1000001];
int mx1[1000001], mx2[1000001];
int a[2000001], u[1000001], v[1000001];
int ans;
int find(int k) {
  if (k != fa[k]) fa[k] = find(fa[k]);
  return fa[k];
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d%d", &u[i], &v[i]);
    a[cnt++] = u[i];
    a[cnt++] = v[i];
  }
  sort(a, a + cnt);
  tot = unique(a, a + cnt) - a - 1;
  for (int i = 1; i <= n; i++) {
    u[i] = tot + 1 - (lower_bound(a, a + tot, u[i]) - a);
    v[i] = tot + 1 - (lower_bound(a, a + tot, v[i]) - a);
  }
  for (int i = 1; i <= tot; i++) fa[i] = i, mx1[i] = a[i], mx2[i] = 0;
  for (int i = 1; i <= n; i++) {
    int x = find(u[i]), y = find(v[i]);
    if (x == y) {
      if (bj[x]) {
        printf("-1\n");
        return 0;
      } else
        bj[x] = 1;
    } else {
      fa[x] = y;
      if (mx1[x] > mx1[y]) {
        mx2[y] = max(mx1[y], mx2[x]);
        mx1[y] = mx1[x];
      } else
        mx2[y] = max(mx1[x], mx2[y]);
    }
  }
  ans = 0;
  for (int i = 1; i <= tot; i++) {
    int x = find(i);
    if (bj[x] == -1) continue;
    if (bj[x] == 0) ans = max(ans, mx2[x]);
    if (bj[x] == 1) ans = max(ans, mx1[x]);
    bj[x] = -1;
  }
  printf("%lld\n", ans);
  return 0;
}
