#include <bits/stdc++.h>
using namespace std;
int n, a, b, wyn = 1;
long long kt = 1;
long long odw[1000009];
int wyny[1000009];
pair<int, int> tab[1000009];
pair<int, int> tab1[1000009];
map<int, int> m;
vector<int> zapy;
int skoj(int from, int to) {
  odw[from] = kt;
  if (m[tab[from].second] == -1) {
    m[tab[from].second] = from;
    wyny[from] = tab[from].second;
    return 1;
  }
  if (m[tab[from].first] == -1 && from <= to) {
    m[tab[from].first] = from;
    wyny[from] = tab[from].first;
    return 1;
  }
  if (odw[m[tab[from].second]] < kt && skoj(m[tab[from].second], to)) {
    m[tab[from].second] = from;
    wyny[from] = tab[from].second;
    return 1;
  }
  if (from <= to) {
    if (odw[m[tab[from].first]] < kt && skoj(m[tab[from].first], to)) {
      m[tab[from].first] = from;
      wyny[from] = tab[from].first;
      return 1;
    }
  }
  return 0;
}
int bin(int l, int p) {
  int second = (l + p) / 2;
  int wyn = 1;
  int mini = -1;
  while (l <= p) {
    for (int i = 0; i < n; ++i) {
      odw[i] = 0;
      m[tab[i].first] = -1;
      m[tab[i].second] = -1;
    }
    kt = 1;
    second = (l + p) / 2;
    wyn = 1;
    for (int i = 0; i < n; ++i) {
      wyn = (wyn & skoj(i, second));
      kt++;
    }
    if (wyn) {
      for (int i = 0; i < n; ++i) {
        mini = max(mini, wyny[i]);
      }
      p = second - 1;
    } else {
      l = second + 1;
    }
  }
  return mini;
}
int main() {
  scanf("%d", &n);
  for (int i = 0; i < n; ++i) {
    scanf("%d", &a);
    scanf("%d", &b);
    tab[i] = make_pair(b, a);
    tab1[i] = make_pair(a, b);
  }
  sort(tab, tab + n);
  sort(tab1, tab1 + n);
  int boi = bin(-1, n - 1);
  if (boi == -1) {
    printf("-1");
  } else {
    if (boi == -1) {
      printf("%d", boi);
    } else {
      printf("%d", boi);
    }
  }
  return 0;
}
