#include <bits/stdc++.h>
#pragma GCC optimize("-O3")
using namespace std;
void _main();
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  _main();
}
const int inf = INT_MAX / 2;
const long long infl = 1LL << 60;
template <class T>
bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T>
bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
struct UnionFind {
  vector<int> par;
  UnionFind(int NV) {
    par.clear();
    for (int i = 0; i < NV; i++) par.push_back(i);
  }
  void reset() {
    for (int i = 0; i < par.size(); i++) par[i] = i;
  }
  int operator[](int x) {
    return par[x] == x ? x : par[x] = operator[](par[x]);
  }
  void operator()(int x, int y) {
    x = operator[](x);
    y = operator[](y);
    if (x != y) par[x] = y;
  }
};
int N, A[1010101], B[1010101];
vector<int> idx;
int M;
int used[2010101], cntv[2010101], cnte[201010];
int check(int x) {
  UnionFind uf(M);
  for (int i = 0; i < M; i++) used[i] = 0;
  for (int i = 0; i < M; i++) cnte[i] = 0;
  for (int i = 0; i < M; i++) cntv[i] = 1;
  for (int i = 0; i < N; i++) {
    if (x < A[i]) return 0;
    if (x < B[i]) cnte[A[i]]++;
  }
  for (int i = 0; i < N; i++)
    if (B[i] <= x) {
      int a = uf[A[i]];
      int b = uf[B[i]];
      if (a == b)
        cnte[a]++;
      else {
        int ce = cnte[a] + cnte[b];
        int cv = cntv[a] + cntv[b];
        uf(A[i], B[i]);
        a = uf[A[i]];
        cnte[a] = ce + 1;
        cntv[a] = cv;
      }
    }
  for (int i = 0; i < M; i++)
    if (uf[i] == i)
      if (cntv[i] < cnte[i]) return 0;
  return 1;
}
void _main() {
  cin >> N;
  for (int i = 0; i < N; i++) cin >> A[i] >> B[i];
  for (int i = 0; i < N; i++) {
    idx.push_back(A[i]);
    idx.push_back(B[i]);
  }
  sort((idx).begin(), (idx).end());
  idx.erase(unique((idx).begin(), (idx).end()), idx.end());
  M = idx.size();
  for (int i = 0; i < N; i++) {
    A[i] = lower_bound((idx).begin(), (idx).end(), A[i]) - idx.begin();
    B[i] = lower_bound((idx).begin(), (idx).end(), B[i]) - idx.begin();
  }
  int ng = -1, ok = M;
  while (ng + 1 != ok) {
    int md = (ng + ok) / 2;
    if (check(md))
      ok = md;
    else
      ng = md;
  }
  int ans = -1;
  if (ok < M) ans = idx[ok];
  cout << ans << endl;
}
