#include <bits/stdc++.h>
using namespace std;
int qread() {
  int x = 0;
  char c = getchar();
  while (c < '0' || c > '9') c = getchar();
  while (c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
  return x;
}
const int maxn = 2000005;
int n, sz, tote;
int a[maxn], b[maxn], col[maxn], deg[maxn], tot[maxn];
int head[maxn], nxt[maxn * 2], to[maxn * 2];
vector<int> num;
bool ok;
void addedge(int u, int v) {
  tote++;
  nxt[tote] = head[u];
  to[tote] = v;
  head[u] = tote;
}
namespace Dsu {
int f[maxn], s[maxn], rk[maxn];
void init() {
  for (int i = 0; i < (sz); i++) f[i] = i, s[i] = 1, rk[i] = 1;
}
int find(int x) { return f[x] == x ? x : f[x] = find(f[x]); }
void merge(int x, int y) {
  x = find(x);
  y = find(y);
  if (x == y) return;
  if (rk[x] > rk[y]) swap(x, y);
  f[x] = y;
  s[y] += s[x];
  if (rk[x] == rk[y]) rk[y]++;
}
}  // namespace Dsu
void color(int x, int val) {
  if (col[x] >= 0) {
    if (col[x] != val) ok = false;
    return;
  }
  col[x] = val;
  for (int i = head[x]; i; i = nxt[i]) {
    int nxtval = to[i];
    if (nxtval == val) continue;
    int y = a[nxtval] == x ? b[nxtval] : a[nxtval];
    color(y, nxtval);
    if (!ok) return;
  }
}
bool check(int lim) {
  for (int i = 0; i < (n); i++) col[i] = -1;
  ok = true;
  for (int i = 0; i < (n); i++) {
    if (a[i] > lim) return false;
    if (b[i] > lim) color(a[i], i);
    if (!ok) return false;
  }
  return true;
}
int main() {
  n = qread();
  for (int i = 0; i < (n); i++) {
    a[i] = qread();
    b[i] = qread();
    num.push_back(a[i]);
    num.push_back(b[i]);
  }
  sort(num.begin(), num.end());
  num.resize(unique(num.begin(), num.end()) - num.begin());
  sz = int(num.size());
  Dsu::init();
  for (int i = 0; i < (n); i++) {
    a[i] = lower_bound(num.begin(), num.end(), a[i]) - num.begin();
    b[i] = lower_bound(num.begin(), num.end(), b[i]) - num.begin();
    addedge(a[i], i);
    addedge(b[i], i);
    Dsu::merge(a[i], b[i]);
    deg[a[i]]++;
    deg[b[i]]++;
  }
  for (int i = 0; i < (sz); i++) tot[Dsu::find(i)] += deg[i];
  for (int i = 0; i < (sz); i++)
    if (Dsu::find(i) == i) {
      if (tot[i] / 2 > Dsu::s[i]) {
        puts("-1");
        return 0;
      }
    }
  int l = -1, r = sz;
  while (r - l > 1) {
    int mid = l + r >> 1;
    if (check(mid))
      r = mid;
    else
      l = mid;
  }
  printf("%d\n", num[r]);
  return 0;
}
