#include <bits/stdc++.h>
const int maxed = 1000000 + 10;
struct Node {
  int x, y;
} node[maxed];
int n, gen[maxed * 2], c[maxed * 2], sum[maxed * 2][2];
int max_[maxed * 2][2];
int find_(int x);
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i) {
    scanf("%d%d", &node[i].x, &node[i].y);
    c[i * 2 - 1] = node[i].x;
    c[i * 2] = node[i].y;
  }
  std::sort(c + 1, c + 1 + 2 * n);
  int len = std::unique(c + 1, c + 1 + 2 * n) - c - 1;
  for (int i = 1; i <= len; ++i) {
    gen[i] = i, sum[i][0] = 1, sum[i][1] = 0;
    max_[i][0] = c[i], max_[i][1] = c[i];
  }
  for (int i = 1; i <= n; ++i) {
    int x = std::lower_bound(c + 1, c + 1 + len, node[i].x) - c;
    int y = std::lower_bound(c + 1, c + 1 + len, node[i].y) - c;
    x = find_(x), y = find_(y);
    if (x != y) {
      if (max_[x][0] < max_[y][0]) {
        max_[x][1] = max_[x][0];
        max_[x][0] = max_[y][0];
      } else if (max_[x][1] < max_[y][0])
        max_[x][1] = max_[y][0];
      sum[x][0] += sum[y][0];
      sum[x][1] += sum[y][1];
      gen[y] = x;
    }
    sum[x][1] += 1;
  }
  int answer = 0;
  for (int i = 1; i <= len; ++i)
    if (i == gen[i]) {
      if (sum[i][0] == sum[i][1] + 1)
        answer = std::max(answer, max_[i][1]);
      else if (sum[i][0] == sum[i][1])
        answer = std::max(answer, max_[i][0]);
      else {
        answer = -1;
        break;
      }
    }
  std::cout << answer << std::endl;
  return 0;
}
int find_(int x) {
  if (x == gen[x]) return x;
  return gen[x] = find_(gen[x]);
}
