#include <bits/stdc++.h>
inline int read() {
  register int re = 0, c, f = 1;
  while ((c = getchar()) < 48) f = c ^ 45;
  while (re = (re << 1) + (re << 3) + (c ^ 48), (c = getchar()) > 32)
    ;
  return f ? re : -re;
}
const int maxn = 2e6 + 3, INF = 0x3f3f3f3f;
typedef int arr[maxn];
int n;
struct node {
  int a, b;
  bool operator<(const node &B) const { return b != B.b ? b < B.b : a < B.a; }
} p[maxn];
std::vector<int> ve;
bool vis[maxn];
int cnt;
arr h;
struct edge {
  int t, nxt;
} e[maxn << 1];
inline void Add(int a, int b) { e[++cnt] = (edge){b, h[a]}, h[a] = cnt; }
int mx1, mx2, num;
arr dep;
inline void Dfs(int u, int fa) {
  vis[u] = 1;
  if (ve[u - 1] > mx1)
    mx2 = mx1, mx1 = ve[u - 1];
  else
    mx2 = std::max(mx2, ve[u - 1]);
  for (int p = h[u], v = e[p].t; p; p = e[p].nxt, v = e[p].t) {
    if (v == fa) continue;
    if (!vis[v]) {
      dep[v] = dep[u] + 1;
      Dfs(v, u);
    } else if (dep[v] > dep[u])
      ++num;
  }
}
int main() {
  n = read();
  for (int i = (1); i <= (n); ++i) {
    int a = read(), b = read();
    p[i] = (node){a, b};
    ve.push_back(a);
    ve.push_back(b);
  }
  std::sort(ve.begin(), ve.end());
  ve.erase(std::unique(ve.begin(), ve.end()), ve.end());
  for (int i = (1); i <= (n); ++i) {
    p[i].a = std::lower_bound(ve.begin(), ve.end(), p[i].a) - ve.begin() + 1;
    p[i].b = std::lower_bound(ve.begin(), ve.end(), p[i].b) - ve.begin() + 1;
  }
  for (int i = (1); i <= (n); ++i) Add(p[i].a, p[i].b), Add(p[i].b, p[i].a);
  int ans = -INF;
  for (int i = (1); i <= (ve.size()); ++i) {
    if (!vis[i]) {
      mx1 = -INF, mx2 = -INF, num = 0;
      Dfs(i, 0);
      if (num > 1) {
        puts("-1");
        return 0;
      } else if (num == 1)
        ans = std::max(ans, mx1);
      else
        ans = std::max(ans, mx2);
    }
  }
  std::cout << ans << '\n';
  return 0;
}
