#include <bits/stdc++.h>
using namespace std;
bool vis[2000005];
int n, m, cn, cm, mx, ms, from[2000005], ans;
pair<int, int> dat[2000005];
map<int, int> to;
vector<int> g[2000005];
int inline read() {
  int res = 0;
  char c = getchar();
  for (; !isdigit(c); c = getchar())
    ;
  for (; isdigit(c); res *= 10, res += c ^ 48, c = getchar())
    ;
  return res;
}
void dfs(int u) {
  ++cn;
  cm += g[u].size();
  if (from[u] > mx) {
    ms = mx;
    mx = from[u];
  } else if (from[u] > ms) {
    ms = from[u];
  }
  vis[u] = 1;
  for (int v : g[u]) {
    if (vis[v]) continue;
    dfs(v);
  }
}
int main() {
  ios_base::sync_with_stdio(0);
  m = read();
  for (int i = 1; i <= m; ++i) {
    int u = read(), v = read();
    dat[i] = {u, v};
    to[u] = to[v] = 0;
    mx = max(mx, max(u, v));
  }
  for (__typeof(to.begin()) it = to.begin(); it != to.end(); ++it) {
    it->second = ++n;
    from[n] = it->first;
  }
  mx = 0;
  for (int i = 1; i <= m; ++i) {
    int u = to[dat[i].first], v = to[dat[i].second];
    g[u].push_back(v);
    g[v].push_back(u);
  }
  for (int u = 1; u <= n; ++u) {
    if (vis[u]) continue;
    cm = cn = mx = ms = 0;
    dfs(u);
    cm /= 2;
    if (cm == cn) {
      ans = max(ans, mx);
    } else if (cm > cn) {
      cout << -1 << '\n';
      return 0;
    } else {
      ans = max(ans, ms);
    }
  }
  cout << ans << '\n';
}
