#include <bits/stdc++.h>
using namespace std;
struct edge {
  int v, nt;
} G[1000010 << 1];
int n, m, cnt = 1, h[1000010], w[1000010], t, vis[1000010];
int s[1000010 << 1], x[1000010], y[1000010], A, B, C, p[1000010], f[1000010];
inline void adj(int x, int y) {
  G[++cnt] = (edge){y, h[x]};
  h[x] = cnt;
}
inline int fc(int x, int p) {
  if (vis[x]) {
    w[++t] = x;
    return 1;
  }
  vis[x] = 1;
  for (int v, i = h[x]; i; i = G[i].nt)
    if (i != p && fc(v = G[i].v, i ^ 1)) {
      w[++t] = x;
      return x != w[1];
    }
  return vis[x] = 0;
}
inline void dfs(int x) {
  p[x] = 1;
  if (s[x] > B) {
    C = B;
    B = s[x];
  } else if (s[x] > C) {
    C = s[x];
  }
  for (int v, i = h[x]; i; i = G[i].nt)
    if (!p[v = G[i].v]) dfs(v);
}
inline int D(int x) {
  fc(x, t = 0);
  C = B = 0;
  dfs(x);
  if (!t)
    A = max(A, C);
  else
    A = max(A, B);
}
inline int gf(int x) { return x == f[x] ? x : f[x] = gf(f[x]); }
int main() {
  scanf("%d", &n);
  bool imp = 0;
  for (int i = 1; i <= n << 1; ++i) f[i] = i;
  for (int i = 1; i <= n; ++i) {
    scanf("%d%d", x + i, y + i);
    s[++m] = x[i];
    s[++m] = y[i];
  }
  sort(s + 1, s + 1 + m);
  m = unique(s + 1, s + 1 + m) - s - 1;
  for (int x, y, i = 1; i <= n; ++i) {
    x = lower_bound(s + 1, s + 1 + m, ::x[i]) - s;
    y = lower_bound(s + 1, s + 1 + m, ::y[i]) - s;
    adj(y, x);
    adj(x, y);
    x = gf(x);
    y = gf(y);
    if (x != y) {
      if (p[x] && p[y])
        imp = 1;
      else {
        p[x] |= p[y];
        f[y] = x;
      }
    } else if (p[x])
      imp = 1;
    else
      p[x] = 1;
  }
  if (imp) return puts("-1");
  for (int i = 1; i <= (n << 1); ++i)
    if (!p[i]) D(i);
  printf("%d\n", A);
}
