#include <bits/stdc++.h>
using namespace std;
map<int, int> M, N;
int cnt, n, used[2000006], hh[2000006];
vector<int> W[2000006];
int maxx, flag, cimax, ans, m, nn;
struct dd {
  int u, v;
} ddd[1000006];
void dfs(int u, int fa) {
  used[u]++;
  nn++;
  if (u < maxx)
    cimax = max(cimax, u);
  else if (u > maxx)
    cimax = maxx, maxx = u;
  int l = W[u].size() - 1;
  for (int i = 0; i <= l; i++) {
    if (W[u][i] == fa) continue;
    if (used[W[u][i]]) {
      used[W[u][i]]++;
      if (used[W[u][i]] == 2) flag = 1;
      if (used[W[u][i]] > 2) {
        printf("-1");
        exit(0);
      }
      continue;
    }
    dfs(W[u][i], u);
  }
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d%d", &ddd[i].u, &ddd[i].v);
    hh[++cnt] = ddd[i].u, hh[++cnt] = ddd[i].v;
  }
  sort(hh + 1, hh + 1 + cnt);
  int tr = 0;
  for (int i = 1; i <= cnt; i++) {
    if (!M[hh[i]]) M[hh[i]] = ++tr, N[tr] = hh[i];
  }
  for (int i = 1; i <= n; i++) {
    W[M[ddd[i].u]].push_back(M[ddd[i].v]);
    W[M[ddd[i].v]].push_back(M[ddd[i].u]);
  }
  for (int i = 1; i <= tr; i++) {
    m = 0, maxx = 0, cimax = 0, nn = 0;
    if (!used[i]) {
      dfs(i, i);
      if (flag == 1) {
        ans = max(ans, maxx);
      } else {
        ans = max(ans, cimax);
      }
    }
  }
  printf("%d", N[ans]);
  return 0;
}
