#include <bits/stdc++.h>
using namespace std;
const int BUF = 1000005;
int n;
int test2day[BUF][2];
int test2id[BUF][2];
vector<int> id2test[BUF * 2];
void read() {
  for (int i = 0; i < BUF * 2; ++i) id2test[i].clear();
  map<int, int> day2id;
  scanf("%d", &n);
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < 2; ++j) {
      scanf("%d", &test2day[i][j]);
      if (!day2id.count(test2day[i][j])) {
        int sz = day2id.size();
        day2id[test2day[i][j]] = sz;
      }
      test2id[i][j] = day2id[test2day[i][j]];
      id2test[day2id[test2day[i][j]]].push_back(i);
    }
  }
}
void work() {
  int ans = 0;
  bool visitedTest[BUF] = {};
  bool visitedId[BUF * 2] = {};
  for (int test = 0; test < n; ++test) {
    if (visitedTest[test]) continue;
    queue<int> tQ, iQ;
    visitedTest[test] = true;
    tQ.push(test);
    int cntT = 1;
    int cntI = 0;
    vector<int> dayList;
    while (!tQ.empty() || !iQ.empty()) {
      if (!tQ.empty()) {
        int curr = tQ.front();
        tQ.pop();
        for (int i = 0; i < 2; ++i) {
          if (!visitedId[test2id[curr][i]]) {
            dayList.push_back(test2day[curr][i]);
            ++cntI;
            visitedId[test2id[curr][i]] = true;
            iQ.push(test2id[curr][i]);
          }
        }
      }
      if (!iQ.empty()) {
        int curr = iQ.front();
        iQ.pop();
        for (int i = 0; i < id2test[curr].size(); ++i) {
          if (!visitedTest[id2test[curr][i]]) {
            ++cntT;
            visitedTest[id2test[curr][i]] = true;
            tQ.push(id2test[curr][i]);
          }
        }
      }
    }
    if (cntT > cntI) {
      cout << -1 << endl;
      return;
    }
    sort(dayList.begin(), dayList.end());
    ans = max(ans, dayList[cntT - 1]);
  }
  cout << ans << endl;
}
int main() {
  read();
  work();
  return 0;
}
