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;
class Solution {
 public:
  int calc(vector<pair<int, int>>& exams) {
    set<int> days;
    unordered_map<int, int> indices;
    int n = exams.size();
    for (auto& p : exams) {
      days.insert(p.first);
      days.insert(p.second);
    }
    if (n > days.size()) {
      return -1;
    }
    int idx = 0;
    for (auto& d : days) {
      indices[d] = idx++;
    }
    int m = indices.size();
    vector<vector<int>> graph(m);
    for (auto& p : exams) {
      int u = indices[p.first], v = indices[p.second];
      graph[u].push_back(v);
      graph[v].push_back(u);
    }
    vector<int> visited(m, 0);
    int clock = 0;
    int res = 0;
    for (int i = 0; i < m; ++i) {
      if (visited[i] > 0) {
        continue;
      }
      int cnt = 0;
      int cand = INT_MAX;
      ++clock;
      cand = min(cand, bfs(i, graph, visited, clock, cnt));
      if (cand == -1) {
        return -1;
      }
      res = max(res, cand);
    }
    for (auto& p : indices) {
      if (p.second == res) {
        return p.first;
      }
    }
    return -1;
  }

 private:
  int bfs(int u, vector<vector<int>>& graph, vector<int>& visited, int clock,
          int& cnt) {
    visited[u] = clock;
    queue<int> q;
    q.push(u);
    cnt = 0;
    int mx = -1;
    int mx2 = -1;
    int ecnt = 0;
    int res = -1;
    while (!q.empty()) {
      int u = q.front();
      q.pop();
      ++cnt;
      ecnt += graph[u].size();
      if (u > mx) {
        mx2 = mx;
        mx = u;
      } else if (u > mx2) {
        mx2 = u;
      }
      for (auto v : graph[u]) {
        if (visited[v] <= 0) {
          visited[v] = clock;
          q.push(v);
        }
      }
    }
    ecnt /= 2;
    if (ecnt > cnt) {
      return -1;
    } else if (ecnt == cnt) {
      return mx;
    } else {
      return mx2;
    }
    return res;
  }
};
int main(int argc, char** argv) {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  vector<pair<int, int>> exams;
  exams.reserve(n);
  for (int i = 0, a = 0, b = 0; i < n; ++i) {
    cin >> a >> b;
    exams.emplace_back(a, b);
  }
  Solution sol;
  cout << sol.calc(exams) << endl;
  return 0;
}
