#include <bits/stdc++.h>   using namespace std;   class union_find {    public:     vector<int> arr;     union_find(int n) {       arr.resize(n);       fill(arr.begin(), arr.end(), -1);     }     int find(int x) { return arr[x] < 0 ? x : arr[x] = find(arr[x]); }     void unite(int x, int y) {       x = find(x), y = find(y);       if (x == y) return;       if (arr[x] > arr[y]) swap(x, y);       arr[x] += arr[y];       arr[y] = x;     }     bool connected(int x, int y) { return find(x) == find(y); }     int size(int x) { return -arr[find(x)]; }   };   template <typename T>   vector<int> coordinate_compression(const T &a, int cur = 0) {     int n = a.size();     T b = a;     sort(b.begin(), b.end());     b.erase(unique(b.begin(), b.end()), b.end());     vector<int> res(n);     for (int i = 0; i < n; i++)       res[i] = lower_bound(b.begin(), b.end(), a[i]) - b.begin() + cur;     return res;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     int n;     cin >> n;     vector<long long> a(2 * n);     for (int i = 0; i < a.size(); i++) cin >> a[i];     vector<int> b = coordinate_compression(a);     union_find uf(2 * n);     vector<int> mx0(2 * n), mx1(2 * n, -1);     for (int i = 0; i < 2 * n; i++) mx0[i] = i;     vector<bool> ju(2 * n);     for (int i = 0; i < n; i++) {       int u = b[2 * i], v = b[2 * i + 1];       if (uf.connected(u, v)) {         if (ju[uf.find(u)]) {           cout << -1 <<   n ;           return 0;         }         ju[uf.find(u)] = true;       } else {         int ru = uf.find(u), rv = uf.find(v);         uf.unite(u, v);         mx1[uf.find(u)] = max(max(mx1[ru], mx1[rv]), min(mx0[ru], mx0[rv]));         mx0[uf.find(u)] = max(mx0[ru], mx0[rv]);       }     }     int res = 0;     for (int i = 0; i < 2 * n; i++) {       if (ju[uf.find(i)]) {         res = max(res, mx0[uf.find(i)]);       } else {         res = max(res, mx1[uf.find(i)]);       }     }     for (int i = 0; i < 2 * n; i++) {       if (res == b[i]) {         cout << a[i] <<   n ;         return 0;       }     }     return 0;   }   