#include <bits/stdc++.h>
using namespace std;
int n, a[1000005], b[1000005], num[1000005 * 2];
int info[1000005 * 2], Prev[1000005 * 2], to[1000005 * 2], cnt_e;
inline void Node(int u, int v) {
  Prev[++cnt_e] = info[u], info[u] = cnt_e, to[cnt_e] = v;
}
bool vis[1000005];
int siz, edge;
void dfs(int now) {
  if (vis[now]) return;
  vis[now] = 1;
  siz++;
  for (int i = info[now]; i; i = Prev[i]) {
    edge++;
    dfs(to[i]);
  }
}
inline bool check(int id) {
  for (int i = 1; i <= num[0]; i++)
    if (i <= id)
      vis[i] = 0;
    else
      vis[i] = 1;
  for (int i = num[0]; i >= 1; i--)
    if (vis[i])
      for (int j = info[i]; j; j = Prev[j]) {
        if (!vis[to[j]])
          vis[to[j]] = 1;
        else
          return 0;
      }
  return 1;
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    scanf("%d%d", &a[i], &b[i]), num[++num[0]] = a[i], num[++num[0]] = b[i];
  sort(num + 1, num + 1 + num[0]);
  num[0] = unique(num + 1, num + 1 + num[0]) - num - 1;
  for (int i = 1; i <= n; i++)
    a[i] = lower_bound(num + 1, num + 1 + num[0], a[i]) - num,
    b[i] = lower_bound(num + 1, num + 1 + num[0], b[i]) - num, Node(b[i], a[i]);
  for (int i = num[0]; i >= 1; i--)
    if (!vis[i]) {
      siz = edge = 0;
      dfs(i);
      if (edge > siz) {
        puts("-1");
        return 0;
      }
    }
  int L = 1, R = num[0], Mid;
  for (; L < R;) {
    Mid = (L + R) >> 1;
    if (check(Mid))
      R = Mid;
    else
      L = Mid + 1;
  }
  printf("%d\n", num[L]);
}
