#include <bits/stdc++.h>
using namespace std;
char gc() {
  static char buf[100000], *p1 = buf, *p2 = buf;
  return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 100000, stdin), p1 == p2)
             ? EOF
             : *p1++;
}
long long read() {
  char ch = getchar();
  long long x = 0;
  int op = 1;
  for (; !isdigit(ch); ch = getchar())
    if (ch == '-') op = -1;
  for (; isdigit(ch); ch = getchar()) x = (x << 1) + (x << 3) + ch - '0';
  return x * op;
}
int n, sl, q[2000005], fa[2000005], t[2000005],
    cnt = 1, head[2000005], num, Mx, Mx2, vis[2000005], dep[2000005], ans;
struct edge {
  int to, nxt;
} e[2000005];
struct node {
  int x, y;
} a[2000005];
int getfa(int x) { return x == fa[x] ? x : fa[x] = getfa(fa[x]); }
void adde(int x, int y) {
  e[++cnt].to = y;
  e[cnt].nxt = head[x];
  head[x] = cnt;
}
void dfs(int u, int pr) {
  vis[u] = 1;
  if (q[u] > Mx)
    Mx2 = Mx, Mx = q[u];
  else
    Mx2 = max(Mx2, q[u]);
  for (int i = head[u]; i; i = e[i].nxt)
    if ((i ^ 1) != pr) {
      int v = e[i].to;
      if (!vis[v])
        dep[v] = dep[u] + 1, dfs(v, i);
      else if (dep[u] > dep[v])
        num++;
    }
}
int main() {
  n = read();
  int x, y;
  for (int i = (1); i <= (n); i++) {
    q[++sl] = a[i].x = read(), q[++sl] = a[i].y = read();
    if (a[i].x > a[i].y) swap(a[i].x, a[i].y);
  }
  sort(&q[1], &q[sl + 1]);
  sl = unique(&q[1], &q[sl + 1]) - q - 1;
  for (int i = (1); i <= (n); i++) {
    a[i].x = lower_bound(&q[1], &q[sl + 1], a[i].x) - q;
    a[i].y = lower_bound(&q[1], &q[sl + 1], a[i].y) - q;
    adde(a[i].x, a[i].y);
    adde(a[i].y, a[i].x);
  }
  for (int i = (1); i <= (sl); i++)
    if (!vis[i]) {
      num = Mx = Mx2 = 0;
      dfs(i, 0);
      if (num > 1) return puts("-1"), 0;
      if (num == 1)
        ans = max(ans, Mx);
      else
        ans = max(ans, Mx2);
    }
  printf("%d\n", ans);
  return 0;
}
