#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1000990;
int n, mid;
int a[MAXN][2];
map<int, vector<int> > day;
bool done[MAXN];
map<int, bool> doned;
bool dfs(int d) {
  if (d > mid) return false;
  int pp = day[d].size();
  if (doned[d]) return false;
  doned[d] = true;
  vector<int> cur;
  bool flag = true;
  for (int i = 0; i < pp; i++) {
    int x = day[d][i];
    if (!done[x]) {
      int t;
      if (a[x][0] == d)
        t = a[x][1];
      else
        t = a[x][0];
      done[x] = true;
      cur.push_back(x);
      if (!dfs(t)) {
        flag = false;
        break;
      }
    }
  }
  if (flag) return true;
  doned[d] = false;
  for (int i = 0; i < cur.size(); i++) done[cur[i]] = false;
  return false;
}
bool ojbk() {
  doned.clear();
  memset(done, false, sizeof(done));
  for (int i = 1; i <= n; i++)
    if (!done[i]) {
      done[i] = true;
      if (dfs(a[i][0])) continue;
      if (dfs(a[i][1])) continue;
      return false;
    }
  return true;
}
int main() {
  scanf("%d", &n);
  int l = 1, r = 0;
  for (int i = 1; i <= n; i++) {
    scanf("%d%d", &a[i][0], &a[i][1]);
    if (a[i][1] > r) r = a[i][1];
    day[a[i][0]].push_back(i);
    day[a[i][1]].push_back(i);
  }
  mid = r;
  if (!ojbk()) {
    printf("-1\n");
    return 0;
  }
  while (r > l) {
    mid = (l + r) >> 1;
    if (ojbk())
      r = mid;
    else
      l = mid + 1;
  }
  printf("%d\n", l);
  return 0;
}
