#include <bits/stdc++.h>
using namespace std;
const int N = 2e6 + 5;
int n, ans, a[N], b[N];
int ed, nd;
int head[N], nxt[N << 1], to[N << 1], id[N << 1], tot;
bool mark[N], vis[N];
pair<int, int> mx;
void add_edge(int u, int v, int iid) {
  nxt[++tot] = head[u], head[u] = tot, to[tot] = v, id[tot] = iid;
}
void dfs(int u) {
  vis[u] = 1, nd++;
  if (u > mx.first)
    mx.second = mx.first, mx.first = u;
  else if (u > mx.second)
    mx.second = u;
  for (int i = head[u]; i; i = nxt[i]) {
    int v = to[i], iid = id[i];
    if (!mark[iid]) mark[iid] = 1, ed++;
    if (vis[v]) continue;
    dfs(v);
  }
}
int main() {
  scanf("%d", &n);
  for (int i = (1); i < (n + 1); i++) {
    int u, v;
    scanf("%d%d", &u, &v);
    a[2 * i] = u, a[2 * i - 1] = v;
    b[2 * i] = u, b[2 * i - 1] = v;
  }
  int m = 2 * n;
  sort(b + 1, b + 1 + m);
  m = unique(b + 1, b + 1 + m) - b - 1;
  for (int i = (1); i < (n + 1); i++) {
    int u, v;
    u = a[2 * i] = lower_bound(b + 1, b + 1 + m, a[2 * i]) - b;
    v = a[2 * i - 1] = lower_bound(b + 1, b + 1 + m, a[2 * i - 1]) - b;
    add_edge(u, v, i);
    add_edge(v, u, i);
  }
  for (int i = (1); i < (2 * n + 1); i++)
    if (!vis[a[i]]) {
      mx = make_pair(0, 0);
      ed = nd = 0;
      dfs(a[i]);
      if (ed > nd)
        return puts("-1"), 0;
      else if (ed == nd)
        ans = max(ans, b[mx.first]);
      else
        ans = max(ans, b[mx.second]);
    }
  printf("%d\n", ans);
  return 0;
}
