#include <bits/stdc++.h>
using namespace std;
pair<int, int> p[1000000];
int a[1000000];
int main() {
  int n;
  cin >> n;
  map<int, unordered_set<int> > mp;
  set<int> s;
  for (int i = 0; i < n; i++) {
    cin >> p[i].first >> p[i].second;
    mp[p[i].first].insert(i);
    mp[p[i].second].insert(i);
    s.insert(p[i].first);
    s.insert(p[i].second);
  }
  deque<int> dq;
  for (auto& elem : mp) {
    if (elem.second.size() == 1) dq.push_back(elem.first);
  }
  int m = mp.size(), h = 0;
  unordered_map<int, int> dk;
  while (m && h < n) {
    int f = 0;
    while (!dq.empty()) {
      int r = dq.front();
      dq.pop_front();
      if (mp.find(r) == mp.end() || mp[r].size() == 0) {
        break;
      }
      int i = *mp[r].begin();
      mp.erase(r);
      a[i] = r;
      dk[r] = i;
      if (p[i].first == r) {
        if (mp.find(p[i].second) != mp.end()) mp[p[i].second].erase(i);
        if (mp[p[i].second].size() == 1) dq.push_back(p[i].second);
      } else {
        if (mp.find(p[i].first) != mp.end()) mp[p[i].first].erase(i);
        if (mp[p[i].first].size() == 1) dq.push_back(p[i].first);
      }
      m--;
      h++;
      if (m == 0 || h == n) break;
    }
    while (dq.empty() && m > 0 && h < n) {
      auto it = mp.begin();
      auto jt = it->second.begin();
      if (it == mp.end() || jt == it->second.end()) {
        f = 1;
        break;
      }
      int r = it->first;
      int i = *jt;
      mp.erase(r);
      a[i] = r;
      dk[r] = i;
      if (p[i].first == r) {
        mp[p[i].second].erase(i);
        if (mp[p[i].second].size() == 1) dq.push_back(p[i].second);
      } else {
        mp[p[i].first].erase(i);
        if (mp[p[i].first].size() == 1) dq.push_back(p[i].first);
      }
      m--;
      h++;
      if (m == 0 || h == n) break;
    }
    if (f) break;
  }
  if (h != n) {
    cout << "-1\n";
    return 0;
  }
  int prev = *s.rbegin();
  while (1) {
    s.erase(prev);
    int ind = dk[prev];
    dk.erase(prev);
    int f = 0;
    while (1) {
      int d = (a[ind] == p[ind].first) ? p[ind].second : p[ind].first;
      if (d >= prev) {
        f = 1;
        break;
      }
      if (dk.find(d) == dk.end()) {
        a[ind] = d;
        dk[d] = ind;
        prev = *s.rbegin();
        break;
      }
      int ind2 = dk[d];
      a[ind] = d;
      dk[d] = ind;
      int d2 = (d == p[ind2].first) ? p[ind2].second : p[ind2].first;
      if (d2 > prev) {
        f = 1;
        break;
      }
      if (dk.find(d2) == dk.end()) {
        a[ind2] = d2;
        dk[d2] = ind2;
        prev = *s.rbegin();
        break;
      } else {
        ind = ind2;
      }
    }
    if (f) break;
  }
  cout << prev << "\n";
}
