#include <bits/stdc++.h>
using namespace std;
int n, u, v;
int val[2000010];
pair<int, int> l[2000010];
int fa[2000010];
bool spe[2000010];
int maxv[2000010], secv[2000010];
int c[5];
int get_fa(int x) {
  if (fa[x] == 0) return x;
  fa[x] = get_fa(fa[x]);
  return fa[x];
}
bool merge(int u, int v) {
  u = get_fa(u);
  v = get_fa(v);
  if (u == v) {
    if (spe[u] == 1) return 0;
    spe[u] = 1;
  } else {
    fa[u] = v;
    spe[v] = max(spe[v], spe[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() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d%d", &u, &v);
    l[i] = make_pair(u, v);
    val[i * 2] = u;
    val[i * 2 - 1] = v;
  }
  sort(val + 1, val + 1 + 2 * n);
  int cnt = unique(val + 1, val + 1 + 2 * n) - val - 1;
  for (int i = 1; i <= n; i++) {
    l[i].first = lower_bound(val + 1, val + 1 + cnt, l[i].first) - val;
    l[i].second = lower_bound(val + 1, val + 1 + cnt, l[i].second) - val;
  }
  for (int i = 1; i <= cnt; i++) {
    maxv[i] = val[i];
    secv[i] = 0;
  }
  for (int i = 1; i <= n; i++) {
    if (merge(l[i].first, l[i].second) == 0) {
      printf("-1");
      return 0;
    }
  }
  int ans = 0;
  for (int i = 1; i <= cnt; i++)
    if (i == get_fa(i)) {
      if (spe[i] == 1)
        ans = max(ans, maxv[i]);
      else
        ans = max(ans, secv[i]);
    }
  printf("%d", ans);
}
