#include <bits/stdc++.h>
using namespace std;
int n, A[2000005], B[2000005];
vector<int> v;
vector<int> e[2000005];
bool vis[2000005];
void compress(int &gg) { gg = lower_bound(v.begin(), v.end(), gg) - v.begin(); }
void dfs(int node, int &mm1, int &mm2, int &dif) {
  vis[node] = true;
  if (v[node] >= mm1) {
    mm2 = mm1;
    mm1 = v[node];
  } else if (v[node] > mm2) {
    mm2 = v[node];
  }
  int hoppa = (int)e[node].size() - 2;
  dif += hoppa;
  for (int i = 0; i < (int)e[node].size(); i++) {
    int ggwp = e[node][i];
    if (vis[ggwp]) continue;
    dfs(ggwp, mm1, mm2, dif);
  }
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d %d", &A[i], &B[i]);
    v.push_back(A[i]);
    v.push_back(B[i]);
  }
  sort(v.begin(), v.end());
  v.resize(unique(v.begin(), v.end()) - v.begin());
  for (int i = 1; i <= n; i++) {
    compress(A[i]);
    compress(B[i]);
    e[A[i]].push_back(B[i]);
    e[B[i]].push_back(A[i]);
  }
  int sol = 0;
  for (int i = 0; i < (int)v.size(); i++) {
    int go = i;
    if (vis[i]) continue;
    int m1 = 0, m2 = 0, diff = 0;
    dfs(i, m1, m2, diff);
    if (diff > 0) {
      printf("-1");
      return 0;
    }
    if (diff == 0)
      sol = max(sol, m1);
    else
      sol = max(sol, m2);
  }
  printf("%d", sol);
  return 0;
}
