#include <bits/stdc++.h>
using namespace std;
const int N = 2e6 + 101010;
const long long mod = 998244353;
int powmod(int a, int b) {
  int res = 1;
  assert(b >= 0);
  while (b > 0) {
    if (b & 1) res = res * 1ll * a % mod;
    a = a * 1ll * a % mod;
    b >>= 1;
  }
  return res;
}
inline void add(int &a, int b) {
  a += b;
  if (a >= mod) a -= mod;
}
int n, k, a[N], b[N], nn;
int rev[N];
void cc() {
  map<int, int> m;
  int cnt = 0;
  set<int> st;
  for (int i = 0; i < n; ++i) {
    st.insert(a[i]);
    st.insert(b[i]);
  }
  for (int x : st) {
    m[x] = ++cnt;
    rev[cnt] = x;
  }
  nn = (int)st.size();
  for (int i = 0; i < n; ++i) {
    a[i] = m[a[i]];
    b[i] = m[b[i]];
  }
}
int edg[N], cnt[N], ma[N];
int f[N];
int find(int x) { return x == f[x] ? x : f[x] = find(f[x]); }
vector<int> vec[N];
int main(int argc, const char *argv[]) {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> n;
  for (int i = 0; i < n; ++i) {
    cin >> a[i] >> b[i];
  }
  cc();
  for (int i = 1; i <= nn; ++i) f[i] = i;
  for (int i = 0; i < n; ++i) {
    f[find(a[i])] = find(b[i]);
  }
  for (int i = 0; i < n; ++i) {
    ++edg[find(a[i])];
  }
  for (int i = 1; i <= nn; ++i) {
    ++cnt[find(i)];
    ma[find(i)] = max(ma[find(i)], i);
    vec[find(i)].push_back(i);
  }
  int ans = -1;
  for (int i = 1; i <= nn; ++i)
    if (i == f[i]) {
      if (vec[i].size() == 1) assert(0);
      sort(vec[i].begin(), vec[i].end());
      if (edg[i] > cnt[i]) {
        cout << "-1\n";
        return 0;
      }
      if (edg[i] == cnt[i]) {
        ans = max(ans, rev[ma[i]]);
      } else {
        ans = max(ans, rev[vec[i][vec[i].size() - 2]]);
      }
    }
  cout << ans << "\n";
  return 0;
}
