#include <bits/stdc++.h>
using namespace std;
template <typename T>
void printvec(string s, vector<T> v) {
  cout << s << ":";
  for (int i = 0; i < v.size(); i++) cout << v[i] << " ";
  cout << endl;
}
double get_current_time() {
  timeval t;
  gettimeofday(&t, 0);
  return (double)t.tv_sec + (double)t.tv_usec / 1000000;
}
int n;
vector<vector<pair<int, int>>> graph;
vector<int> vid;
void preprocess() {
  ios::sync_with_stdio(false);
  cin >> n;
  vector<pair<int, int>> old_edge;
  old_edge.resize(n);
  for (int i = 0; i < n; i++) cin >> old_edge[i].first >> old_edge[i].second;
  for (int i = 0; i < n; i++) {
    vid.push_back(old_edge[i].first);
    vid.push_back(old_edge[i].second);
  }
  sort(vid.begin(), vid.end());
  vid.resize(unique(vid.begin(), vid.end()) - vid.begin());
  unordered_map<int, int> o2n;
  for (int i = 0; i < vid.size(); i++) {
    o2n[vid[i]] = i;
  }
  graph.resize(o2n.size());
  int from, to;
  for (int i = 0; i < n; i++) {
    from = o2n[old_edge[i].first];
    to = o2n[old_edge[i].second];
    graph[from].push_back(pair<int, int>(to, i));
    graph[to].push_back(pair<int, int>(from, i));
  }
}
int schedule() {
  vector<int> dfs_stack;
  vector<int> edge_stack;
  vector<int> j_stack;
  vector<bool> visited(graph.size(), false);
  int time = 0;
  int max_scheduled = -1;
  for (int i = 0; i < graph.size(); i++) {
    if (!visited[i]) {
      visited[i] = true;
      dfs_stack.push_back(i);
      j_stack.push_back(0);
      edge_stack.push_back(-1);
      int m1 = 0, m2 = 0;
      if (dfs_stack.back() > m1) {
        m2 = m1;
        m1 = dfs_stack.back();
      } else if (dfs_stack.back() > m2 && dfs_stack.back() < m1) {
        m2 = dfs_stack.back();
      }
      int circle_edge_id = -1;
      while (!dfs_stack.empty()) {
        if (j_stack.back() < graph[dfs_stack.back()].size()) {
          if (graph[dfs_stack.back()][j_stack.back()].second !=
              edge_stack.back()) {
            if (!visited[graph[dfs_stack.back()][j_stack.back()].first]) {
              edge_stack.push_back(
                  graph[dfs_stack.back()][j_stack.back()].second);
              dfs_stack.push_back(
                  graph[dfs_stack.back()][j_stack.back()].first);
              if (dfs_stack.back() > m1) {
                m2 = m1;
                m1 = dfs_stack.back();
              } else if (dfs_stack.back() > m2 && dfs_stack.back() < m1) {
                m2 = dfs_stack.back();
              }
              visited[dfs_stack.back()] = true;
              j_stack.back()++;
              j_stack.push_back(0);
            } else {
              if (circle_edge_id < 0) {
                circle_edge_id = graph[dfs_stack.back()][j_stack.back()].second;
              } else if (circle_edge_id !=
                         graph[dfs_stack.back()][j_stack.back()].second) {
                return -1;
              }
              j_stack.back()++;
            }
          } else {
            j_stack.back()++;
          }
        } else {
          dfs_stack.pop_back();
          j_stack.pop_back();
          edge_stack.pop_back();
        }
      }
      if (circle_edge_id < 0) {
        max_scheduled = max(max_scheduled, m2);
      } else {
        max_scheduled = max(max_scheduled, m1);
      }
    }
  }
  return vid[max_scheduled];
}
vector<bool> vvisited;
vector<bool> evisited;
int circlenum = 0;
int m1 = -1, m2 = -1;
int maxvalue = -1;
int dfs(int v) {
  vvisited[v] = true;
  if (v > m1) {
    m2 = m1;
    m1 = v;
  } else if (v > m2) {
    m2 = v;
  }
  for (int i = 0; i < graph[v].size(); ++i) {
    if (!evisited[graph[v][i].second]) {
      evisited[graph[v][i].second] = true;
      if (vvisited[graph[v][i].first]) {
        circlenum++;
      } else {
        dfs(graph[v][i].first);
        if (circlenum > 1) return -1;
      }
    }
  }
  return 0;
}
int call() {
  vvisited.resize(graph.size(), false);
  evisited.resize(n, false);
  for (int i = 0; i < graph.size(); i++) {
    if (!vvisited[i]) {
      circlenum = 0;
      m1 = -1;
      m2 = -1;
      if (dfs(i) == -1)
        return -1;
      else {
        if (circlenum == 0) {
          maxvalue = max(m2, maxvalue);
        } else {
          maxvalue = max(m1, maxvalue);
        }
      }
    }
  }
  return maxvalue;
}
int main() {
  preprocess();
  cout << call();
  return 0;
}
