#include <bits/stdc++.h>
using namespace std;
int A[1234567], B[1234567];
bool resolved[1234567];
int N;
bool resolve(int u, int d, map<int, set<int>> &m, map<int, bool> &days) {
  resolved[u] = true;
  if (days[d]) return false;
  days[d] = true;
  for (auto x : m[d]) {
    if (x == u) continue;
    resolve(x, (d == A[x] ? B[x] : A[x]), m, days);
  }
  m.erase(d);
  return true;
}
void singleResolve(int u, int d, map<int, set<int>> &m, map<int, bool> &days) {
  resolved[u] = true;
  assert(days[d] == false);
  days[d] = true;
  if (m.count(A[u]) != 0) {
    auto &s = m[A[u]];
    s.erase(u);
    if (s.size() == 1) singleResolve(*s.begin(), A[u], m, days);
  }
  if (m.count(B[u]) != 0) {
    auto &s = m[B[u]];
    s.erase(u);
    if (s.size() == 1) singleResolve(*s.begin(), B[u], m, days);
  }
}
bool check(int D) {
  map<int, set<int>> m;
  map<int, bool> days;
  for (int i = 0; i < N; i++) {
    if (A[i] <= D) {
      m[A[i]].insert(i);
      days[A[i]] = false;
    }
    if (B[i] <= D) {
      m[B[i]].insert(i);
      days[B[i]] = false;
    }
  }
  for (int i = 0; i < N; i++) resolved[i] = false;
  for (int i = 0; i < N; i++) {
    if (A[i] > D)
      return false;
    else if (B[i] > D) {
      if (!resolve(i, A[i], m, days)) return false;
    }
  }
  for (auto &p : m)
    if (p.second.size() == 1) {
      int u = *(p.second.begin());
      singleResolve(u, p.first, m, days);
    }
  for (auto &p : m)
    if (p.second.size() != 2 && p.second.size() != 0) return false;
  int total = 0;
  for (auto &p : m)
    if (p.second.size() == 2) total += 1;
  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]);
  int lo = 1, hi = 1e9 + 1;
  while (lo < hi) {
    int mid = (lo + hi) >> 1;
    if (check(mid))
      hi = mid;
    else
      lo = mid + 1;
  }
  if (hi == 1e9 + 1)
    cout << -1 << endl;
  else
    cout << hi << endl;
  return 0;
}
