#include <bits/stdc++.h>
using namespace std;
set<int> s;
vector<int> p;
int pre[2000006], x[2000006], y[2000006], b[2000006];
vector<int> d[2000006];
int find(int x) { return (x == pre[x]) ? x : (pre[x] = find(pre[x])); }
int main() {
  int n, tag = 0, ans = 0;
  cin >> n;
  for (int i = 1; i <= 2000000; i++) {
    pre[i] = i;
  }
  for (int i = 1; i <= n; i++) {
    scanf("%d%d", &x[i], &y[i]);
    s.insert(x[i]);
    s.insert(y[i]);
  }
  p.push_back(0);
  for (auto i : s) {
    p.push_back(i);
  }
  for (int i = 1; i <= n; i++) {
    int l = lower_bound(p.begin(), p.end(), x[i]) - p.begin();
    int r = lower_bound(p.begin(), p.end(), y[i]) - p.begin();
    l = find(l);
    r = find(r);
    if (l != r) {
      pre[r] = l;
      b[l] += b[r];
    }
    b[l]++;
  }
  for (int i = 1; i <= 2000000; i++) {
    d[find(i)].push_back(i);
  }
  for (int i = 1; i <= 2000000; i++) {
    if (d[i].size() <= 1 || i != find(i)) continue;
    if (d[i].size() < b[i]) {
      tag = 1;
      break;
    } else if (d[i].size() == b[i]) {
      for (auto j : d[i]) {
        ans = max(ans, p[j]);
      }
    } else if (d[i].size() == b[i] + 1) {
      int mx1 = 0, mx2 = 0;
      for (auto j : d[i]) {
        if (p[j] > mx1) {
          mx2 = mx1;
          mx1 = p[j];
        } else if (mx2 < p[j]) {
          mx2 = p[j];
        }
      }
      ans = max(ans, mx2);
    }
  }
  if (tag)
    cout << -1 << endl;
  else
    cout << ans << endl;
}
