#include <bits/stdc++.h>
using namespace std;
int a[1000010], m, b[1000010], c[2000010], rmp[2000010], vis[2000010],
    vis2[1000010];
map<int, int> mp;
vector<int> v1, v[2000010];
int find(int x) {
  if (c[x] < 0) return x;
  c[x] = find(c[x]);
  return c[x];
}
void uni(int x, int y) {
  if (c[x] < c[y])
    c[y] = x;
  else if (c[y] < c[x])
    c[x] = y;
  else {
    c[y] = x;
    c[x]--;
  }
  return;
}
int dfs(int s, int x) {
  if (vis2[s] == 1) return 1;
  if (x > m || vis[x] == 1) return 0;
  vis2[s] = 1, vis[x] = 1;
  int i, tp;
  for (i = 0; i < (int)(v[x].size()); i++) {
    tp = v[x][i];
    if (tp != s && dfs(tp, x ^ a[tp] ^ b[tp]) == 0) break;
  }
  if (i < (int)(v[x].size())) return 0;
  return 1;
}
int main() {
  int i, n, cnt = 1, l, r, tp, tp2;
  scanf("%d", &n);
  for (i = 1; i <= n; i++)
    scanf("%d%d", &a[i], &b[i]), v1.push_back(a[i]), v1.push_back(b[i]);
  sort(v1.begin(), v1.end());
  mp[v1[0]] = cnt;
  rmp[cnt++] = v1[0];
  for (i = 1; i < (int)(v1.size()); i++)
    if (v1[i] != v1[i - 1]) mp[v1[i]] = cnt, rmp[cnt++] = v1[i];
  for (i = 1; i <= n; i++) a[i] = mp[a[i]], b[i] = mp[b[i]];
  v1.clear();
  mp.clear();
  for (i = 1; i <= n; i++) v[a[i]].push_back(i), v[b[i]].push_back(i);
  l = 1, r = cnt - 1;
  while (l <= r) {
    m = (l + r) >> 1;
    for (i = 1; i <= m; i++) vis[i] = 0;
    for (i = 1; i <= n; i++) vis2[i] = 0;
    for (i = 1; i <= n; i++)
      if (a[i] > m && b[i] > m)
        break;
      else if (a[i] > m && dfs(i, b[i] == 0))
        break;
      else if (b[i] > m && dfs(i, a[i]) == 0)
        break;
    if (i <= n)
      l = m + 1;
    else {
      for (i = 1; i <= cnt; i++) c[i] = -1;
      for (i = 1; i <= n; i++)
        if (vis2[i] == 0) {
          if (vis[a[i]] == 1 && vis[b[i]] == 1) break;
          if (vis[a[i]] == 1 && dfs(i, b[i]) == 0)
            break;
          else if (vis[b[i]] == 1 && dfs(i, a[i]) == 0)
            break;
          else {
            tp = find(a[i]);
            tp2 = find(b[i]);
            if (tp == tp2 && dfs(i, a[i]) == 0)
              break;
            else
              uni(tp, tp2);
          }
        }
      if (i <= n)
        l = m + 1;
      else
        r = m - 1;
    }
  }
  if (l >= cnt)
    printf("-1\n");
  else
    printf("%d\n", rmp[l]);
  return 0;
}
