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;
inline bool SR(int &x) { return scanf("%d", &x) == 1; }
inline bool SR(long long &x) { return scanf("%lld", &x) == 1; }
inline bool SR(double &x) { return scanf("%lf", &x) == 1; }
inline bool SR(char *s) { return scanf("%s", s) == 1; }
inline bool RI() { return true; }
template <typename I, typename... T>
inline bool RI(I &x, T &...tail) {
  return SR(x) && RI(tail...);
}
inline void SP(const int x) { printf("%d", x); }
inline void SP(const long long x) { printf("%lld", x); }
inline void SP(const double x) { printf("%.16lf", x); }
inline void SP(const char *s) { printf("%s", s); }
inline void PL() { puts(""); }
template <typename I, typename... T>
inline void PL(const I x, const T... tail) {
  SP(x);
  if (sizeof...(tail)) putchar(' ');
  PL(tail...);
}
template <typename I>
void _DOING(const char *s, I &&x) {
  cerr << s << " = " << x << endl;
}
template <typename I, typename... T>
void _DOING(const char *s, I &&x, T &&...tail) {
  int c = 0;
  while (*s != ',' || c != 0) {
    if (*s == '(' || *s == '[' || *s == '{') c++;
    if (*s == ')' || *s == ']' || *s == '}') c--;
    cerr << *s++;
  }
  cerr << " = " << x << " , ";
  _DOING(s + 1, tail...);
}
const int INF = 0x3f3f3f3f;
const long long INF64 = 0x3f3f3f3f3f3f3f3fll;
inline int RAND() {
  static int x = 880301;
  return (x = x * 0xdefaced + 1) % 0x7fffffff;
}
const int MAX_N = 2000000 + 7;
int par[MAX_N], sz[MAX_N], sz2[MAX_N], mx[MAX_N], mx2[MAX_N];
int find(int x) {
  if (x == par[x])
    return x;
  else
    return par[x] = find(par[x]);
}
bool merge(int x, int y) {
  x = find(x);
  y = find(y);
  if (sz[x] < sz[y]) swap(x, y);
  sz2[x]++;
  if (x == y) return 0;
  sz[x] += sz[y];
  sz2[x] += sz2[y];
  par[y] = x;
  vector<int> v = {mx[x], mx2[x], mx[y], mx2[y]};
  sort((v).begin(), (v).end(), greater<int>());
  mx[x] = v[0], mx2[x] = v[1];
  v.clear();
  v.shrink_to_fit();
  return 1;
}
int main() {
  int n;
  RI(n);
  vector<pair<int, int> > vs;
  vector<int> rd;
  for (int i = 0; i < int(n); i++) {
    int a, b;
    RI(a, b);
    vs.push_back(make_pair(a, b));
    rd.push_back(a);
    rd.push_back(b);
  }
  sort((rd).begin(), (rd).end());
  rd.erase(unique((rd).begin(), (rd).end()), rd.end());
  for (int i = 0; i < int(((int)rd.size())); i++) {
    par[i] = i;
    sz[i] = 1;
    sz2[i] = 0;
    mx[i] = rd[i];
    mx2[i] = 0;
  }
  for (pair<int, int> p : vs) {
    int a = lower_bound((rd).begin(), (rd).end(), p.first) - rd.begin();
    int b = lower_bound((rd).begin(), (rd).end(), p.second) - rd.begin();
    merge(a, b);
  }
  int ans = 0;
  for (int i = 0; i < int(((int)rd.size())); i++) {
    if (par[i] != i) continue;
    if (sz2[i] > sz[i]) {
      PL(-1);
      return 0;
    } else if (sz2[i] == sz[i])
      ans = max(ans, mx[i]);
    else
      ans = max(ans, mx2[i]);
  }
  PL(ans);
  return 0;
}
