#include <bits/stdc++.h>
using namespace std;
const int maxn = 2000007;
const int INF = 1e9 + 7;
int t[maxn];
int a[maxn];
int n;
int cnt;
int used_time[maxn];
int arg_exam[maxn];
vector<pair<int, int> > edge[maxn];
inline int ID(int x) { return lower_bound(a, a + cnt, x) - a; }
bool forbid(int u, int i) {
  used_time[u] = arg_exam[i] = 1;
  for (auto e : edge[u]) {
    int v1 = e.first;
    int v2 = e.second;
    if (arg_exam[v2]) continue;
    if (used_time[v1]) return false;
    if (!forbid(v1, v2)) return false;
  }
  return true;
}
void dfs(int u, int fa, int& vn, int& en) {
  vn++;
  used_time[u] = 1;
  for (auto e : edge[u]) {
    int v1 = e.first;
    int v2 = e.second;
    if (arg_exam[v2]) continue;
    en++;
    if (!used_time[v1]) dfs(v1, u, vn, en);
  }
}
bool judge(int x) {
  memset(used_time, 0, sizeof(used_time));
  memset(arg_exam, 0, sizeof(arg_exam));
  for (int i = (0); i < (n); ++i) {
    if (t[2 * i + 1] > x) {
      if (used_time[t[2 * i]] || !forbid(t[2 * i], i)) return false;
    }
  }
  for (int i = (0); i < (cnt); ++i) {
    int vn, en;
    vn = en = 0;
    if (used_time[i]) continue;
    dfs(i, -1, vn, en);
    if (en % 2) {
      cerr << "WA";
      exit(-1);
    }
    if (en / 2 > vn) return false;
  }
  return true;
}
int main(void) {
  scanf("%d", &(n));
  for (int i = (0); i < (n); ++i)
    for (int j = (0); j < (2); ++j)
      scanf("%d", &(t[2 * i + j])), a[cnt++] = t[2 * i + j];
  sort(a, a + cnt);
  cnt = unique(a, a + cnt) - a;
  for (int i = (0); i < (n); ++i)
    for (int j = (0); j < (2); ++j) t[2 * i + j] = ID(t[i * 2 + j]);
  for (int i = (0); i < (n); ++i) {
    int u, v;
    u = t[2 * i], v = t[2 * i + 1];
    edge[u].push_back({v, i}), edge[v].push_back({u, i});
  }
  int L = 0;
  int R = cnt;
  for (int i = (0); i < (n); ++i) L = max(L, t[2 * i]);
  while (L < R) {
    int mid = R - (R - L + 1) / 2;
    if (judge(mid))
      R = mid;
    else
      L = mid + 1;
  }
  if (L < cnt)
    printf("%d\n", a[L]);
  else
    puts("-1");
  return 0;
}
