#include <bits/stdc++.h>
using std::abs;
using std::bitset;
using std::cerr;
using std::cin;
using std::cout;
using std::map;
using std::max;
using std::min;
using std::pair;
using std::set;
using std::sort;
using std::string;
using std::swap;
using std::unordered_map;
using std::unordered_set;
using std::vector;
using ll = long long;
using uint = unsigned int;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using ull = unsigned long long;
using ld = long double;
template <typename T>
void _(const char* s, T h) {
  cerr << s << " = " << h << "\n";
}
template <typename T, typename... Ts>
void _(const char* s, T h, Ts... t) {
  int b = 0;
  while (((b += *s == '(') -= *s == ')') != 0 || *s != ',') cerr << *s++;
  cerr << " = " << h << ",";
  _(s + 1, t...);
}
struct init {
  init() {
    cin.tie(0);
    std::iostream::sync_with_stdio(0);
    cout << std::fixed << std::setprecision(10);
    cerr << std::fixed << std::setprecision(5);
  }
  ~init() {}
} init;
template <typename T, typename U>
void upx(T& x, U y) {
  if (x < y) x = y;
}
template <typename T, typename U>
void upn(T& x, U y) {
  if (x > y) x = y;
}
const ll N = 2e6 + 1;
ll p[N], size[N], es[N];
ll get(ll x) { return x == p[x] ? x : (p[x] = get(p[x])); }
ll a[N], b[N];
void join(ll x, ll y) {
  x = get(x);
  y = get(y);
  if (x ^ y) {
    p[y] = x;
    size[x] += size[y];
    es[x] += es[y] + 1;
  } else {
    es[x]++;
  }
}
ll xs[N], mx[N], sx[N];
int32_t main() {
  ll n, m = 0;
  cin >> n;
  for (ll i = 0; i < (n); ++i) {
    cin >> a[i] >> b[i];
    xs[m++] = a[i];
    xs[m++] = b[i];
  }
  sort(xs, xs + m);
  m = std::unique(xs, xs + m) - xs;
  for (ll i = 0; i < (m); ++i) {
    p[i] = i;
    size[i] = 1;
    es[i] = 0;
  }
  for (ll i = 0; i < (n); ++i) {
    ll x = std::lower_bound(xs, xs + m, a[i]) - xs;
    ll y = std::lower_bound(xs, xs + m, b[i]) - xs;
    join(x, y);
  }
  for (ll i = 0; i < (m); ++i) {
    ll c = get(i);
    upx(sx[c], xs[i]);
    if (sx[c] > mx[c]) swap(sx[c], mx[c]);
  }
  ll ans = 0;
  for (ll i = 0; i < (m); ++i) {
    if (i == get(i)) {
      ;
      if (es[i] > size[i]) {
        ans = -1;
        break;
      }
      if (es[i] == size[i])
        upx(ans, mx[i]);
      else
        upx(ans, sx[i]);
    }
  }
  cout << ans << '\n';
  return 0;
}
