#include <bits/stdc++.h>
using namespace std;
const int N = 2e6 + 6;
int a[N], b[N], mark[N], cnt, e, mx, mx2;
vector<int> P, g[N];
void dfs(int v) {
  mark[v] = 1, cnt++, e += (int)g[v].size();
  if (P[v] > mx)
    mx2 = mx, mx = P[v];
  else if (P[v] > mx2)
    mx2 = P[v];
  for (int u : g[v])
    if (!mark[u]) dfs(u);
  return;
}
int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i] >> b[i];
    P.push_back(a[i]), P.push_back(b[i]);
  }
  sort(P.begin(), P.end()), P.resize(unique(P.begin(), P.end()) - P.begin());
  for (int i = 0; i < n; i++) {
    a[i] = lower_bound(P.begin(), P.end(), a[i]) - P.begin();
    b[i] = lower_bound(P.begin(), P.end(), b[i]) - P.begin();
    g[a[i]].push_back(b[i]), g[b[i]].push_back(a[i]);
  }
  int ans = 0;
  for (int i = 0; i < (int)P.size(); i++)
    if (!mark[i]) {
      cnt = 0, e = 0, mx = 0, mx2 = 0, dfs(i), e >>= 1;
      if (cnt < e) return cout << -1 << "\n", 0;
      if (cnt == e)
        ans = max(ans, mx);
      else
        ans = max(ans, mx2);
    }
  cout << ans << "\n";
}
