#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], b[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 (i > j) {
      swap(i, j);
    }
    parent[j] = i;
    v[i] += v[j];
    e[i] += e[j];
    b[i] = max(b[i], make_pair(max(b[i].first, b[j].first),
                               min(b[i].first, b[j].first)));
  }
  ++e[i];
}
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);
  }
  for (int i = 0; i < c.size(); ++i) {
    parent[i] = i;
    v[i] = 1;
    e[i] = 0;
    b[i] = make_pair(c[i], 0);
  }
  for (int x = 0; x < n; ++x) {
    int i, j;
    tie(i, j) = a[x];
    Union(i, j);
  }
  int ans = 0;
  for (int i = 0; i < c.size() && ans >= 0; ++i) {
    if (parent[i] != i) {
      continue;
    }
    if (e[i] > v[i]) {
      ans = -1;
    }
    ans = max(ans, e[i] == v[i] ? b[i].first : b[i].second);
  }
  printf("%d\n", ans);
  return 0;
}
