#include <bits/stdc++.h>
using namespace std;
using ll = long long;
map<int, int> mp;
vector<pair<int, int>> v;
int d[2000000];
int sum[2000000];
int pts[2000000];
int num;
void init(int N) {
  for (int i = 0; i < N; ++i) {
    d[i] = i;
    sum[i] = 0;
    pts[i] = 1;
  }
}
int find(int v) {
  if (d[v] == v) return v;
  return d[v] = find(d[v]);
}
void U(int a, int b) {
  a = find(a);
  b = find(b);
  if (a != b) {
    sum[b] += sum[a];
    pts[b] += pts[a];
    d[a] = b;
  }
  sum[b]++;
}
bool check(int M) {
  int s, e;
  init(num);
  for (auto &p : v) {
    tie(s, e) = p;
    if (s > M) return false;
    if (e > M)
      U(s, s);
    else
      U(s, e);
  }
  for (int i = 0; i < num; ++i) {
    if (sum[find(i)] > pts[find(i)]) return false;
  }
  return true;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int N;
  int s, e;
  cin >> N;
  for (int i = 0; i < N; ++i) {
    cin >> s >> e;
    v.emplace_back(s, e);
    mp[s] = mp[e] = 0;
  }
  for (auto &p : mp) p.second = num++;
  for (auto &p : v) {
    p.first = mp[p.first];
    p.second = mp[p.second];
  }
  int L = 0, R = num - 1;
  if (!check(R)) {
    cout << "-1\n";
  } else {
    while (L + 1 < R) {
      int M = (L + R) / 2;
      if (check(M))
        R = M;
      else
        L = M;
    }
    for (auto p : mp)
      if (p.second == R) {
        cout << p.first << '\n';
        break;
      }
  }
}
