#include <bits/stdc++.h>
using namespace std;
ifstream in;
ofstream out;
const long long kk = 1000;
const long long ml = kk * kk;
const long long mod = ml * kk + 7;
const long long inf = ml * ml * ml + 7;
const long long gs = 500 * kk * 2;
long n, i, j;
set<long> all;
vector<long> ril;
vector<bool> used;
vector<pair<long long, long long> > p;
vector<long> var[2 * gs + 10];
set<long> t[gs + 10];
long col[gs + 10];
bool viv = false;
void DFS(long v, long c) {
  col[v] = c;
  t[c].insert(p[v].first);
  t[c].insert(p[v].second);
  if (!used[p[v].first]) {
    used[p[v].first] = true;
    for (auto s : var[p[v].first])
      if (col[s] == -1) DFS(s, c);
  }
  if (!used[p[v].second]) {
    used[p[v].second] = true;
    for (auto s : var[p[v].second])
      if (col[s] == -1) DFS(s, c);
  }
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  for (i = 0; i < n; i++) {
    long long a, b;
    cin >> a >> b;
    all.insert(a);
    all.insert(b);
    p.push_back({a, b});
  }
  for (auto v : all) ril.push_back(v);
  for (auto &j : p)
    j.first = lower_bound(ril.begin(), ril.end(), j.first) - ril.begin(),
    j.second = lower_bound(ril.begin(), ril.end(), j.second) - ril.begin();
  for (i = 0; i < n; i++)
    var[p[i].first].push_back(i), var[p[i].second].push_back(i);
  for (i = 0; i < all.size(); i++) used.push_back(false);
  for (i = 0; i < n; i++) col[i] = -1;
  long long tc = 0;
  for (i = 0; i < n; i++)
    if (col[i] == -1) DFS(i, tc), tc++;
  vector<long long> kol(tc, 0);
  for (i = 0; i < n; i++) kol[col[i]]++;
  long long last = 0;
  for (i = 0; i < tc; i++) {
    if (kol[i] > t[i].size()) {
      cout << "-1" << endl;
      return 0;
    }
    if (kol[i] < t[i].size()) t[i].erase(t[i].find(*t[i].rbegin()));
    long long z = *t[i].rbegin();
    last = max(last, z);
  }
  cout << ril[last] << endl;
  return 0;
}
