#include <bits/stdc++.h>
using namespace std;
const int maxn = 1000010 * 3;
struct node {
  int u, v;
} p[maxn];
int fa[maxn], pre[maxn], maxv[maxn], secv[maxn], s[maxn];
int find(int x) {
  if (x == fa[x]) return x;
  return fa[x] = find(fa[x]);
}
int merge(int x, int y) {
  int c[10], u, v;
  u = find(x);
  v = find(y);
  if (u == v) {
    if (pre[v] == 1) {
      return 0;
    }
    pre[v] = 1;
    return 1;
  }
  fa[u] = v;
  pre[v] = max(pre[v], pre[u]);
  c[0] = maxv[v];
  c[1] = maxv[u];
  c[2] = secv[v];
  c[3] = secv[u];
  sort(c, c + 4);
  maxv[v] = c[3];
  secv[v] = c[2];
  return 1;
}
int main() {
  int ans, cnt, top, i, n;
  scanf("%d", &n);
  top = 0;
  for (i = 1; i <= n; i++) {
    scanf("%d%d", &p[i].u, &p[i].v);
    s[++top] = p[i].u;
    s[++top] = p[i].v;
  }
  sort(s + 1, s + top + 1);
  cnt = unique(s + 1, s + top + 1) - s - 1;
  for (i = 1; i <= n; i++) {
    p[i].u = lower_bound(s + 1, s + cnt + 1, p[i].u) - s;
    p[i].v = lower_bound(s + 1, s + cnt + 1, p[i].v) - s;
  }
  for (i = 1; i <= cnt; i++) {
    fa[i] = i;
    maxv[i] = s[i];
    secv[i] = 0;
  }
  for (i = 1; i <= n; i++) {
    if (merge(p[i].u, p[i].v) == 0) {
      printf("%d\n", -1);
      return 0;
    }
  }
  ans = 0;
  for (i = 1; i <= cnt; i++) {
    if (i == find(i)) {
      if (pre[i] == 1) {
        ans = max(ans, maxv[i]);
      } else {
        ans = max(ans, secv[i]);
      }
    }
  }
  printf("%d\n", ans);
  return 0;
}
