#include <bits/stdc++.h>
#pragma GCC optimize(2)
#pragma G++ optimize(2)
#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std;
const int inf = 0x3f3f3f3f;
long long rd() {
  long long x = 0, f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return x * f;
}
const int maxn = 2000010;
const long long mod = 1e9 + 7;
int n, m;
vector<int> ve[maxn];
int vis[maxn], a[maxn], b[maxn], siz, mx, smx, ans, tot;
vector<int> ha;
void dfs(int u) {
  if (vis[u]) return;
  vis[u]++;
  tot++;
  if (u > mx) {
    smx = mx, mx = u;
  } else if (u > smx)
    smx = u;
  for (auto &v : ve[u]) {
    siz++;
    dfs(v);
  }
}
int main() {
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    a[i] = rd(), b[i] = rd();
    ha.push_back(a[i]), ha.push_back(b[i]);
  }
  sort(ha.begin(), ha.end());
  ha.erase(unique(ha.begin(), ha.end()), ha.end());
  for (int i = 1; i <= n; i++) {
    a[i] = lower_bound(ha.begin(), ha.end(), a[i]) - ha.begin() + 1;
    b[i] = lower_bound(ha.begin(), ha.end(), b[i]) - ha.begin() + 1;
    ve[a[i]].push_back(b[i]);
    ve[b[i]].push_back(a[i]);
  }
  ans = 1;
  for (int i = 1; i <= n; i++) {
    if (!vis[i]) {
      mx = smx = 0;
      siz = 0, tot = 0;
      dfs(i);
      siz /= 2;
      if (siz > tot) {
        ans = -1;
        break;
      } else if (siz == tot) {
        ans = max(ans, mx);
      } else
        ans = max(ans, smx);
    }
  }
  if (ans == -1)
    puts("-1");
  else
    printf("%d\n", ha[ans - 1]);
}
