#include <bits/stdc++.h>
using namespace std;
inline int read(void) {
  register int x = 0, sgn = 1, ch = getchar();
  while (ch < 48 || 57 < ch) {
    if (ch == 45) sgn = 0;
    ch = getchar();
  }
  while (47 < ch && ch < 58) {
    x = x * 10 + ch - 48;
    ch = getchar();
  }
  return sgn ? x : -x;
}
inline void write(int x) {
  if (x < 0) putchar('-'), x = -x;
  if (x > 9) write(x / 10);
  putchar(x % 10 + '0');
}
int n;
int rnk[2000005], l[1000005], r[1000005], fa[2000005];
int Getfa(int x) {
  if (fa[x] != x) fa[x] = Getfa(fa[x]);
  return fa[x];
}
signed main() {
  n = read();
  for (register int i = 1; i <= n; ++i) {
    l[i] = read();
    r[i] = read();
  }
  for (register int i = 1; i <= n; ++i) {
    rnk[2 * i - 1] = l[i];
    rnk[2 * i] = r[i];
  }
  sort(rnk + 1, rnk + 2 * n + 1);
  int s = unique(rnk + 1, rnk + 2 * n + 1) - rnk;
  for (register int i = 1; i <= n; ++i) {
    l[i] = lower_bound(rnk + 1, rnk + s, l[i]) - rnk;
    r[i] = lower_bound(rnk + 1, rnk + s, r[i]) - rnk;
  }
  for (register int i = 1; i <= s; ++i) fa[i] = i;
  for (register int i = 1; i <= n; ++i) {
    int Lfa = Getfa(l[i]);
    int Rfa = Getfa(r[i]);
    if (Lfa == 0 && Rfa == 0) {
      puts("-1");
      return 0;
    }
    if (Lfa == 0) {
      fa[Rfa] = 0;
      continue;
    }
    if (Rfa == 0) {
      fa[Lfa] = 0;
      continue;
    }
    if (Lfa == Rfa) {
      fa[Lfa] = 0;
      continue;
    }
    if (Lfa < Rfa) {
      fa[Lfa] = Rfa;
      fa[l[i]] = Rfa;
    } else {
      fa[Rfa] = Lfa;
      fa[r[i]] = Lfa;
    }
  }
  for (register int i = s; i >= 0; --i)
    if (fa[i] != i) {
      write(rnk[i]);
      puts("");
      return 0;
    }
  return 0;
}
