#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
using namespace std;
const int N = 1e6 + 5;
inline int read() {
  int s = 0;
  char ch = getchar();
  while (!isdigit(ch)) ch = getchar();
  while (isdigit(ch)) s = s * 10 + ch - '0', ch = getchar();
  return s;
}
vector<int> G[N << 1];
int n, m;
int b[N << 1];
int l[N], r[N], c[N], idl[N], idr[N], vis[N << 1];
int num_v, num_e;
void dfs(int u) {
  vis[u] = 1, num_v++, num_e += G[u].size() + c[u];
  for (int i = 0; i < (int)G[u].size(); i++) {
    int v = G[u][i];
    if (!vis[v]) dfs(v);
  }
}
inline bool check(int x) {
  for (int i = 1; i <= m; i++) G[i].clear(), vis[i] = 0;
  for (int i = 1; i <= n; i++) {
    c[i] = 0;
    if (r[i] <= x)
      G[idl[i]].push_back(idr[i]), G[idr[i]].push_back(idl[i]);
    else
      c[i] = 2;
  }
  for (int i = 1; i <= m; i++) {
    if (vis[i]) continue;
    num_v = num_e = 0;
    dfs(i);
    if (num_e / 2 > num_v) return 0;
  }
  return 1;
}
int main() {
  n = read();
  int L = 0, R = 0;
  for (int i = 1; i <= n; i++) {
    l[i] = read(), r[i] = read();
    if (l[i] > r[i]) swap(l[i], r[i]);
    L = max(L, l[i]), R = max(R, r[i]);
    b[++m] = l[i];
    b[++m] = r[i];
  }
  sort(b + 1, b + m + 1);
  for (int i = 1; i <= n; i++) {
    idl[i] = lower_bound(b + 1, b + m + 1, l[i]) - b;
    idr[i] = lower_bound(b + 1, b + m + 1, r[i]) - b;
  }
  int ans = -1;
  while (L <= R) {
    int mid = (L + R) >> 1;
    if (check(mid))
      R = mid - 1, ans = mid;
    else
      L = mid + 1;
  }
  printf("%d", ans);
  return 0;
}
