#include <bits/stdc++.h>
using namespace std;
vector<int> id;
int Find(int x) {
  return lower_bound(id.begin(), id.end(), x) - id.begin() + 1;
}
const int N = 1000050;
const int M = 3 * N;
vector<int> E[M];
int a[N], b[N];
void Clear(int n) {
  for (int i = 1; i <= n; i++) E[i].clear();
}
bool mark[M], bad = 0;
vector<int> push;
int tk[M];
void BFS(int u, int take) {
  queue<int> q;
  q.push(u);
  tk[u] = take;
  while (q.size()) {
    u = q.front();
    q.pop();
    mark[u] = 1;
    push.push_back(u);
    int sz = 0;
    for (int i = 0; i < E[u].size(); i++) {
      int v = E[u][i];
      if (!mark[v]) sz++, mark[v] = 1, q.push(v), tk[v] = tk[u] ^ 1;
    }
    if (tk[u] && sz != 1) bad = 1;
  }
}
bool Match(int n, int m) {
  bad = 0;
  if (n - m < m) return 0;
  for (int i = 1; i <= n; i++) mark[i] = 0;
  for (int i = 1; i <= m; i++)
    if (E[i].empty()) return 0;
  for (int i = 1; i <= n; i++)
    if (!mark[i] && E[i].size() == 1) BFS(i, 1);
  for (int i = 1; i <= n; i++) {
    if (bad) return 0;
    if (mark[i]) continue;
    if (E[i].size() == 2) {
      mark[i] = 1;
      push.clear();
      BFS(E[i][0], 0);
      if (bad) {
        for (int j = 0; j < push.size(); j++) mark[push[j]] = 0;
        BFS(E[i][1], 0);
      }
    }
  }
  return !bad;
}
int main() {
  int n, i;
  scanf("%i", &n);
  for (i = 1; i <= n; i++) {
    scanf("%i %i", &a[i], &b[i]);
    id.push_back(a[i]);
    id.push_back(b[i]);
  }
  sort(id.begin(), id.end());
  id.erase(unique(id.begin(), id.end()), id.end());
  for (i = 1; i <= n; i++) {
    a[i] = n + Find(a[i]);
    b[i] = n + Find(b[i]);
  }
  int top = id.size(), bot = 1, mid, ans = -1;
  while (top >= bot) {
    mid = top + bot >> 1;
    Clear(n + id.size());
    for (i = 1; i <= n; i++) {
      if (a[i] <= mid + n) E[i].push_back(a[i]), E[a[i]].push_back(i);
      if (b[i] <= mid + n) E[i].push_back(b[i]), E[b[i]].push_back(i);
    }
    if (Match(n + mid, n))
      ans = mid, top = mid - 1;
    else
      bot = mid + 1;
  }
  if (ans == -1)
    printf("-1\n");
  else
    printf("%i\n", id[ans - 1]);
  return 0;
}
