#include <bits/stdc++.h>
using namespace std;
const int MXN = 3000010;
struct TEdge {
  int x, y;
} ed[MXN];
int M, N;
std::vector<int> E[MXN], V;
std::map<pair<int, int>, int> H;
int vis[MXN], fa[MXN], nil[MXN];
bool ok(int m) {
  for (int i = 1; i <= M; ++i) {
    if (ed[i].x > m && ed[i].y > m) return false;
  }
  stack<int> S;
  for (int i = 1; i <= N; ++i) vis[i] = 0;
  for (int i = 1; i <= N; ++i)
    if (nil[i]) {
      if (vis[i]) {
        return false;
      }
      S.push(i);
      fa[i] = -1;
      vis[i] = 1;
    }
  for (int i = 1; i <= M; ++i) {
    if (ed[i].y > m) {
      if (vis[ed[i].x]) {
        return false;
      }
      S.push(ed[i].x);
      fa[ed[i].x] = ed[i].y;
      vis[ed[i].x] = 1;
    }
  }
  while (!S.empty()) {
    int u = S.top();
    S.pop();
    for (int v : E[u])
      if (v != fa[u]) {
        if (vis[v]) {
          return false;
        }
        vis[v] = 1;
        S.push(v);
        fa[v] = u;
      }
  }
  return true;
}
int main() {
  scanf("%d", &M);
  int X = 0, Y = M * 2 + 1;
  for (int i = 1; i <= M; ++i) {
    int x, y;
    scanf("%d %d", &x, &y);
    if (x > y) swap(x, y);
    ed[i] = {x, y};
    V.push_back(x);
    V.push_back(y);
  }
  sort(V.begin(), V.end());
  V.erase(unique(V.begin(), V.end()), V.end());
  N = V.size();
  for (int i = 1; i <= M; ++i) {
    ed[i].x = lower_bound(V.begin(), V.end(), ed[i].x) - V.begin() + 1;
    ed[i].y = lower_bound(V.begin(), V.end(), ed[i].y) - V.begin() + 1;
  }
  for (int i = 1; i <= M; ++i) {
    int x, y;
    x = ed[i].x, y = ed[i].y;
    int k = ++H[pair<int, int>(x, y)];
    if (k > 2) {
      puts("-1");
      return 0;
    }
    if (k == 2) {
      nil[x] = 1;
      nil[y] = 1;
    }
  }
  for (int i = 1; i <= M; ++i) {
    int x, y;
    x = ed[i].x, y = ed[i].y;
    if (H[pair<int, int>(x, y)] == 2) continue;
    E[x].push_back(y);
    E[y].push_back(x);
  }
  while (Y - X > 1) {
    int mid = (X + Y) >> 1;
    if (ok(mid))
      Y = mid;
    else
      X = mid;
  }
  if (Y == M * 2 + 1) {
    cout << -1 << endl;
    return 0;
  }
  cout << V[Y - 1] << endl;
  return 0;
}
