#include <bits/stdc++.h>
using namespace std;
const int N = 2e6 + 5;
int n, m, a[N], b[N];
int par[N], exam[N], day[N], c[N], d[N];
vector<int> vc;
int find(int x) {
  if (par[x] == x) return x;
  return par[x] = find(par[x]);
}
bool check(int t) {
  for (int i = 0; i < m; i++) {
    par[i] = i;
    day[i] = 1;
    exam[i] = 0;
  }
  for (int i = 1; i <= n; i++) {
    if (a[i] > t && b[i] > t) return 0;
    int x = -1, y = -1;
    if (a[i] <= t && b[i] <= t) {
      x = find(c[i]);
      y = find(d[i]);
      if (x == y)
        exam[x]++;
      else {
        par[y] = x;
        day[x] += day[y];
        exam[x] += exam[y] + 1;
      }
    } else if (a[i] <= t) {
      x = find(a[i]);
      exam[x]++;
    } else {
      x = find(b[i]);
      exam[x]++;
    }
    if (exam[x] > day[x]) return 0;
  }
  return 1;
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d %d", &a[i], &b[i]);
    vc.push_back(a[i]);
    vc.push_back(b[i]);
  }
  sort(vc.begin(), vc.end());
  m = unique(vc.begin(), vc.end()) - vc.begin();
  vc.resize(m);
  for (int i = 1; i <= n; i++) {
    c[i] = lower_bound(vc.begin(), vc.end(), a[i]) - vc.begin();
    d[i] = lower_bound(vc.begin(), vc.end(), b[i]) - vc.begin();
  }
  int l = 0, r = 1e9 + 10, mid, ans = -1;
  while (l <= r) {
    mid = (l + r) / 2;
    if (check(mid)) {
      r = mid - 1;
      ans = mid;
    } else
      l = mid + 1;
  }
  printf("%d\n", ans);
}
