#include <bits/stdc++.h>
using namespace std;
int i, i0, n, m, pre[2000005], cnt[2000005], ans;
vector<int> v, v0, mp[2000005];
int fin(int x) {
  int tmp = x, tmp2;
  while (pre[tmp] != tmp) tmp = pre[tmp];
  while (x != tmp) tmp2 = pre[x], pre[x] = tmp, x = tmp2;
  return tmp;
}
void uni(int x, int y) {
  int fx = fin(x), fy = fin(y);
  if (fy < fx) swap(fx, fy);
  if (fx != fy) pre[fy] = fx, cnt[fx] += cnt[fy], cnt[fy] = 0;
}
struct node {
  int x, y;
} a[2000005];
int main() {
  scanf("%lld", &n);
  ans = 0;
  for (i = 1; i <= 2 * n + 1; i++) pre[i] = i;
  for (i = 1; i <= n; i++) {
    scanf("%d %d", &a[i].x, &a[i].y);
    v.push_back(a[i].x);
    v.push_back(a[i].y);
  }
  int rn = 0;
  sort(v.begin(), v.end()), v.erase(unique(v.begin(), v.end()), v.end());
  for (i = 1; i <= n; i++) {
    a[i].x = lower_bound(v.begin(), v.end(), a[i].x) - v.begin() + 1;
    a[i].y = lower_bound(v.begin(), v.end(), a[i].y) - v.begin() + 1;
    rn = max(a[i].x, rn);
    rn = max(a[i].y, rn);
    if (fin(a[i].x) == fin(a[i].y)) cnt[fin(a[i].x)]++;
    uni(fin(a[i].x), fin(a[i].y));
  }
  for (i = 1; i <= rn; i++) {
    if (fin(i) == i) v0.push_back(i);
    mp[fin(i)].push_back(i);
  }
  for (i = 0; i < v0.size(); i++) {
    if (cnt[v0[i]] == 1) {
      sort(mp[v0[i]].begin(), mp[v0[i]].end());
      if (mp[v0[i]].size() > 1) ans = max(ans, *(mp[v0[i]].end() - 1));
    } else if (cnt[v0[i]] == 0) {
      sort(mp[v0[i]].begin(), mp[v0[i]].end());
      if (mp[v0[i]].size() > 2)
        ans = max(ans, *(mp[v0[i]].end() - 2));
      else if (mp[v0[i]].size() == 2)
        ans = max(ans, (mp[v0[i]][0]));
    } else {
      ans = -1;
      break;
    }
  }
  if (ans == -1)
    printf("-1\n");
  else
    printf("%d\n", v[ans - 1]);
  return 0;
}
