#include <bits/stdc++.h>
using namespace std;
const int N = 2 * 1000 * 1000 + 11;
const int INF = 1e9 + 1;
int n;
int a[N][2];
vector<int> s;
bool used[N];
bool exam[N];
vector<pair<int, int>> g[N];
bool deny(int v) {
  if (used[v]) return false;
  used[v] = true;
  for (auto it : g[v]) {
    int to = it.first;
    int ex = it.second;
    if (exam[ex]) continue;
    if (used[to]) return false;
    exam[ex] = true;
    if (!deny(to)) return false;
  }
  return true;
}
int cntv, cnte;
void dfs(int v) {
  ++cntv;
  used[v] = true;
  for (auto it : g[v]) {
    int to = it.first;
    int ex = it.second;
    if (exam[ex]) continue;
    exam[ex] = true;
    ++cnte;
    if (!used[to]) dfs(to);
  }
}
bool check(int day) {
  memset(used, false, sizeof(used));
  memset(exam, false, sizeof(exam));
  for (int i = 0; i < int(s.size()); ++i)
    if (i > day) {
      if (!deny(i)) {
        return false;
      }
    }
  for (int i = 0; i < int(s.size()); ++i) {
    if (!used[i]) {
      cntv = 0;
      cnte = 0;
      dfs(i);
      if (cntv < cnte) return false;
    }
  }
  return true;
}
int main() {
  scanf("%d", &n);
  for (int i = 0; i < int(n); ++i)
    for (int j = 0; j < int(2); ++j) {
      scanf("%d", &a[i][j]);
      s.push_back(a[i][j]);
    }
  sort(s.begin(), s.end());
  s.resize(unique(s.begin(), s.end()) - s.begin());
  for (int i = 0; i < int(n); ++i) {
    for (int j = 0; j < int(2); ++j) {
      a[i][j] = lower_bound(s.begin(), s.end(), a[i][j]) - s.begin();
    }
    g[a[i][0]].push_back(make_pair(a[i][1], i));
    g[a[i][1]].push_back(make_pair(a[i][0], i));
  }
  int l = 0, r = int(s.size()) - 1;
  while (r - l > 1) {
    int mid = (l + r) >> 1;
    if (check(mid))
      r = mid;
    else
      l = mid;
  }
  for (int i = l; i <= r; ++i) {
    if (check(i)) {
      printf("%d\n", s[i]);
      return 0;
    }
  }
  puts("-1");
}
