#include <bits/stdc++.h>
using namespace std;
const int MXN = 2e6 + 1;
vector<pair<int, int> > exam;
int par[MXN], vert[MXN], edge[MXN];
int tmp[MXN];
int n, mxday;
void compress() {
  int sz = 0;
  for (int i = 0; i < n; i++) {
    tmp[sz++] = exam[i].first;
    tmp[sz++] = exam[i].second;
  }
  sort(tmp, tmp + sz);
  int uniquesz = unique(tmp, tmp + sz) - tmp;
  for (int i = 0; i < n; i++) {
    exam[i].first = lower_bound(tmp, tmp + uniquesz, exam[i].first) - tmp + 1;
    exam[i].second = lower_bound(tmp, tmp + uniquesz, exam[i].second) - tmp + 1;
  }
}
void dsu_prep() {
  for (int i = 1; i <= mxday; i++) {
    par[i] = i;
    vert[i] = 1;
    edge[i] = 0;
  }
}
int get_par(int v) { return (par[v] == v ? v : par[v] = get_par(par[v])); }
int unite(int v, int u) {
  v = get_par(v);
  u = get_par(u);
  if (v == u) {
    edge[v]++;
    return v;
  }
  if (vert[v] < vert[u]) swap(v, u);
  vert[v] += vert[u];
  edge[v] += edge[u] + 1;
  par[u] = v;
  return v;
}
void clear(int d) {
  for (int i = 1; i <= d; i++) {
    vert[i] = 1;
    edge[i] = 0;
    par[i] = i;
  }
}
bool check(int d) {
  for (int i = 0; i < n; i++) {
    int op = exam[i].first, en = exam[i].second;
    if (op > d) return 0;
    if (en > d) {
      int v = unite(op, op);
      if (edge[v] > vert[v]) return 0;
    } else {
      int v = unite(op, en);
      if (edge[v] > vert[v]) return 0;
    }
  }
  return 1;
}
int solve(int l, int r) {
  int lst = r;
  if (!check(r)) return -1;
  clear(lst);
  lst = l;
  if (check(l)) return l;
  while (r - l > 1) {
    clear(lst);
    int m = (l + r) / 2;
    if (check(m)) {
      r = m;
    } else {
      l = m;
    }
    lst = m;
  }
  return r;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  cin >> n;
  for (int i = 0; i < n; i++) {
    int a, b;
    cin >> a >> b;
    exam.push_back({a, b});
  }
  compress();
  for (int i = 0; i < n; i++) {
    mxday = max(mxday, exam[i].second);
  }
  dsu_prep();
  int ans = solve(1, mxday);
  cout << (ans == -1 ? ans : tmp[ans - 1]) << "\n";
}
