#include <bits/stdc++.h>
using namespace std;
template <typename T>
inline void read(T &x) {
  char c;
  bool nega = 0;
  while ((!isdigit(c = getchar())) && (c != '-'))
    ;
  if (c == '-') {
    nega = 1;
    c = getchar();
  }
  x = c - 48;
  while (isdigit(c = getchar())) x = x * 10 + c - 48;
  if (nega) x = -x;
}
template <typename T>
inline void writep(T x) {
  if (x > 9) writep(x / 10);
  putchar(x % 10 + 48);
}
template <typename T>
inline void write(T x) {
  if (x < 0) {
    putchar('-');
    x = -x;
  }
  writep(x);
}
template <typename T>
inline void writeln(T x) {
  write(x);
  putchar('\n');
}
map<int, int> mp;
long long cnt, n;
long long too[2000001];
long long p[2000001];
long long deg[2000001];
long long total[2000001];
long long len[2000001];
vector<long long> adj[2000001];
inline int findroot(long long x) {
  if (p[x] < 0) return x;
  return p[x] = findroot(p[x]);
}
inline void Union(int u, int v) {
  if (u != v) p[u] = v;
}
inline bool satisfy(int x) {
  if (total[x] > len[x]) return false;
  return true;
}
void compose() {
  for (int i = 1; i <= n; i++) {
    long long u, v;
    read(u);
    read(v);
    if (!mp[u]) mp[u] = ++cnt, too[cnt] = u;
    if (!mp[v]) mp[v] = ++cnt, too[cnt] = v;
    u = mp[u], v = mp[v];
    int pu = findroot(u), pv = findroot(v);
    Union(pu, pv);
    deg[u]++;
  }
  for (int i = 1; i <= cnt; i++) {
    int root = findroot(i);
    total[root] += deg[i];
    len[root]++;
    adj[root].push_back(too[i]);
  }
}
int main() {
  read(n);
  memset(p, -1, sizeof(p));
  compose();
  long long res = 0;
  for (int i = 1; i <= cnt; i++) {
    if (findroot(i) != i) continue;
    if (!satisfy(i)) {
      writeln(-1);
      return 0;
    }
    if (total[i] != len[i]) {
      long long dif = 0;
      for (long long p : adj[i]) dif = max(dif, p);
      for (long long p : adj[i])
        if (p != dif) res = max(res, p);
    } else {
      for (long long p : adj[i]) res = max(res, p);
    }
  }
  writeln(res);
}
