#include <bits/stdc++.h>
using namespace std;
const int N = 1000001;
map<int, bool> vis;
map<int, vector<int> > V;
map<pair<int, int>, int> mp;
vector<int> ans;
vector<int> abc;
map<int, bool> vs;
vector<int> ee;
void dfs(int u, int &nodes, int &edges) {
  for (int i = 0; i < V[u].size(); i++) {
    if (!vis[V[u][i]]) {
      vis[V[u][i]] = 1;
      ee.push_back(V[u][i]);
      nodes++;
      edges += mp[make_pair(u, V[u][i])];
      dfs(V[u][i], nodes, edges);
    } else {
      edges += mp[make_pair(u, V[u][i])];
    }
  }
}
vector<pair<int, int> > uu;
int main() {
  int n, a, b;
  cin >> n;
  for (int i = 0; i < n; i++) {
    scanf("%d%d", &a, &b);
    uu.push_back(make_pair(a, b));
  }
  sort(uu.begin(), uu.end());
  for (int i = 2; i < n; i++) {
    if (uu[i].first == uu[i - 2].first && uu[i].second == uu[i - 2].second) {
      cout << -1;
      return 0;
    }
  }
  for (int i = 1; i < n; i++) {
    if (uu[i].first == uu[i - 1].first && uu[i].second == uu[i - 1].second) {
      uu[i].first = -1;
      uu[i].second = -1;
    }
  }
  for (int i = 0; i < n; i++) {
    a = uu[i].first;
    b = uu[i].second;
    if (a == -1) continue;
    if (!vs[a]) {
      vs[a] = 1;
      ans.push_back(a);
    }
    if (!vs[b]) {
      vs[b] = 1;
      ans.push_back(b);
    }
    abc.push_back(a);
    abc.push_back(b);
    if (mp[make_pair(a, b)] == 0) {
      V[a].push_back(b);
      V[b].push_back(a);
    }
    mp[make_pair(a, b)]++;
    mp[make_pair(b, a)]++;
  }
  bool k = 1;
  int mx = 0;
  int nodes = 0, edges = 0;
  for (int j = 0; j < abc.size(); j++) {
    int i = abc[j];
    if (!vis[i]) {
      vis[i] = 1;
      ee.clear();
      ee.push_back(i);
      nodes = 1;
      edges = 0;
      dfs(i, nodes, edges);
      edges /= 2;
      if (edges > nodes) {
        k = 0;
        break;
      }
      sort(ee.begin(), ee.end());
      mx = max(mx, ee[edges - 1]);
    }
  }
  if (!k) {
    cout << -1;
    return 0;
  }
  cout << mx;
  return 0;
}
