#include <bits/stdc++.h>
using namespace std;
const int MaxN = 1000001;
const int oo = 1e9;
int n, a[MaxN], b[MaxN], ans;
map<int, int> root;
int get_root(int u) {
  if (root[u] == 0) return u;
  return root[u] = get_root(root[u]);
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i] >> b[i];
  }
  for (int i = 1; i <= n; i++) {
    int p = get_root(a[i]), q = get_root(b[i]);
    ans = max(ans, min(p, q));
    if (ans == oo) break;
    if (p == q)
      root[p] = oo;
    else {
      if (p < q)
        root[p] = q;
      else
        root[q] = p;
    }
  }
  if (ans == oo)
    cout << "-1";
  else
    cout << ans;
  return 0;
}
