#include <bits/stdc++.h>
using namespace std;
const int N = (int)2e6 + 100;
const int M = (int)1e5 + 100;
const int K = (int)200;
const int INF = (int)1e9 + 7;
const int mod = (int)998244353;
const long double EPS = (long double)1e-9;
const long long LINF = (long long)1e18;
int n;
int a[N], b[N];
bool dead[N], was[N];
int used[N];
vector<int> values;
vector<pair<int, int> > g[N];
int cnt;
void dfs(int x, int p = -1) {
  used[x] = 1;
  for (auto it : g[x]) {
    if (it.second == p) continue;
    if (!used[it.first]) {
      dfs(it.first, it.second);
    } else if (used[it.first] == 1) {
      cnt++;
    }
  }
  used[x] = 2;
}
bool ok(int lim) {
  for (int i = 0; i < int(values.size()); i++) {
    g[i].clear();
  }
  memset(was, 0, sizeof(was));
  memset(dead, 0, sizeof(dead));
  memset(used, 0, sizeof(used));
  for (int i = 1; i <= n; i++) {
    if (a[i] > lim) {
      return false;
    }
    if (b[i] > lim) {
      if (dead[a[i]]) return false;
      dead[a[i]] = 1;
    }
  }
  while (true) {
    bool changed = false;
    for (int i = 1; i <= n; i++) {
      if (b[i] <= lim && !was[i]) {
        was[i] = 1;
        if (dead[a[i]] && dead[b[i]]) {
          return false;
        }
        if (dead[a[i]]) {
          dead[b[i]] = 1;
          changed = true;
        } else if (dead[b[i]]) {
          dead[a[i]] = 1;
          changed = true;
        }
      }
    }
    if (!changed) break;
  }
  for (int i = 1; i <= n; i++) {
    if (b[i] <= lim) {
      if (!dead[a[i]] && !dead[b[i]]) {
        g[a[i]].push_back(make_pair(b[i], i));
        g[b[i]].push_back(make_pair(a[i], i));
      }
    }
  }
  for (int i = 0; i < int(values.size()); i++) {
    if (used[i]) continue;
    cnt = 0;
    dfs(i);
    if (cnt > 1) return false;
  }
  return true;
}
int main() {
  srand(time(0));
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d%d", &a[i], &b[i]);
    values.push_back(a[i]);
    values.push_back(b[i]);
  }
  sort(values.begin(), values.end());
  values.resize(unique(values.begin(), values.end()) - values.begin());
  for (int i = 1; i <= n; i++) {
    a[i] = lower_bound(values.begin(), values.end(), a[i]) - values.begin();
    b[i] = lower_bound(values.begin(), values.end(), b[i]) - values.begin();
  }
  int l = 0, r = int(values.size()), ans = int(values.size());
  while (l <= r) {
    int mid = (l + r) / 2;
    if (ok(mid)) {
      r = mid - 1;
      ans = mid;
    } else {
      l = mid + 1;
    }
  }
  if (ans == int(values.size())) values.push_back(-1);
  cout << values[ans];
  return 0;
}
