#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1000000007;
long long POW(long long a, long long b, long long MMM = MOD) {
  long long ret = 1;
  for (; b; b >>= 1, a = (a * a) % MMM)
    if (b & 1) ret = (ret * a) % MMM;
  return ret;
}
long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }
long long lcm(long long a, long long b) {
  if (a == 0 || b == 0) return a + b;
  return a * (b / gcd(a, b));
}
int dx[] = {0, 1, 0, -1, 1, 1, -1, -1}, dy[] = {1, 0, -1, 0, 1, -1, 1, -1};
int parent[2000000], sz[2000000], ed[2000000];
pair<int, int> p[1000000];
vector<int> vv;
int find(int a) {
  if (parent[a] < 0) return a;
  return parent[a] = find(parent[a]);
}
void merge(int a, int b) {
  a = find(a), b = find(b);
  if (a == b) {
    ed[a]++;
    return;
  }
  parent[a] = b;
  sz[b] += sz[a];
  ed[b] += ed[a] + 1;
}
int mx[2][2000000];
int n;
int main() {
  memset((mx), -1, sizeof(mx));
  ;
  memset((parent), -1, sizeof(parent));
  ;
  scanf("%d", &n);
  for (int(i) = (0); (i) <= (n - 1); (i) += (1)) {
    scanf("%d%d", &p[i].first, &p[i].second);
    vv.push_back(p[i].first);
    vv.push_back(p[i].second);
  }
  sort(vv.begin(), vv.end());
  vv.resize(unique(vv.begin(), vv.end()) - vv.begin());
  int N = vv.size();
  for (int(i) = (0); (i) <= (N - 1); (i) += (1)) sz[i] = 1;
  for (int(i) = (0); (i) <= (n - 1); (i) += (1)) {
    p[i].first = lower_bound(vv.begin(), vv.end(), p[i].first) - vv.begin();
    p[i].second = lower_bound(vv.begin(), vv.end(), p[i].second) - vv.begin();
    merge(p[i].first, p[i].second);
  }
  for (int(i) = (0); (i) <= (N - 1); (i) += (1)) {
    if (i == find(i)) {
      if (sz[i] < ed[i]) return !printf("-1");
    }
  }
  for (int(i) = (0); (i) <= (N - 1); (i) += (1)) {
    int x = find(i);
    if (vv[i] > mx[0][x]) {
      mx[1][x] = mx[0][x];
      mx[0][x] = vv[i];
    } else if (vv[i] > mx[1][x]) {
      mx[1][x] = vv[i];
    }
  }
  int ans = 0;
  for (int(i) = (0); (i) <= (N - 1); (i) += (1)) {
    if (find(i) == i) {
      if (sz[i] > ed[i])
        ans = max(ans, mx[1][i]);
      else if (sz[i] == ed[i])
        ans = max(ans, mx[0][i]);
    }
  }
  printf("%d", ans);
}
