#include <bits/stdc++.h>
using namespace std;
int n, m, a[1000009], b[1000009];
int x[2 * 1000009];
bool taken[2 * 1000009], done[1000009];
vector<int> g[2 * 1000009];
bool take(int d, int u, int j) {
  if (done[u]) return true;
  if (taken[j] || x[j] > d) return false;
  taken[j] = true;
  done[u] = true;
  for (int v : g[j]) {
    if (u == v) continue;
    if (!take(d, v, a[v] + b[v] - j)) return false;
  }
  return true;
}
bool check(int d) {
  memset(&done, (false), sizeof(done));
  ;
  memset(&taken, (false), sizeof(taken));
  ;
  for (int i = 1; (i) <= int(n); (i)++) {
    if (done[i]) continue;
    if (x[a[i]] > d) return false;
    if (x[b[i]] > d) {
      if (!take(d, i, a[i])) return false;
    }
  }
  for (int j = 1; (j) <= int(m); (j)++) {
    if (x[j] > m || taken[j]) continue;
    int cnt = 0;
    for (int v : g[j]) {
      if (!done[v]) cnt++;
    }
    if (cnt == 1) {
      for (int v : g[j]) {
        if (!done[v] && !take(d, v, j)) return false;
      }
    }
  }
  for (int i = 1; (i) <= int(n); (i)++) {
    if (done[i]) continue;
    if (taken[a[i]] && taken[b[i]])
      return false;
    else if (!taken[a[i]]) {
      if (!take(d, i, a[i])) return false;
    } else if (!taken[b[i]]) {
      if (!take(d, i, b[i])) return false;
    }
  }
  return true;
}
int main() {
  scanf("%d", &n);
  map<int, int> reduce;
  for (int i = 1; (i) <= int(n); (i)++) {
    scanf("%d %d", &a[i], &b[i]);
    reduce[a[i]] = 0;
    reduce[b[i]] = 0;
  }
  m = 0;
  for (auto& pp : reduce) {
    pp.second = ++m;
    x[m] = pp.first;
  }
  for (int i = 1; (i) <= int(n); (i)++) {
    a[i] = reduce[a[i]];
    b[i] = reduce[b[i]];
    g[a[i]].push_back(i);
    g[b[i]].push_back(i);
  }
  int lo = 0;
  int hi = 0x3f3f3f3f;
  while (hi > lo + 1) {
    int mid = (hi + lo) / 2;
    if (check(mid))
      hi = mid;
    else
      lo = mid;
  }
  printf("%d\n", hi == 0x3f3f3f3f ? -1 : hi);
  return 0;
}
