#include <bits/stdc++.h>
const int N = 5e6 + 10;
const int M = 5e6 + 10;
int n, cnt, tot, VERT, EDGE, first_max, second_max, ans;
int first[N], vis[N], num[N], pos[N], rev[N];
struct IN {
  int a, b;
} t[N];
struct edge {
  int to, _next;
} e[M];
inline int read() {
  register int x = 0, f = 0, ch;
  while (!isdigit(ch = getchar())) f |= ch == '-';
  while (isdigit(ch)) x = (x << 1) + (x << 3) + (ch ^ 48), ch = getchar();
  return f ? -x : x;
}
void Add_Edge(int u, int v) {
  e[cnt].to = v;
  e[cnt]._next = first[u];
  first[u] = cnt++;
}
void dfs(int x) {
  vis[x] = 1;
  VERT++;
  if (x > n) {
    if (first_max < x - n) {
      second_max = first_max;
      first_max = x - n;
    } else
      second_max = std::max(second_max, x - n);
  }
  for (register int i = first[x]; ~i; i = e[i]._next) {
    int y = e[i].to;
    EDGE++;
    if (vis[y]) continue;
    dfs(y);
  }
}
int main() {
  n = read();
  memset(first, -1, sizeof(first));
  for (register int i = 1; i <= n; ++i) {
    t[i].a = read(), num[++tot] = t[i].a;
    t[i].b = read(), num[++tot] = t[i].b;
  }
  std::sort(num + 1, num + tot + 1);
  tot = std::unique(num + 1, num + tot + 1) - (num + 1);
  for (register int i = 1; i <= n; ++i) {
    pos[t[i].a] =
        std::lower_bound(num + 1, num + tot + 1, t[i].a) - (num + 1) + 1;
    pos[t[i].b] =
        std::lower_bound(num + 1, num + tot + 1, t[i].b) - (num + 1) + 1;
    rev[pos[t[i].a]] = t[i].a;
    rev[pos[t[i].b]] = t[i].b;
    Add_Edge(i, pos[t[i].a] + n), Add_Edge(pos[t[i].a] + n, i);
    Add_Edge(i, pos[t[i].b] + n), Add_Edge(pos[t[i].b] + n, i);
  }
  for (register int i = 1; i <= n; ++i)
    if (!vis[i]) {
      first_max = second_max = VERT = EDGE = 0;
      dfs(i);
      if (EDGE > VERT * 2) {
        puts("-1");
        return 0;
      } else if (EDGE == VERT * 2)
        ans = std::max(ans, first_max);
      else
        ans = std::max(ans, second_max);
    }
  printf("%d", rev[ans]);
  return 0;
}
