#include <bits/stdc++.h>
using namespace std;
long long n, a[1000009], b[1000009], n1;
long long d[1000009], c[1000009];
int par[2000009], used[2000009], cycle[2000009];
map<int, int> mp;
int findpar(int u) {
  if (par[u] == u) return u;
  return par[u] = findpar(par[u]);
}
bool possible(long long val) {
  for (int i = 1; i <= 2 * n; i++) {
    par[i] = i;
    used[i] = 0;
    cycle[i] = 0;
  }
  for (int i = 1; i <= n; i++) {
    if (a[i] > val && b[i] > val) return 0;
    if (a[i] > val || b[i] > val) continue;
    if (findpar(c[i]) == findpar(d[i])) continue;
    par[findpar(c[i])] = findpar(d[i]);
    used[i] = 1;
  }
  for (int i = 1; i <= n; i++) {
    if (used[i]) continue;
    if (a[i] > val) {
      cycle[findpar(d[i])]++;
      if (cycle[findpar(d[i])] > 1) return 0;
    }
    if (b[i] > val) {
      cycle[findpar(c[i])]++;
      if (cycle[findpar(c[i])] > 1) return 0;
    }
    if (a[i] <= val && b[i] <= val) {
      cycle[findpar(c[i])]++;
      if (cycle[findpar(c[i])] > 1) return 0;
    }
  }
  return 1;
}
vector<long long> vec;
int main() {
  scanf("%lld", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%lld %lld", &a[i], &b[i]);
    vec.push_back(a[i]);
    vec.push_back(b[i]);
    if (mp.find(a[i]) == mp.end()) {
      mp[a[i]] = ++n1;
    }
    if (mp.find(b[i]) == mp.end()) {
      mp[b[i]] = ++n1;
    }
    c[i] = mp[a[i]];
    d[i] = mp[b[i]];
  }
  sort(vec.begin(), vec.end());
  vec.erase(unique(vec.begin(), vec.end()), vec.end());
  long long lo = 0, hi = vec.size() - 1, mid, best = -1;
  while (hi >= lo) {
    mid = (hi + lo) / 2;
    if (possible(vec[mid])) {
      best = vec[mid];
      hi = mid - 1;
    } else
      lo = mid + 1;
  }
  cout << best << endl;
  return 0;
}
