#include <bits/stdc++.h>
using namespace std;
int n, f[1000007 << 1], ans, q[1000007 << 1], cnt;
struct node {
  int x, id, num;
} a[1000007 << 1];
inline int read() {
  int s = 0, w = 1;
  char c = getchar();
  for (; !isdigit(c); c = getchar())
    if (c == '-') w = -1;
  for (; isdigit(c); c = getchar()) s = (s << 1) + (s << 3) + (c ^ 48);
  return (s * w);
}
inline bool cmp1(node x, node y) { return x.x < y.x; };
inline bool cmp2(node x, node y) {
  return x.id == y.id ? x.num < y.num : x.id < y.id;
}
inline int find(int x) {
  if (f[x] == x)
    return f[x];
  else
    return f[x] = find(f[x]);
}
int main() {
  n = read();
  for (int i = 1; i <= n; i++)
    a[(i << 1) - 1].x = read(), a[i << 1].x = read(),
                 a[i << 1].id = a[(i << 1) - 1].id = i;
  sort(a + 1, a + (n << 1) + 1, cmp1);
  for (int i = 1; i <= (n << 1); i++)
    if (a[i].x != a[i - 1].x)
      a[i].num = ++cnt, q[cnt] = a[i].x;
    else
      a[i].num = cnt;
  sort(a + 1, a + (n << 1) + 1, cmp2);
  for (int i = 1; i <= cnt; i++) f[i] = i;
  for (int i = 1; i <= n; i++) {
    int x = find(a[(i << 1) - 1].num), y = find(a[i << 1].num);
    if (!x && !y) {
      printf("-1\n");
      return 0;
    }
    if (!x || !y || x == y) {
      f[x] = 0, f[y] = 0;
      ans = max(ans, max(q[x], q[y]));
    } else {
      if (x > y) swap(x, y);
      ans = max(ans, q[x]);
      f[x] = y;
    }
  }
  printf("%d\n", ans);
  return 0;
}
