#include <bits/stdc++.h>
using namespace std;
const int N = 1 << 20;
int A[N], B[N], C[N], M[N], P[N], n;
int Get(int x) { return (P[x] ? Get(P[x]) : x); }
void Add(int x, int y) {
  x = Get(x);
  y = Get(y);
  if (x < y) swap(x, y);
  if (x == y) {
    C[x]++;
    return;
  }
  C[x] += C[y];
  M[x] = max({M[x], M[y], y});
  P[y] = x;
}
int main() {
  ios::sync_with_stdio(0), cin.tie(0);
  cin >> n;
  vector<int> vec;
  vec.push_back(0);
  for (int i = 0; i < n; i++) cin >> A[i] >> B[i];
  for (int i = 0; i < n; i++) {
    vec.push_back(A[i]);
    vec.push_back(B[i]);
  }
  sort(vec.begin(), vec.end());
  vec.resize(unique(vec.begin(), vec.end()) - vec.begin());
  for (int i = 0; i < n; i++) {
    A[i] = lower_bound(vec.begin(), vec.end(), A[i]) - vec.begin();
    B[i] = lower_bound(vec.begin(), vec.end(), B[i]) - vec.begin();
    Add(A[i], B[i]);
  }
  int ans = 0;
  for (int i = 0; i < vec.size(); i++) {
    if (P[i]) continue;
    if (C[i] > 1) return cout << -1, 0;
    if (C[i])
      ans = max({ans, i, M[i]});
    else
      ans = max(ans, M[i]);
  }
  cout << vec[ans];
}
