#include <bits/stdc++.h>
using namespace std;
struct node {
  int x, y;
} a[2001000];
int n, cc, cnt, ans;
int h[2001000], f[2001000];
int get(int x) { return x == f[x] ? x : f[x] = get(f[x]); }
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    scanf("%d%d", &a[i].x, &a[i].y), h[++cc] = a[i].x, h[++cc] = a[i].y;
  sort(h + 1, h + cc + 1);
  cnt = unique(h + 1, h + cc + 1) - h - 1;
  for (int i = 1; i <= n; i++) {
    a[i].x = lower_bound(h + 1, h + cnt + 1, a[i].x) - h;
    a[i].y = lower_bound(h + 1, h + cnt + 1, a[i].y) - h;
  }
  for (int i = 1; i <= cnt; i++) f[i] = i;
  for (int i = 1; i <= n; i++) {
    int x = a[i].x, y = a[i].y;
    x = get(x);
    y = get(y);
    if (!x && !y) {
      puts("-1");
      return 0;
    }
    if (x > y) swap(x, y);
    if (x == y || !x || !y)
      ans = max(ans, max(h[x], h[y])), f[x] = f[y] = 0;
    else
      ans = max(ans, min(h[x], h[y])), f[x] = y;
  }
  printf("%d\n", ans);
  return 0;
}
