#include <bits/stdc++.h>
using namespace std;
queue<int> que;
int n, m, cnt, tot, val, tp;
int tmp1, tmp2, ans, mn1, mn2;
int vis[3200005];
int head[3200005];
int st[2000005];
struct node {
  int fr;
  int to;
  int id;
} tim[2000005];
struct Edge {
  int fr;
  int to;
  int val;
  int nxt;
} edge[5000005];
void init() { memset(head, -1, sizeof(head)); }
void addedge(int f, int t) {
  cnt++;
  edge[cnt].fr = f;
  edge[cnt].to = t;
  edge[cnt].nxt = head[f];
  head[f] = cnt;
}
int cmp(node a, node b) { return a.fr < b.fr; }
int cmp1(int a, int b) { return a < b; }
void bfs() {
  tp = 0;
  mn1 = mn2 = 0x3f3f3f;
  while (!que.empty()) {
    int s = que.front();
    que.pop();
    tmp1++;
    for (int i = head[s]; i != -1; i = edge[i].nxt) {
      int v = edge[i].to;
      tmp2++;
      if (vis[v]) continue;
      vis[v] = true;
      if (v > n) {
        st[++tp] = v - n;
      }
      que.push(v);
    }
  }
}
int main() {
  init();
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d%d", &tim[++tot].fr, &tim[++tot].fr);
    tim[tot].id = tim[tot - 1].id = i;
  }
  sort(tim + 1, tim + tot + 1, cmp);
  for (int i = 1; i <= tot; i++) {
    if (tim[i].fr != tim[i - 1].fr) val++;
    tim[i].to = val;
    addedge(n + tim[i].to, tim[i].id);
    addedge(tim[i].id, n + tim[i].to);
  }
  for (int i = 1; i <= n; i++) {
    if (vis[i]) continue;
    tmp1 = tmp2 = 0;
    vis[i] = 1;
    que.push(i);
    bfs();
    tmp2 /= 2;
    if (tmp1 < tmp2) {
      printf("-1\n");
      return 0;
    }
    sort(st + 1, st + tp + 1, cmp1);
    if (tmp1 == tmp2) {
      ans = max(ans, st[tp]);
    } else {
      ans = max(ans, st[tp - 1]);
    }
  }
  for (int i = 1; i <= tot; i++) {
    if (tim[i].to == ans) {
      ans = tim[i].fr;
      break;
    }
  }
  printf("%d\n", ans);
  return 0;
}
