#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("O2")
const int N = 1e6 + 5;
int n, a[N], b[N], t[N], tv, te;
bool fix[N], use[N << 1], mark[N << 1];
vector<int> v[N << 1], adj[N << 1], comp[N << 1];
vector<int> num;
map<int, int> mp;
void dfs(int v) {
  mark[v] = 1;
  te += adj[v].size();
  tv++;
  for (auto u : adj[v])
    if (!mark[u]) dfs(u);
}
bool check(int x) {
  fill(t, t + n + 1, 0);
  fill(fix, fix + n + 1, false);
  fill(use, use + mp[num.back()] + 1, false);
  fill(mark, mark + mp[num.back()] + 1, false);
  for (int i = 0; i < mp[num.back()]; i++) {
    comp[i].clear();
    v[i].clear();
    adj[i].clear();
  }
  for (int i = 0; i < n; i++) {
    if (a[i] <= x) {
      t[i] |= 1;
      v[a[i]].push_back(i);
    }
    if (b[i] <= x) {
      t[i] |= 2;
      v[b[i]].push_back(i);
    }
  }
  queue<int> q;
  for (int i = 0; i < n; i++)
    if (t[i] == 0)
      return false;
    else if (t[i] == 1 || t[i] == 2)
      q.push(i);
  while (q.size()) {
    int to = q.front();
    q.pop();
    if (fix[to])
      continue;
    else if (t[to] == 0)
      return false;
    else if (t[to] == 1) {
      fix[to] = true;
      use[a[to]] = true;
      for (auto p : v[a[to]]) {
        if (a[p] == a[to]) {
          t[p] -= 1;
          if (t[p] == 2 && !fix[p])
            q.push(p);
          else if (!t[p] && !fix[p])
            return false;
        } else if (b[p] == a[to]) {
          t[p] -= 2;
          if (t[p] == 1 && !fix[p])
            q.push(p);
          else if (!t[p] && !fix[p])
            return false;
        }
      }
    } else if (t[to] == 2) {
      fix[to] = true;
      use[b[to]] = true;
      for (auto p : v[b[to]]) {
        if (a[p] == b[to]) {
          t[p] -= 1;
          if (t[p] == 2 && !fix[p])
            q.push(p);
          else if (!t[p] && !fix[p])
            return false;
        } else if (b[p] == b[to]) {
          t[p] -= 2;
          if (t[p] == 1 && !fix[p])
            q.push(p);
          else if (!t[p] && !fix[p])
            return false;
        }
      }
    } else
      return false;
  }
  for (int i = 0; i < n; i++) {
    if (fix[i])
      continue;
    else {
      adj[a[i]].push_back(b[i]);
      adj[b[i]].push_back(a[i]);
    }
  }
  for (int i = 0; i <= mp[num.back()]; i++)
    if (!mark[i]) {
      tv = 0;
      te = 0;
      dfs(i);
      te /= 2;
      if (te > tv) {
        return false;
      }
    }
  return true;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  for (int i = 0; i < n; i++) cin >> a[i] >> b[i];
  for (int i = 0; i < n; i++) {
    num.push_back(a[i]);
    num.push_back(b[i]);
  }
  sort(num.begin(), num.end());
  num.erase(unique(num.begin(), num.end()), num.end());
  for (int i = 0; i < num.size(); i++) mp[num[i]] = i;
  for (int i = 0; i < n; i++) {
    a[i] = mp[a[i]];
    b[i] = mp[b[i]];
  }
  int l = 0, r = 1e9 + 1;
  while (r - l > 1) {
    int mid = l + r >> 1;
    auto x = upper_bound(num.begin(), num.end(), mid) - num.begin() - 1;
    if (check(x))
      r = mid;
    else
      l = mid;
  }
  cout << (r == 1e9 + 1 ? -1 : r) << '\n';
  return 0;
}
