Polycarp studies in Berland State University. Soon he will have to take his exam. He has to pass exactly n exams.

For the each exam i there are known two days: a_i — day of the first opportunity to pass the exam, b_i — day of the second opportunity to pass the exam (a_i < b_i). Polycarp can pass at most one exam during each day. For each exam Polycarp chooses by himself which day he will pass this exam. He has to pass all the n exams.

Polycarp wants to pass all the exams as soon as possible. Print the minimum index of day by which Polycarp can pass all the n exams, or print -1 if he cannot pass all the exams at all.

Input

The first line of the input contains one integer n (1 ≤ n ≤ 10^6) — the number of exams.

The next n lines contain two integers each: a_i and b_i (1 ≤ a_i < b_i ≤ 10^9), where a_i is the number of day of the first passing the i-th exam and b_i is the number of day of the second passing the i-th exam.

Output

If Polycarp cannot pass all the n exams, print -1. Otherwise print the minimum index of day by which Polycarp can do that.

Examples

Input

2
1 5
1 7


Output

5


Input

3
5 13
1 5
1 7


Output

7


Input

3
10 40
40 80
10 80


Output

80


Input

3
99 100
99 100
99 100


Output

-1
#include <bits/stdc++.h>
using namespace std;
constexpr long long N = 1e6 + 5;
constexpr long long M = 998244353;
struct IO {
  char buf[(1 << 20)], *p1, *p2;
  char pbuf[(1 << 20)], *pp;
  IO() : p1(buf), p2(buf), pp(pbuf) {}
  ~IO() { fwrite(pbuf, 1, pp - pbuf, stdout); }
  inline char gc() {
    if (p1 == p2) p2 = (p1 = buf) + fread(buf, 1, (1 << 20), stdin);
    return p1 == p2 ? ' ' : *p1++;
  }
  inline bool blank(char ch) {
    return ch == ' ' || ch == '\n' || ch == '\r' || ch == '\t';
  }
  template <class T>
  inline void read(T &x) {
    long double tmp = 1;
    bool sign = 0;
    x = 0;
    char ch = gc();
    for (; !(ch >= '0' && ch <= '9'); ch = gc())
      if (ch == '-') sign = 1;
    for (; (ch >= '0' && ch <= '9'); ch = gc()) x = x * 10 + (ch - '0');
    if (ch == '.')
      for (ch = gc(); (ch >= '0' && ch <= '9'); ch = gc())
        tmp /= 10.0, x += tmp * (ch - '0');
    if (sign) x = -x;
  }
  inline void read(char *s) {
    char ch = gc();
    for (; blank(ch); ch = gc())
      ;
    for (; !blank(ch); ch = gc()) *s++ = ch;
    *s = 0;
  }
  inline void read(char &c) {
    for (c = gc(); blank(c); c = gc())
      ;
  }
  inline void push(const char &c) {
    if (pp - pbuf == (1 << 20)) fwrite(pbuf, 1, (1 << 20), stdout), pp = pbuf;
    *pp++ = c;
  }
  template <class T>
  inline void write(T x) {
    if (x < 0) x = -x, push('-');
    static T sta[35];
    T top = 0;
    do {
      sta[top++] = x % 10, x /= 10;
    } while (x);
    while (top) push(sta[--top] + '0');
  }
  template <class T>
  inline void write(T x, char lastChar) {
    write(x), push(lastChar);
  }
} io;
long long n, m;
pair<long long, long long> a[N];
long long b[N * 2];
long long fa[N * 2];
long long sz[N * 2];
long long e[N * 2];
long long maxi[N * 2];
long long maxi2[N * 2];
long long find(long long x) {
  if (fa[x] == x) return x;
  return fa[x] = find(fa[x]);
}
void uni(long long x, long long y) {
  long long px = find(x), py = find(y);
  if (px == py) {
    e[px]++;
    return;
  }
  if (sz[px] > sz[py]) swap(px, py);
  sz[py] += sz[px];
  e[py] += e[px] + 1;
  fa[px] = py;
  long long candi = min(maxi[px], maxi[py]);
  maxi[py] = max(maxi[py], maxi[px]);
  maxi2[py] = max(candi, max(maxi2[px], maxi2[py]));
}
signed main() {
  ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
  ;
  io.read(n);
  for (long long i = 1; i <= n; i++) {
    io.read(a[i].first), io.read(a[i].second);
    b[(i << 1) - 1] = a[i].first;
    b[i << 1] = a[i].second;
  }
  sort(b + 1, b + (n << 1) + 1);
  m = unique(b + 1, b + (n << 1) + 1) - (b + 1);
  for (long long i = 1; i <= m; i++) fa[i] = i, sz[i] = 1, maxi[i] = i;
  for (long long i = 1; i <= n; i++) {
    long long u = lower_bound(b + 1, b + m + 1, a[i].first) - b;
    long long v = lower_bound(b + 1, b + 1 + m, a[i].second) - b;
    uni(u, v);
  }
  long long ans = 0;
  for (long long i = 1; i <= m; i++) {
    if (fa[i] == i) {
      if (e[i] == sz[i] - 1)
        ans = max(ans, maxi2[i]);
      else if (e[i] == sz[i])
        ans = max(ans, maxi[i]);
      else {
        io.write(-1, '\n');
        return 0;
      }
    }
  }
  io.write(b[ans], '\n');
  return 0;
}
