#include <bits/stdc++.h>
using namespace std;
const long long inf = 4e18;
const long long maxn = 2e5 + 10;
const long long mod = 1e9 + 7;
const long long Big = 1e9 + 10;
pair<long long, long long> p[maxn];
long long N, M, n;
vector<long long> vec, v[maxn], g[maxn];
long long dg[maxn];
map<long long, long long> cd;
long long dc[maxn];
bool mark[maxn];
void dfs(long long u) {
  M += int((v[u]).size());
  N++;
  mark[u] = 1;
  for (long long x : v[u]) {
    if (!mark[x]) dfs(x);
  }
}
bool ok(long long x) {
  for (int i = 0; i < int(n); i++) {
    if (p[i].first > x && p[i].second > x)
      return 0;
    else if (p[i].second > x)
      dg[i] = 1, vec.push_back(i);
    else if (p[i].first > x)
      dg[i] = 2, vec.push_back(i);
    else
      dg[i] = 3;
  }
  while (int((vec).size())) {
    long long w = vec.back(), y;
    vec.pop_back();
    if (dg[w] == 1) y = p[w].first;
    if (dg[w] == 2) y = p[w].second;
    for (long long x : g[y]) {
      if (x != w) {
        if (dg[x] != 3) return 0;
        if (p[x].first == y)
          dg[x] = 2;
        else
          dg[x] = 1;
        vec.push_back(x);
      }
    }
    dg[w] = 0;
  }
  for (int i = 0; i < int(n); i++) {
    if (dg[i] == 3) {
      v[p[i].first].push_back(p[i].second);
      v[p[i].second].push_back(p[i].first);
    }
  }
  for (int i = 0; i < int(maxn); i++) {
    N = M = 0;
    if (mark[i]) continue;
    dfs(i);
    M /= 2;
    if (M != (N - 1) && M != N) return 0;
  }
  return 1;
}
int main() {
  cin >> n;
  for (int i = 0; i < int(n); i++) {
    cin >> p[i].first >> p[i].second;
    cd[p[i].first] = cd[p[i].second] = 0;
  }
  long long H = 0;
  for (map<long long, long long>::iterator it = cd.begin(); it != cd.end();
       it++)
    cd[it->first] = H, dc[H] = (it->first), H++;
  for (int i = 0; i < int(n); i++) {
    p[i].first = cd[p[i].first];
    p[i].second = cd[p[i].second];
  }
  for (int i = 0; i < int(n); i++) {
    g[p[i].first].push_back(i);
    g[p[i].second].push_back(i);
  }
  long long l = 1, r = H - 1, ans = -1;
  while (l <= r) {
    long long mid = (l + r) / 2;
    memset(dg, 0, sizeof dg);
    memset(mark, 0, sizeof mark);
    for (int i = 0; i < int(maxn); i++) {
      v[i].clear();
    }
    vec.clear();
    if (ok(mid)) {
      ans = dc[mid];
      r = mid - 1;
    } else {
      l = mid + 1;
    }
  }
  cout << ans << endl;
}
