#include <bits/stdc++.h>
using namespace std;
vector<int> all;
const int maxn = 1e6 + 5;
int a[maxn], b[maxn];
int m;
int bad[2 * maxn];
int par[2 * maxn];
bool hascycle[2 * maxn];
bool proc[2 * maxn];
int n;
multiset<int> adj[2 * maxn];
int deg[2 * maxn];
int findset(int x) {
  if (par[x] == x) return x;
  return par[x] = findset(par[x]);
}
void prop(int x) {
  if (!hascycle[x]) return;
  hascycle[x] = true;
  for (auto v : adj[x]) {
    if (!hascycle[v]) prop(v);
  }
}
bool works(int x) {
  memset(bad, 0, sizeof bad);
  memset(hascycle, 0, sizeof hascycle);
  memset(proc, false, sizeof proc);
  memset(deg, 0, sizeof deg);
  for (int i = 1; i <= n; i++) {
    par[i] = i;
    adj[i].clear();
  }
  queue<int> cyc;
  for (int i = 1; i <= m; i++) {
    if (a[i] > x) return 0;
    if (b[i] > x) {
      if (bad[a[i]]) return false;
      bad[a[i]] = true;
    } else {
      adj[a[i]].insert(b[i]);
      adj[b[i]].insert(a[i]);
      deg[a[i]]++;
      deg[b[i]]++;
      if (findset(a[i]) == findset(b[i])) {
        cyc.push(a[i]);
      } else {
        int x = findset(a[i]), y = findset(b[i]);
        par[x] = y;
      }
    }
  }
  while (!cyc.empty()) {
    int u = cyc.front();
    cyc.pop();
    prop(u);
  }
  vector<int> sets;
  for (int i = 1; i <= n; i++) {
    if (bad[i] && hascycle[i]) return false;
    if (bad[i]) sets.push_back(findset(i));
  }
  int ori = sets.size();
  sort(sets.begin(), sets.end());
  sets.resize(unique(sets.begin(), sets.end()) - sets.begin());
  if (ori != (int)sets.size()) return false;
  queue<int> q;
  for (int i = 1; i <= n; i++) {
    if (deg[i] == 1) q.push(i);
  }
  while (!q.empty()) {
    int u = q.front();
    q.pop();
    proc[u] = true;
    for (auto v : adj[u]) adj[v].erase(adj[v].find(u));
    for (auto v : adj[u]) {
      deg[u]--;
      deg[v]--;
      if (deg[v] == 1) q.push(v);
    }
  }
  for (int i = 1; i <= n; i++) {
    if (deg[i] == 0 || deg[i] == 2) continue;
    return false;
  }
  return true;
}
int main() {
  scanf("%d", &m);
  for (int i = 1; i <= m; i++) {
    scanf("%d %d", &a[i], &b[i]);
    all.push_back(a[i]);
    all.push_back(b[i]);
  }
  all.push_back(0);
  sort(all.begin(), all.end());
  all.resize(unique(all.begin(), all.end()) - all.begin());
  for (int i = 1; i <= m; i++) {
    a[i] = lower_bound(all.begin(), all.end(), a[i]) - all.begin();
    b[i] = lower_bound(all.begin(), all.end(), b[i]) - all.begin();
  }
  n = all.size();
  int lo = 1, hi = n;
  while (lo < hi) {
    int mid = (lo + hi) / 2;
    if (works(mid))
      hi = mid;
    else
      lo = mid + 1;
  }
  if (works(lo))
    printf("%d\n", all[lo]);
  else
    printf("-1\n");
}
