#include <bits/stdc++.h>
using namespace std;
vector<vector<int> > adj;
vector<int> c;
int parent[2000000], v[2000000], e[2000000];
pair<int, int> a[2000000];
bool visited[2000000];
int Find(int i) { return (i == parent[i]) ? i : parent[i] = Find(parent[i]); }
void Union(int i, int j) {
  i = Find(i), j = Find(j);
  if (i == j) {
    if (e[i] < v[i]) {
      ++e[i];
    }
  } else {
    if (i > j) {
      swap(i, j);
    }
    parent[j] = i;
    v[i] += v[j];
    e[i] += e[j];
    e[i] += (e[i] < v[i]);
  }
}
bool Check(int n, int t) {
  for (int x = 0; x < n; ++x) {
    if (a[x].first > t && a[x].second > t) {
      return 0;
    }
  }
  for (int i = 0; i <= t; ++i) {
    v[i] = 1;
    e[i] = 0;
    visited[i] = 0;
    parent[i] = i;
  }
  queue<int> Q;
  for (int i = t + 1; i < c.size(); ++i) {
    Q.push(i);
  }
  while (!Q.empty()) {
    int i = Q.front();
    Q.pop();
    for (auto j : adj[i]) {
      if (j > t) {
        continue;
      }
      if (visited[j] == 0) {
        e[j] = 1;
        visited[j] = 1;
        Q.push(j);
      }
    }
  }
  for (int x = 0; x < n; ++x) {
    int i, j;
    tie(i, j) = a[x];
    if (i > t || j > t || visited[i] == 1 || visited[j] == 1) {
      continue;
    }
    Union(i, j);
  }
  for (int i = 0; i <= t; ++i) {
    if (parent[i] == i) {
      n -= e[i];
    }
  }
  return (n == 0);
}
int BinarySearch(int n) {
  int x = 0, y = c.size() - 1, z = -1;
  while (x <= y) {
    int mid = x + (y - x) / 2;
    if (Check(n, mid)) {
      z = c[mid], y = mid - 1;
    } else {
      x = mid + 1;
    }
  }
  return z;
}
int main() {
  int n;
  scanf("%d", &n);
  for (int x = 0; x < n; ++x) {
    int i, j;
    scanf("%d%d", &i, &j);
    c.push_back(i);
    c.push_back(j);
    a[x] = make_pair(i, j);
  }
  sort(c.begin(), c.end());
  c.erase(unique(c.begin(), c.end()), c.end());
  adj.resize(c.size());
  for (int x = 0; x < n; ++x) {
    int i, j;
    tie(i, j) = a[x];
    i = lower_bound(c.begin(), c.end(), i) - c.begin();
    j = lower_bound(c.begin(), c.end(), j) - c.begin();
    adj[i].push_back(j);
    adj[j].push_back(i);
    a[x] = make_pair(i, j);
  }
  printf("%d\n", BinarySearch(n));
  return 0;
}
