#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
const int N = 2e6 + 4;
int par[N], mx1[N], mx2[N], cycle_cnt[N], sz[N];
int find(int x) {
  if (par[x] == x) return x;
  return par[x] = find(par[x]);
}
void should_exit(int val) {
  if (val > 1) {
    printf("%d", -1);
    exit(0);
  }
}
void merge(int a, int b) {
  int pa = find(a), push_back = find(b);
  if (pa == push_back) {
    cycle_cnt[pa]++;
    should_exit(cycle_cnt[pa]);
    return;
  }
  par[pa] = push_back;
  cycle_cnt[push_back] += cycle_cnt[pa];
  sz[push_back] += sz[pa];
  if (mx1[pa] > mx1[push_back]) {
    mx2[push_back] = max(mx2[pa], mx1[push_back]);
    mx1[push_back] = mx1[pa];
  } else
    mx2[push_back] = max(mx2[push_back], mx1[pa]);
  should_exit(cycle_cnt[push_back]);
}
int main() {
  int n;
  scanf("%d", &n);
  vector<pair<int, int>> edges;
  map<int, int> hash;
  for (int i = 0; i < n; i++) {
    int a, b;
    scanf("%d", &a);
    scanf("%d", &b);
    edges.emplace_back(a, b);
    hash[a] = 1;
    hash[b] = 1;
  }
  int cntr = 1;
  for (auto &it : hash) {
    it.second = cntr++;
    par[it.second] = it.second;
    sz[it.second] = 1;
    mx1[it.second] = it.first;
  }
  for (int i = 0; i < n; i++) {
    int a = edges[i].first, b = edges[i].second;
    merge(hash[a], hash[b]);
  }
  int ans = 0;
  for (int i = 1; i < cntr; i++)
    if (sz[i] > 1) {
      if (cycle_cnt[i] == 1)
        ans = max(ans, mx1[i]);
      else
        ans = max(ans, mx2[i]);
    }
  printf("%d", ans);
  return 0;
}
