#include <bits/stdc++.h>
using namespace std;
const int MAXN = 2 * 1e6 + 10;
inline int read() {
  char c = getchar();
  int x = 0, f = 1;
  while (c < '0' || c > '9') {
    if (c == '-') f = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
  return x * f;
}
int N, a[MAXN], b[MAXN], date[MAXN << 1], cnt = 0, fa[MAXN], tag[MAXN],
                                          mx[MAXN], smx[MAXN];
int find(int x) {
  if (fa[x] == x)
    return fa[x];
  else
    return fa[x] = find(fa[x]);
}
int main() {
  N = read();
  for (int i = 1; i <= N; i++)
    a[i] = read(), b[i] = read(), date[++cnt] = a[i], date[++cnt] = b[i];
  sort(date + 1, date + cnt + 1);
  cnt = unique(date + 1, date + cnt + 1) - date - 1;
  for (int i = 1; i <= N; i++)
    a[i] = lower_bound(date + 1, date + cnt + 1, a[i]) - date,
    b[i] = lower_bound(date + 1, date + cnt + 1, b[i]) - date;
  for (int i = 1; i <= cnt; i++) fa[i] = i, mx[i] = i;
  for (int i = 1; i <= N; i++) {
    int x = a[i], y = b[i];
    int fx = find(x), fy = find(y);
    if (find(x) != find(y)) {
      fa[fy] = fx;
      tag[fx] |= tag[fy];
      if (mx[fy] > mx[fx])
        smx[fx] = max(smx[fx], max(smx[fy], mx[fx])), mx[fx] = mx[fy];
      else
        smx[fx] = max(smx[fx], mx[fy]);
    } else if (!tag[fx])
      tag[fx] = 1;
    else {
      puts("-1");
      return 0;
    }
  }
  int ans = 0;
  for (int i = 1; i <= cnt; i++) {
    if (fa[i] == i)
      if (tag[i])
        ans = max(ans, mx[i]);
      else
        ans = max(ans, smx[i]);
  }
  printf("%d\n", date[ans]);
  return 0;
}
