#include <bits/stdc++.h>
using namespace std;
map<int, int> mp;
int anc[2000000], ind[2000000], ans[2000000][2];
int cir[2000000];
int fid(int n, int circle) {
  circle += cir[n];
  if (anc[n] == n) {
    cir[n] = circle;
    return n;
  }
  return anc[n] = fid(anc[n], circle);
}
int main() {
  int n, a, b, i, idx = 0, lans = 0;
  scanf("%d", &n);
  for (i = 0; i < n; i++) {
    scanf("%d%d", &a, &b);
    if (!mp.count(a)) {
      anc[idx] = idx;
      ans[idx][0] = a;
      mp[a] = idx++;
    }
    if (!mp.count(b)) {
      anc[idx] = idx;
      ans[idx][0] = b;
      mp[b] = idx++;
    }
    ind[mp[a]]++;
    ind[mp[b]]++;
    a = fid(mp[a], 0), b = fid(mp[b], 0);
    if (a == b) cir[a]++;
    anc[a] = b;
  }
  for (i = 0; i < idx; i++) {
    fid(i, 0);
    if (cir[i] > 1) {
      printf("-1");
      return 0;
    }
    if (ans[i][0] > ans[anc[i]][0]) {
      ans[anc[i]][1] = ans[anc[i]][0];
      ans[anc[i]][0] = ans[i][0];
    } else if (anc[i] != i && ans[i][0] > ans[anc[i]][1])
      ans[anc[i]][1] = ans[i][0];
  }
  for (i = 0; i < idx; i++)
    if (anc[i] == i && lans < cir[i] ? ans[i][0] : ans[i][1])
      lans = cir[i] ? ans[i][0] : ans[i][1];
  printf("%d", lans);
  return 0;
}
