#include <bits/stdc++.h>
using namespace std;
const int maxn = 2000005;
int n, sz;
int a[maxn], b[maxn], col[maxn], deg[maxn], tot[maxn];
vector<int> ver[maxn], num;
bool ok;
namespace Dsu {
int f[maxn], s[maxn];
void init() {
  for (int i = 0; i < (sz); i++) f[i] = i, s[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;
  f[x] = y;
  s[y] += s[x];
}
}  // 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 = 0; i < (int(ver[val].size())); i++) {
    int y = ver[val][i];
    int nxtval = a[y] == val ? b[y] : a[y];
    color(y, nxtval);
  }
}
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(i, a[i]);
  }
  return ok;
}
int main() {
  scanf("%d", &n);
  for (int i = 0; i < (n); i++) {
    scanf("%d%d", &a[i], &b[i]);
    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();
    ver[a[i]].push_back(i);
    ver[b[i]].push_back(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;
}
