#include <bits/stdc++.h>
using namespace std;
const int maxn = 1000007;
const int INF = 1e9 + 7;
int t[maxn * 2];
int a[maxn * 2];
int n;
int cnt;
vector<int> Tpart[maxn * 2];
int vist[maxn * 2];
int agd[maxn];
int dfn;
int sz;
int id[maxn * 2];
int vis[maxn * 2];
inline int ID(int x) { return lower_bound(a, a + cnt, x) - a; }
void dfs(int u, int& vn, int& en, vector<int>& vis,
         vector<vector<int> >& edge) {
  vis[u] = 1;
  vn++;
  for (auto v : edge[u]) {
    en++;
    if (vis[v]) continue;
    dfs(v, vn, en, vis, edge);
  }
}
bool judge(int x) {
  fill(vist, vist + cnt, 0);
  queue<int> q;
  fill(agd, agd + n, 0);
  for (int i = (0); i < (n); ++i) {
    if (t[2 * i + 1] > x) {
      if (vist[t[2 * i]]++) return false;
      q.push(t[2 * i]);
      agd[i] = 1;
    }
  }
  while (!q.empty()) {
    auto u = q.front();
    q.pop();
    for (auto v : Tpart[u]) {
      if (agd[v / 2]) continue;
      if (vist[t[v ^ 1]]++) return false;
      agd[v / 2] = 1;
      q.push(t[v ^ 1]);
    }
  }
  vector<vector<int> > edge;
  dfn = 0;
  fill(id, id + cnt, -1), fill(vis, vis + cnt, 0);
  for (int i = (0); i < (n); ++i) {
    if (agd[i]) continue;
    int u, v;
    u = t[2 * i], v = t[2 * i + 1];
    if (id[u] == -1) id[u] = dfn++, edge.push_back(vector<int>());
    if (id[v] == -1) id[v] = dfn++, edge.push_back(vector<int>());
    u = id[u], v = id[v];
    edge[u].push_back(v), edge[v].push_back(u);
  }
  for (int i = (0); i < (sz); ++i) {
    if (vis[i]) continue;
    int en, vn;
    en = vn = 0;
    q.push(i);
    vis[i] = 1;
    while (!q.empty()) {
      auto u = q.front();
      q.pop(), vn++;
      for (auto v : edge[u]) {
        en++;
        if (vis[v]) continue;
        vis[v] = 1, q.push(v);
      }
    }
    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]), Tpart[t[2 * i + j]].push_back(2 * i + j);
  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;
}
