#include <bits/stdc++.h>
using namespace std;
inline bool SR(int &x) { return scanf("%d", &x) == 1; }
inline bool SR(long long &x) { return scanf("%lld", &x) == 1; }
inline bool SR(double &x) { return scanf("%lf", &x) == 1; }
inline bool SR(char *s) { return scanf("%s", s) == 1; }
inline bool RI() { return true; }
template <typename I, typename... T>
inline bool RI(I &x, T &...tail) {
  return SR(x) && RI(tail...);
}
inline void SP(const int x) { printf("%d", x); }
inline void SP(const long long x) { printf("%lld", x); }
inline void SP(const double x) { printf("%.16lf", x); }
inline void SP(const char *s) { printf("%s", s); }
inline void PL() { puts(""); }
template <typename I, typename... T>
inline void PL(const I x, const T... tail) {
  SP(x);
  if (sizeof...(tail)) putchar(' ');
  PL(tail...);
}
template <typename I>
void _DOING(const char *s, I &&x) {
  cerr << s << " = " << x << endl;
}
template <typename I, typename... T>
void _DOING(const char *s, I &&x, T &&...tail) {
  int c = 0;
  while (*s != ',' || c != 0) {
    if (*s == '(' || *s == '[' || *s == '{') c++;
    if (*s == ')' || *s == ']' || *s == '}') c--;
    cerr << *s++;
  }
  cerr << " = " << x << " , ";
  _DOING(s + 1, tail...);
}
const int INF = 0x3f3f3f3f;
const long long INF64 = 0x3f3f3f3f3f3f3f3fll;
inline int RAND() {
  static int x = 880301;
  return (x = x * 0xdefaced + 1) % 0x7fffffff;
}
const int MAX_N = 2000000 + 7;
int par[MAX_N], sz[MAX_N], sz2[MAX_N], mx[MAX_N], mx2[MAX_N];
int find(int x) {
  if (x == par[x])
    return x;
  else
    return par[x] = find(par[x]);
}
bool merge(int x, int y) {
  x = find(x);
  y = find(y);
  if (sz[x] < sz[y]) swap(x, y);
  sz2[x]++;
  if (x == y) return 0;
  sz[x] += sz[y];
  sz2[x] += sz2[y];
  par[y] = x;
  vector<int> v = {mx[x], mx2[x], mx[y], mx2[y]};
  sort((v).begin(), (v).end(), greater<int>());
  mx[x] = v[0], mx2[x] = v[1];
  v.clear();
  v.shrink_to_fit();
  return 1;
}
int main() {
  int n;
  RI(n);
  vector<pair<int, int> > vs;
  vector<int> rd;
  for (int i = 0; i < int(n); i++) {
    int a, b;
    RI(a, b);
    vs.push_back(make_pair(a, b));
    rd.push_back(a);
    rd.push_back(b);
  }
  sort((rd).begin(), (rd).end());
  rd.erase(unique((rd).begin(), (rd).end()), rd.end());
  for (int i = 0; i < int(((int)rd.size())); i++) {
    par[i] = i;
    sz[i] = 1;
    sz2[i] = 0;
    mx[i] = rd[i];
    mx2[i] = 0;
  }
  for (pair<int, int> p : vs) {
    int a = lower_bound((rd).begin(), (rd).end(), p.first) - rd.begin();
    int b = lower_bound((rd).begin(), (rd).end(), p.second) - rd.begin();
    merge(a, b);
  }
  int ans = 0;
  for (int i = 0; i < int(((int)rd.size())); i++) {
    if (par[i] != i) continue;
    if (sz2[i] > sz[i]) {
      PL(-1);
      return 0;
    } else if (sz2[i] == sz[i])
      ans = max(ans, mx[i]);
    else
      ans = max(ans, mx2[i]);
  }
  PL(ans);
  return 0;
}
