#include <bits/stdc++.h>
using namespace std;
const int N = 2e6 + 5;
vector<pair<int, int>> e[N];
int a[N], b[N], c[N], vis[N], fl[N], tot, cnt;
vector<int> vec;
void dfs(int u) {
  vis[u] = 1;
  vec.push_back(u);
  for (auto p : e[u]) {
    int v = p.first;
    if (!vis[v]) dfs(v);
    if (!fl[p.second]) fl[p.second] = 1, ++cnt;
  }
}
signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0), cout.tie(0);
  int n;
  cin >> n;
  for (int i = 1; i <= n; ++i)
    cin >> a[i] >> b[i], c[++tot] = a[i], c[++tot] = b[i];
  sort(c + 1, c + tot + 1);
  tot = unique(c + 1, c + tot + 1) - c - 1;
  for (int i = 1; i <= n; ++i)
    a[i] = lower_bound(c + 1, c + tot + 1, a[i]) - c,
    b[i] = lower_bound(c + 1, c + tot + 1, b[i]) - c,
    e[a[i]].push_back({b[i], i}), e[b[i]].push_back({a[i], i});
  int ans = 0;
  for (int i = 1; i <= tot; ++i)
    if (!vis[i]) {
      cnt = 0, vec.clear();
      dfs(i);
      sort(vec.begin(), vec.end());
      if (vec.size() < cnt) return cout << "-1\n", 0;
      if (cnt + 1 == vec.size())
        ans = max(ans, vec[vec.size() - 2]);
      else
        ans = max(ans, vec.back());
    }
  cout << c[ans] << "\n";
  return 0;
}
