#include <bits/stdc++.h>
using namespace std;
int N, TT[2000005], R[2000005];
pair<int, int> Edge[1000005];
int Coord[2000005], cnt, Aux[2000005], aux;
int Sz[2000005], Max[200005], Max2[200005], Cnt[200005];
void Read() {
  scanf("%d", &N);
  for (int i = 1; i <= N; i++) {
    int a, b;
    scanf("%d%d", &a, &b);
    Edge[i] = make_pair(a, b);
    Coord[++cnt] = a;
    Coord[++cnt] = b;
  }
}
int findCoordinate(int val) {
  int left = 1, right = cnt, mid, sol = 0;
  while (left <= right) {
    mid = (left + right) / 2;
    if (Coord[mid] == val) return mid;
    if (Coord[mid] < val)
      left = mid + 1;
    else
      right = mid - 1;
  }
  return 0;
}
void coordinateCompression() {
  sort(Coord + 1, Coord + cnt + 1);
  for (int i = 1; i <= cnt; i++) {
    if (Coord[i] != Coord[i - 1]) Aux[++aux] = Coord[i];
  }
  cnt = aux;
  for (int i = 1; i <= cnt; i++)
    Coord[i] = Aux[i], TT[i] = i, Sz[i] = 1, Max[i] = Coord[i];
}
void normalizeEdges() {
  for (int i = 1; i <= N; i++) {
    Edge[i].first = findCoordinate(Edge[i].first);
    Edge[i].second = findCoordinate(Edge[i].second);
  }
}
void uniteDS(int x, int y) {
  TT[x] = y;
  Sz[y] += Sz[x];
  Cnt[y] += Cnt[x] + 1;
  int max1[] = {Max[x], Max[y]}, max2[] = {Max2[x], Max2[y]};
  Max[y] = max(max1[0], max1[1]);
  int aux = max(max2[0], max2[1]);
  if (max1[0] > max1[1])
    Max2[y] = max(max1[1], aux);
  else
    Max2[y] = max(max1[0], aux);
}
void Unite(int x, int y) {
  if (x == y) return;
  if (R[x] < R[y]) {
    uniteDS(x, y);
  } else
    uniteDS(y, x);
  if (R[x] == R[y]) ++R[x];
}
int Father(int x) {
  int init = x;
  while (x != TT[x]) x = TT[x];
  while (init != x) {
    int next = TT[init];
    TT[init] = x;
    init = next;
  }
  return x;
}
void buildConnectedComponents() {
  for (int i = 1; i <= N; i++) {
    int a = Edge[i].first, b = Edge[i].second;
    if (Father(a) != Father(b))
      Unite(Father(a), Father(b));
    else
      Cnt[Father(a)]++;
  }
}
void Solve() {
  int ans = 0;
  for (int i = 1; i <= cnt; i++) {
    if (TT[i] == i) {
      if (Cnt[i] > Sz[i]) {
        printf("-1\n");
        return;
      }
      if (Cnt[i] == Sz[i])
        ans = max(ans, Max[i]);
      else
        ans = max(ans, Max2[i]);
    }
  }
  printf("%d\n", ans);
}
int main() {
  Read();
  coordinateCompression();
  normalizeEdges();
  buildConnectedComponents();
  Solve();
  return 0;
}
