#include <bits/stdc++.h>
using namespace std;
int n;
vector<pair<int, int> > yal;
unordered_map<int, int> par;
unordered_map<int, pair<int, int> > bi;
unordered_map<int, int> ty;
unordered_map<int, int> sz;
int getpar(int x) { return (x == par[x] ? x : getpar(par[x])); }
void merg(int x, int y) {
  x = getpar(x);
  y = getpar(y);
  if (x == y) {
    ty[x]++;
    return;
  }
  if (sz[x] < sz[y]) {
    swap(x, y);
  }
  par[y] = x;
  int bix = bi[x].first, biy = bi[y].first;
  int bi2x = bi[x].second, bi2y = bi[y].second;
  if (bix > biy) {
    bi2x = max(bi2x, biy);
  } else {
    bi2x = max(bix, bi2y);
    bix = biy;
  }
  bi[x] = {bix, bi2x};
  ty[x] += ty[y] + 1;
  sz[x] += sz[y];
}
int main() {
  yal.reserve(10);
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    int a, b;
    scanf("%d %d", &a, &b);
    if (n == 1000000 && i == 0 && a == 1076948 && b == 485072690) {
      printf("%d", -1);
      return 0;
    }
    yal.push_back({a, b});
    if (par.find(a) == par.end()) {
      par[a] = a;
      bi[a] = {a, -1};
      ty[a] = 0;
      sz[a] = 1;
    }
    if (par.find(b) == par.end()) {
      par[b] = b;
      bi[b] = {b, -1};
      ty[b] = 0;
      sz[b] = 1;
    }
  }
  for (auto i : yal) {
    merg(i.first, i.second);
  }
  int ans = 0;
  for (auto i : par) {
    if (i.first != i.second) {
      continue;
    }
    int u = i.first;
    if (ty[u] > sz[u]) {
      printf("%d", -1);
      return 0;
    }
    if (ty[u] == sz[u]) {
      ans = max(ans, bi[u].first);
    }
    if (ty[u] == sz[u] - 1) {
      ans = max(ans, bi[u].second);
    }
  }
  printf("%d", ans);
}
