#include <bits/stdc++.h>
using namespace std;
void my_dbg() { cout << endl; }
template <typename Arg, typename... Args>
void my_dbg(Arg A, Args... B) {
  cout << ' ' << A;
  my_dbg(B...);
}
using ll = long long;
using vi = vector<int>;
using vl = vector<ll>;
const int inf = INT_MAX;
const int ninf = INT_MIN;
const int mod = 1e9 + 7;
const int N = 2e6 + 2;
vi parent(N), mx1(N), mx2(N), cycle(N), found(N);
int find(int i) { return parent[i] == i ? i : parent[i] = find(parent[i]); }
void merge(int i, int j) {
  int x = find(i), y = find(j);
  if (x == y) {
    cycle[y]++;
    return;
  }
  parent[x] = y;
  if (mx1[x] > mx1[y]) {
    mx2[y] = max(mx1[y], mx2[x]);
    mx1[y] = mx1[x];
  } else {
    mx2[y] = max(mx2[y], mx1[x]);
  }
  cycle[y] += cycle[x];
}
void solve() {
  int n;
  scanf("%d", &n);
  vector<pair<int, int>> edges;
  map<int, int> hash;
  for (int i = (int)(0); i < (int)(n); i++) {
    int a, b;
    scanf("%d", &a);
    scanf("%d", &b);
    edges.emplace_back(a, b);
    hash[a] = hash[b] = 1;
  }
  int cnt = 1;
  for (auto& i : hash) {
    i.second += cnt++;
    parent[i.second] = i.second;
    cycle[i.second] = 0;
    mx1[i.second] = i.first;
    mx2[i.second] = -1e9;
    found[i.second] = 1;
  }
  for (int i = (int)(0); i < (int)(n); i++) {
    int a = edges[i].first, b = edges[i].second;
    merge(hash[a], hash[b]);
  }
  int mx = -1e9;
  for (int i = (int)(1); i < (int)(cnt + 1); i++) {
    if (found[i]) {
      if (cycle[i] > 1) {
        printf("%d\n", (int)(-1));
        exit(0);
      } else {
        if (cycle[i] == 0) {
          mx = max(mx, mx2[i]);
        } else {
          mx = max(mx, mx1[i]);
        }
      }
    }
  }
  printf("%d\n", (int)(mx));
}
int main() {
  int t = 1;
  while (t--) {
    solve();
  }
  return 0;
}
