#include <bits/stdc++.h>
using namespace std;
const int N = 2e6 + 5;
int f[N];
int num[N];
pair<int, int> a[N];
int tot;
int vis[N];
int vise[N];
int totn, totm;
int mx, ci;
int n;
vector<pair<int, int> > ve[N];
void init() {
  tot = 0;
  for (int i = 0; i < N; i++) f[i] = i;
}
int getf(int x) { return f[x] == x ? x : (f[x] = getf(f[x])); }
void merge(int x, int y) {
  int t1 = getf(x);
  int t2 = getf(y);
  if (t1 > t2) swap(t1, t2);
  if (t1 != t2) {
    f[t2] = t1;
  }
}
void dfs(int u) {
  if (vis[u]) return;
  vis[u] = 1;
  if (u > mx) {
    ci = mx;
    mx = u;
  } else if (u > ci) {
    ci = u;
  }
  totn++;
  for (int i = 0; i < ve[u].size(); i++) {
    int v = ve[u][i].first;
    int id = ve[u][i].second;
    if (vise[id] == 0) {
      vise[id] = 1;
      totm++;
    }
    if (!vis[v]) {
      dfs(v);
    }
  }
}
int main() {
  init();
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d %d", &a[i].first, &a[i].second);
    num[++tot] = a[i].first;
    num[++tot] = a[i].second;
  }
  sort(num + 1, num + tot + 1);
  tot = unique(num + 1, num + tot + 1) - (num + 1);
  for (int i = 1; i <= n; i++) {
    a[i].first = lower_bound(num + 1, num + tot + 1, a[i].first) - num;
    a[i].second = lower_bound(num + 1, num + tot + 1, a[i].second) - num;
  }
  for (int i = 1; i <= n; i++) {
    merge(a[i].first, a[i].second);
    ve[a[i].first].push_back(pair<int, int>(a[i].second, i));
    ve[a[i].second].push_back(pair<int, int>(a[i].first, i));
  }
  int Ans = 0;
  for (int i = 1; i <= tot; i++) {
    int fa = getf(i);
    if (i == fa) {
      totn = 0;
      totm = 0;
      mx = 0;
      ci = 0;
      dfs(i);
      if (totn < totm) {
        cout << -1 << endl;
        return 0;
      }
      if (totn == totm) {
        Ans = max(Ans, mx);
      } else if (totm + 1 == totn) {
        Ans = max(Ans, ci);
      }
    }
  }
  cout << num[Ans] << endl;
  return 0;
}
