#include <bits/stdc++.h>
using namespace std;
template <typename T>
void pfill(T* pst, const T* ped, T val) {
  for (; pst != ped; *(pst++) = val)
    ;
}
typedef class Input {
 protected:
  const static int limit = 65536;
  FILE* file;
  int ss, st;
  char buf[limit];

 public:
  Input() : file(NULL){};
  Input(FILE* file) : file(file) {}
  void open(FILE* file) { this->file = file; }
  void open(const char* filename) { file = fopen(filename, "r"); }
  char pick() {
    if (ss == st) st = fread(buf, 1, limit, file), ss = 0;
    return buf[ss++];
  }
} Input;
Input& operator>>(Input& in, unsigned& u) {
  char x;
  while (~(x = in.pick()) && !((x) >= '0' && (x) <= '9'))
    ;
  for (u = x - '0'; ~(x = in.pick()) && ((x) >= '0' && (x) <= '9');
       u = u * 10 + x - '0')
    ;
  return in;
}
Input& operator>>(Input& in, unsigned long long& u) {
  char x;
  while (~(x = in.pick()) && !((x) >= '0' && (x) <= '9'))
    ;
  for (u = x - '0'; ~(x = in.pick()) && ((x) >= '0' && (x) <= '9');
       u = u * 10 + x - '0')
    ;
  return in;
}
Input& operator>>(Input& in, int& u) {
  char x;
  while (~(x = in.pick()) && !((x) >= '0' && (x) <= '9') && x != '-')
    ;
  int aflag = ((x == '-') ? (x = in.pick(), -1) : (1));
  for (u = x - '0'; ~(x = in.pick()) && ((x) >= '0' && (x) <= '9');
       u = u * 10 + x - '0')
    ;
  u *= aflag;
  return in;
}
Input& operator>>(Input& in, long long& u) {
  char x;
  while (~(x = in.pick()) && !((x) >= '0' && (x) <= '9') && x != '-')
    ;
  int aflag = ((x == '-') ? (x = in.pick(), -1) : (1));
  for (u = x - '0'; ~(x = in.pick()) && ((x) >= '0' && (x) <= '9');
       u = u * 10 + x - '0')
    ;
  u *= aflag;
  return in;
}
Input in(stdin);
int n, m;
int* uf;
int* es;
pair<int, int>* ps;
vector<int> br;
int find(int x) { return (uf[x] == x) ? (x) : (uf[x] = find(uf[x])); }
inline void init() {
  in >> n;
  ps = new pair<int, int>[(n + 1)];
  for (int i = 1; i <= n; i++) in >> ps[i].first >> ps[i].second;
}
inline void descrete() {
  for (int i = 1; i <= n; i++) {
    br.push_back(ps[i].first);
    br.push_back(ps[i].second);
  }
  sort(br.begin(), br.end());
  m = unique(br.begin(), br.end()) - br.begin();
  for (int i = 1; i <= n; i++) {
    ps[i].first =
        lower_bound(br.begin(), br.begin() + m, ps[i].first) - br.begin();
    ps[i].second =
        lower_bound(br.begin(), br.begin() + m, ps[i].second) - br.begin();
  }
}
void addEdge(int u, int v) {
  if (find(u) != find(v)) {
    es[find(u)] += es[find(v)];
    uf[find(v)] = find(u);
  }
  es[find(u)]++;
}
bool check(int mid) {
  for (int i = 0; i <= mid; i++) uf[i] = i, es[i] = -1;
  for (int i = 1; i <= n; i++) {
    if (ps[i].first > mid) return false;
    if (ps[i].second > mid)
      addEdge(ps[i].first, ps[i].first);
    else
      addEdge(ps[i].first, ps[i].second);
  }
  for (int i = 0; i <= mid; i++)
    if (es[find(i)] > 0) return false;
  return true;
}
inline void solve() {
  uf = new int[m];
  es = new int[m];
  int l = 0, r = m - 1;
  while (l <= r) {
    int mid = (l + r) >> 1;
    if (!check(mid))
      l = mid + 1;
    else
      r = mid - 1;
  }
  if (r == m - 1)
    puts("-1");
  else
    printf("%d\n", br[r + 1]);
}
int main() {
  init();
  descrete();
  solve();
  return 0;
}
