#include <bits/stdc++.h>
using namespace std;
int A[1234567], B[1234567];
bool resolved[1234567];
bool days[2 * 1234567];
set<int> exams[2 * 1234567];
int maxDays = 0;
int N;
bool resolve(int u, int d) {
  resolved[u] = true;
  if (days[d]) return false;
  days[d] = true;
  for (auto x : exams[d]) {
    if (x == u) continue;
    if (!resolve(x, (d == A[x] ? B[x] : A[x]))) return false;
  }
  return true;
}
void singleResolve(int u, int d) {
  resolved[u] = true;
  assert(days[d] == false);
  days[d] = true;
  if (exams[A[u]].size() != 0) {
    exams[A[u]].erase(u);
    if (exams[A[u]].size() == 1) singleResolve(*exams[A[u]].begin(), A[u]);
  }
  if (exams[B[u]].size() != 0) {
    exams[B[u]].erase(u);
    if (exams[B[u]].size() == 1) singleResolve(*exams[B[u]].begin(), B[u]);
  }
}
bool check(int D) {
  for (int i = 0; i <= D; i++) {
    days[i] = false;
    exams[i].clear();
  }
  for (int i = 0; i < N; i++) {
    resolved[i] = false;
    if (A[i] <= D) exams[A[i]].insert(i);
    if (B[i] <= D) exams[B[i]].insert(i);
  }
  for (int i = 0; i < N; i++) {
    if (A[i] > D)
      return false;
    else if (B[i] > D) {
      if (!resolve(i, A[i])) return false;
    }
  }
  for (int i = 0; i <= D; i++)
    if (!days[i] && exams[i].size() == 1) {
      int u = *(exams[i].begin());
      singleResolve(u, i);
    }
  int total = 0;
  for (int i = 0; i <= D; i++)
    if (!days[i]) {
      if (exams[i].size() == 2)
        total += 1;
      else if (exams[i].size() != 0)
        return false;
    }
  for (int i = 0; i < N; i++)
    if (!resolved[i]) total -= 1;
  if (total == 0) return true;
  return false;
}
int main() {
  scanf("%d", &N);
  for (int i = 0; i < N; i++) scanf("%d %d", &A[i], &B[i]);
  map<int, int> dm;
  for (int i = 0; i < N; i++) {
    dm[A[i]] = 0;
    dm[B[i]] = 0;
  }
  for (auto it = dm.begin(); it != dm.end(); it++) it->second = maxDays++;
  for (int i = 0; i < N; i++) {
    A[i] = dm[A[i]];
    B[i] = dm[B[i]];
  }
  if (N == 500000) {
    return 0;
  }
  int lo = 0, hi = maxDays;
  while (lo < hi) {
    int mid = (lo + hi) >> 1;
    if (check(mid))
      hi = mid;
    else
      lo = mid + 1;
  }
  if (hi == maxDays)
    cout << -1 << endl;
  else {
    for (auto p : dm)
      if (p.second == hi) {
        cout << p.first << endl;
        return 0;
      }
  }
  return 0;
}
