#include <bits/stdc++.h>
using namespace std;
int par[2000001], rep[2000001];
map<int, int> m;
map<int, int>::iterator it;
vector<int> v[2000001];
int findpar(int x) {
  while (x != par[x]) x = par[x];
  return x;
}
int main() {
  int n, x, y, index1 = 1, flag = 1, ans = 1000000001, i, j;
  scanf("%d", &n);
  int a[2 * n], b[2 * n];
  for (i = 1; i <= 2 * n; i++) {
    par[i] = i;
    rep[i] = 0;
  }
  for (i = 0; i < n; i++) {
    scanf("%d %d", &a[2 * i], &a[2 * i + 1]);
    b[2 * i] = a[2 * i];
    b[2 * i + 1] = a[2 * i + 1];
  }
  sort(a, a + 2 * n);
  for (i = 0; i < 2 * n; i++) {
    if (m[a[i]] == 0) {
      m[a[i]] = index1;
      index1++;
    }
  }
  for (i = 0; i < 2 * n; i += 2) {
    x = findpar(m[b[i]]);
    y = findpar(m[b[i + 1]]);
    if (x == y) {
      rep[x]++;
    }
    par[m[b[i]]] = min(x, y);
    par[m[b[i + 1]]] = min(x, y);
  }
  for (i = 1; i <= 2 * n; i++) {
    if (rep[i] > 0) {
      j = findpar(i);
      if (j != i) {
        rep[j] += rep[i];
        rep[i] = 0;
      }
    }
  }
  for (it = m.begin(); it != m.end(); it++) {
    v[findpar((*it).second)].push_back((*it).first);
  }
  for (i = 1; i <= 2000000; i++) {
    if (rep[i] > 1) {
      flag = 0;
      break;
    }
    if (v[i].size() == 0) continue;
    if (rep[i] == 1)
      ans = min(ans, v[i][v[i].size() - 1]);
    else
      ans = min(ans, v[i][v[i].size() - 2]);
  }
  if (flag == 0) {
    printf("-1");
    return 0;
  }
  printf("%d", ans);
  return 0;
}
