#include <bits/stdc++.h>
using namespace std;
const int sz = 2e6 + 10;
int pr[sz], si[sz], maxi[sz], maxi2[sz];
bool ty[sz], us[sz];
int find_se(int v) {
  if (pr[v] != v) pr[v] = find_se(pr[v]);
  return pr[v];
}
void merge(int u, int v) {
  if (u != v) {
    if (si[u] < si[v]) swap(u, v);
    us[v] = 0;
    si[u] += si[v];
    pr[v] = u;
    ty[u] += ty[v];
    if (maxi[v] > maxi[u]) {
      maxi2[u] = maxi[u], maxi[u] = maxi[v];
    } else if (maxi[v] > maxi2[u])
      maxi2[u] = maxi[v];
    if (maxi2[v] > maxi[u]) {
      maxi2[u] = maxi[u], maxi[u] = maxi2[v];
    } else if (maxi2[v] > maxi2[u])
      maxi2[u] = maxi2[v];
  } else
    ty[u] = 1;
}
int main() {
  int n;
  cin >> n;
  map<int, int> ma;
  int sp[n][2];
  for (int a = 0; a < n; a++) {
    scanf("%d%d", &sp[a][0], &sp[a][1]);
    ma[sp[a][0]] = 0;
    ma[sp[a][1]] = 0;
  }
  int m = 0;
  for (auto it = ma.begin(); it != ma.end(); it++) {
    maxi[m] = (*it).first, (*it).second = m, m++;
  }
  for (int a = 0; a < m; a++) {
    pr[a] = a, si[a] = 1, ty[a] = 0, maxi2[a] = 0, us[a] = 1;
  }
  bool ok = 1;
  for (int a = 0; a < n; a++) {
    int u = ma[sp[a][0]], v = ma[sp[a][1]];
    u = find_se(u), v = find_se(v);
    if (ty[u] and ty[v])
      ok = 0;
    else
      merge(u, v);
  }
  if (ok) {
    int an = 0;
    for (int a = 0; a < m; a++) {
      if (us[a]) {
        if (ty[a] == 0)
          an = max(an, maxi2[a]);
        else
          an = max(an, maxi[a]);
      }
    }
    cout << an;
  } else
    cout << -1;
}
