#include <bits/stdc++.h>
#pragma GCC optimize("O3")
const int MAX = (int)2e6 + 1;
namespace DSU {
std::array<int, MAX> pa;
std::array<bool, MAX> has;
void init(int MAX) {
  for (int i = 0; i < MAX; ++i) {
    pa[i] = i, has[i] = 0;
  }
}
std::vector<int> stack;
int finPa(int p) {
  while (pa[p] != p) {
    stack.push_back(p);
    p = pa[p];
  }
  for (int u : stack) {
    pa[u] = p;
  }
  stack.clear();
  return p;
}
}  // namespace DSU
char getChar() {
  static char buf[1 << 18];
  static int size = 0;
  static int pos = 0;
  if (pos == size) {
    size = (int)fread(buf, 1, 1 << 18, stdin);
    pos = 0;
  }
  if (pos == size) {
    return EOF;
  }
  return buf[pos++];
}
int getInt() {
  char c = '?';
  while (!(c == '-' || ('0' <= c && c <= '9'))) {
    c = getChar();
  }
  bool positive = true;
  if (c == '-') {
    positive = false;
    c = getChar();
  }
  int ret = 0;
  while ('0' <= c && c <= '9') {
    (ret *= 10) += (c - '0');
    c = getChar();
  }
  return positive ? ret : -ret;
}
struct Pair {
  int F, S;
};
int main() {
  int n = getInt(), c = 0, MAX;
  std::vector<Pair> ara(n), in(n);
  {
    std::vector<int> uni{0};
    for (int i = 0; i < n; ++i) {
      ara[i].F = getInt();
      ara[i].S = getInt();
      uni.push_back(ara[i].F);
      uni.push_back(ara[i].S);
    }
    std::stable_sort(uni.begin(), uni.end());
    uni.erase(std::unique(uni.begin(), uni.end()), uni.end());
    for (int i = 0; i < n; ++i) {
      int u = ara[i].F, v = ara[i].S;
      u = int(std::lower_bound(uni.begin(), uni.end(), u) - uni.begin());
      v = int(std::lower_bound(uni.begin(), uni.end(), v) - uni.begin());
      in[i] = Pair{u, v};
    }
    MAX = (int)uni.size();
  }
  int st = 1, en = 1e9 + 1, mid, show = -1;
  int COUNTER = 0;
  while (st <= en) {
    ++COUNTER;
    assert(COUNTER <= 30);
    mid = (st + en) >> 1;
    DSU::init(MAX);
    c = 0;
    DSU::has[0] = 1;
    for (int i = 0; i < n; ++i) {
      int u = ara[i].F, v = ara[i].S;
      if (u > mid)
        u = 0;
      else
        u = in[i].F;
      if (v > mid)
        v = 0;
      else
        v = in[i].S;
      int r1 = DSU::finPa(u), r2 = DSU::finPa(v);
      if (!(DSU::has[r1] && DSU::has[r2])) {
        c++;
        if (r1 != r2)
          DSU::has[r1] = DSU::has[r1] || DSU::has[r2];
        else
          DSU::has[r1] = 1;
        DSU::pa[r2] = r1;
      }
    }
    if (c == n)
      en = mid - 1, show = show == -1 ? mid : std::min(show, mid);
    else
      st = mid + 1;
  }
  std::cout << show << std::endl;
  return 0;
}
