#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
using u32 = size_t;
template <typename T>
inline T sqr(const T &a) {
  return a * a;
}
const int MOD = 998244353;
inline void madd(int &x, int delta) {
  x += (x + delta < MOD ? delta : delta - MOD);
}
struct Info {
  int a, b;
  int c = -1;
  vector<int> *la, *lb;
  bool operator<(const Info &x) const { return b > x.b; }
};
vector<Info> a;
vector<int> marked;
bool mark(int i, int c) {
  if (a[i].c != -1 && a[i].c != c) {
    return false;
  }
  if (a[i].c == c) {
    return true;
  }
  a[i].c = c;
  marked.push_back(i);
  const vector<int> &conf = *(a[i].a == c ? a[i].la : a[i].lb);
  for (int x : conf) {
    if (x != i) {
      int mx = a[x].a == c ? a[x].b : a[x].a;
      if (!mark(x, mx)) {
        return false;
      }
    }
  }
  return true;
}
int main() {
  cin.sync_with_stdio(false);
  int T = 1;
  for (int iTest = 1; iTest <= T; ++iTest) {
    int n;
    cin >> n;
    unordered_map<int, vector<int> > b;
    a.resize(n);
    marked.reserve(n);
    b.reserve(n * 4);
    for (int i = 0; i < n; ++i) {
      Info &x = a[i];
      cin >> x.a >> x.b;
      b[x.a];
      b[x.b];
    }
    sort(a.begin(), a.end());
    for (int i = 0; i < n; ++i) {
      Info &x = a[i];
      b[x.a].push_back(i);
      b[x.b].push_back(i);
    }
    for (int i = 0; i < n; ++i) {
      Info &x = a[i];
      x.la = &b[x.a];
      x.lb = &b[x.b];
    }
    bool g = true;
    for (int i = 0; i < n; ++i) {
      Info &x = a[i];
      if (x.c != -1) {
        continue;
      }
      marked.clear();
      if (!mark(i, x.a)) {
        for (int t : marked) {
          a[t].c = -1;
        }
        marked.clear();
        if (!mark(i, x.b)) {
          g = false;
          break;
        }
      }
    }
    int res = -1;
    if (g) {
      for (Info &x : a) {
        res = max(res, x.c);
      }
    }
    cout << res << '\n';
  }
  return 0;
}
