#include <bits/stdc++.h>
using namespace std;
int n;
vector<pair<int, int>> e;
map<int, int> m;
vector<int> x;
bool fail[2000005];
vector<int> cc[2000005];
int fa[2000005];
int find(int x) {
  if (fa[x] == x) return x;
  return fa[x] = find(fa[x]);
}
void unite(int u, int v) {
  u = find(u);
  v = find(v);
  if (u != v) {
    fa[u] = v;
    fail[v] |= fail[u];
    fail[u] |= fail[v];
  } else {
    if (fail[u]) throw(0);
    fail[u] = true;
  }
}
int main() {
  scanf("%d", &n);
  for (int i = 0; i < (n); i++) {
    int u, v;
    scanf("%d%d", &u, &v);
    e.emplace_back(u, v);
    x.push_back(u);
    x.push_back(v);
  }
  sort((x).begin(), (x).end());
  x.resize(unique((x).begin(), (x).end()) - x.begin());
  for (int i = 0; i < (x.size()); i++) {
    m[x[i]] = i;
    fa[i] = i;
  }
  try {
    for (auto& p : e) {
      p.first = m[p.first];
      p.second = m[p.second];
      unite(p.first, p.second);
    }
    for (int i = 0; i < (x.size()); i++) cc[find(i)].push_back(i);
    int maxv = 0;
    for (int i = 0; i < (x.size()); i++) {
      if (i != fa[i]) continue;
      if (fail[i]) {
        maxv = max(maxv, *max_element((cc[i]).begin(), (cc[i]).end()));
      } else {
        nth_element(cc[i].begin(), cc[i].begin() + 1, cc[i].end(),
                    greater<int>());
        maxv = max(maxv, cc[i][1]);
      }
    }
    cout << x[maxv] << endl;
  } catch (int) {
    cout << -1 << endl;
  }
  return 0;
}
