#include <bits/stdc++.h>
using namespace std;
int n, m;
int last;
map<int, int> mt;
vector<int> v[3000005];
bool used[3000005];
bool used2[3000005];
int sz, sz2;
int ans[3000005];
vector<int> q1;
int outmt[3000005];
int qq[3000005];
int qqq[3000005];
void dfs1(int x) {
  used[x] = true;
  if (x <= n)
    qq[++sz] = x;
  else
    qqq[++sz2] = x;
  for (int i = 0; i < v[x].size(); ++i) {
    int to = v[x][i];
    if (used[to] == false) dfs1(to);
  }
}
void dfs2(int x) {
  used[x] = true;
  if (x <= n && !ans[x]) {
    for (int j = 0; j < v[x].size(); ++j) {
      int to = v[x][j];
      if (!used2[to]) {
        used2[to] = true;
        ans[x] = to;
        break;
      }
    }
  }
  for (int i = 0; i < v[x].size(); ++i) {
    int to = v[x][i];
    if (!used[to]) dfs2(to);
  }
}
pair<int, int> a[1000005];
inline bool f(int mid) {
  int x;
  for (int i = 1; i <= n + m; ++i)
    v[i].clear(), ans[i] = 0, used[i] = false, used2[i] = false;
  for (int i = 1; i <= n; ++i) {
    x = a[i].first;
    if (x <= mid) {
      v[i].push_back(n + x);
      v[n + x].push_back(i);
    }
    x = a[i].second;
    if (x <= mid) {
      v[i].push_back(n + x);
      v[n + x].push_back(i);
    }
    if (v[i].size() == 0) return false;
  }
  bool tt, tt2;
  for (int i = 1; i <= n; ++i)
    if (!ans[i]) {
      sz = 0;
      sz2 = 0;
      dfs1(i);
      for (int j = 1; j <= sz; ++j) used[qq[j]] = false;
      for (int j = 1; j <= sz2; ++j) used[qqq[j]] = false;
      tt = false;
      for (int j = 0; j < v[i].size(); ++j) {
        int to = v[i][j];
        ans[i] = to;
        used2[to] = true;
        dfs2(i);
        tt2 = false;
        for (int k = 1; k <= sz; ++k)
          if (!ans[qq[k]]) {
            tt2 = true;
            break;
          }
        if (tt2 == false) {
          tt = true;
          break;
        }
        for (int k = 1; k <= sz; ++k) used[qq[k]] = false, ans[qq[k]] = 0;
        for (int k = 1; k <= sz2; ++k) {
          used[qqq[k]] = false;
          used2[qqq[k]] = false;
        }
      }
      if (tt == false) return false;
    }
  return true;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  for (int i = 1; i <= n; ++i) {
    cin >> a[i].first >> a[i].second;
    q1.push_back(a[i].first);
    q1.push_back(a[i].second);
  }
  sort(q1.begin(), q1.end());
  int last = 0;
  for (int i = 0; i < q1.size(); ++i) {
    if (i == 0 || q1[i] != q1[i - 1]) {
      ++last;
      mt[q1[i]] = last;
      outmt[last] = q1[i];
    }
  }
  for (int i = 1; i <= n; ++i) {
    a[i].first = mt[a[i].first];
    a[i].second = mt[a[i].second];
  }
  m = last;
  if (last < n) {
    cout << "-1";
    return 0;
  }
  int l = n;
  int r = last;
  while (l < r - 1) {
    int mid = (l + r) >> 1;
    if (f(mid))
      r = mid;
    else
      l = mid;
  }
  if (f(l)) r = l;
  if (f(r)) {
    cout << outmt[r];
  } else
    cout << "-1";
}
