#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e6 + 233;
int p[maxn], cnt[maxn], siz[maxn];
vector<int> s[maxn];
int a[maxn], b[maxn], c[maxn];
int find(int x) { return p[x] == x ? x : p[x] = find(p[x]); }
void merge(int x, int y) {
  int a = find(x);
  int b = find(y);
  p[a] = b;
  return;
}
int main() {
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    scanf("%d%d", &a[i], &b[i]);
    c[i * 2 - 1] = a[i];
    c[i * 2] = b[i];
  }
  sort(c + 1, c + 1 + 2 * n);
  int m = unique(c + 1, c + 1 + 2 * n) - c - 1;
  for (int i = 1; i <= m; i++) p[i] = i;
  for (int i = 1; i <= n; i++) {
    a[i] = lower_bound(c + 1, c + 1 + m, a[i]) - c;
    b[i] = lower_bound(c + 1, c + 1 + m, b[i]) - c;
    merge(a[i], b[i]);
  }
  memset(siz, 0, sizeof(siz));
  for (int i = 1; i <= m; i++) {
    int x = find(i);
    siz[x]++;
    s[x].push_back(i);
  }
  for (int i = 1; i <= n; i++) cnt[find(a[i])]++;
  int ans = 0;
  for (int i = 1; i <= m; i++)
    if (find(i) == i) {
      if (cnt[i] > siz[i]) {
        puts("-1");
        return 0;
      }
      sort(s[i].begin(), s[i].end());
      if (cnt[i] == siz[i]) ans = max(ans, c[s[i][siz[i] - 1]]);
      if (cnt[i] + 1 == siz[i]) ans = max(ans, c[s[i][siz[i] - 2]]);
    }
  cout << ans << endl;
  return 0;
}
