#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;
}
