#include <bits/stdc++.h>
using namespace std;
inline long long read() {
  char c = getchar();
  long long x = 0;
  bool f = 0;
  for (; !isdigit(c); c = getchar()) f ^= !(c ^ 45);
  for (; isdigit(c); c = getchar()) x = (x << 1) + (x << 3) + (c ^ 48);
  if (f) x = -x;
  return x;
}
const long long M = 2e5 + 01;
long long n, maxn, pp[M], a[M], b[M], cnt, ppp[M];
long long max1, max2, sum1, sum2, vis[M];
map<long long, long long> t;
vector<long long> e[M];
void dfs(long long u) {
  if (vis[u]) return;
  vis[u] = 1, sum2++;
  max2 = max(max2, u);
  if (max1 < max2) swap(max1, max2);
  for (long long i = 0; i < e[u].size(); i++) {
    long long to = e[u][i];
    sum1++;
    dfs(to);
  }
}
signed main() {
  n = read();
  for (long long i = 1; i <= n; i++)
    pp[++cnt] = a[i] = read(), pp[++cnt] = b[i] = read();
  sort(pp + 1, pp + 1 + cnt);
  cnt = 0;
  for (long long i = 1; i <= 2 * n; i++)
    if (pp[i] != pp[i - 1] || i == 1) t[pp[i]] = ++cnt, ppp[cnt] = pp[i];
  for (long long i = 1; i <= n; i++)
    a[i] = t[a[i]], b[i] = t[b[i]], e[a[i]].push_back(b[i]),
    e[b[i]].push_back(a[i]);
  for (long long i = 1; i <= n; i++)
    if (!vis[i]) {
      max1 = max2 = sum1 = sum2 = 0;
      dfs(i);
      sum1 /= 2;
      if (sum2 == sum1)
        maxn = max(maxn, max1);
      else if (sum2 < sum1)
        maxn = max(maxn, max2);
      else
        return puts("-1"), 0;
    }
  cout << ppp[maxn] << endl;
  return 0;
}
