#include <bits/stdc++.h>
using namespace std;
const int N = 2e6 + 23;
char vis[N];
int ans, ic, ec, s, t, n, ov[N], nid;
vector<int> e[N], values;
map<int, int> id;
void dfs(int i) {
  ++ic;
  ec += e[i].size();
  vis[i] = 1;
  values.push_back(ov[i]);
  for (auto x : e[i])
    if (!vis[x]) dfs(x);
}
int getid(int i) {
  auto it = id.emplace(i, 0);
  return it.second ? (ov[++nid] = i, it.first->second = nid) : it.first->second;
}
int main() {
  scanf("%d", &n);
  for (int i = 0; i < n; ++i) {
    scanf("%d%d", &s, &t);
    s = getid(s);
    t = getid(t);
    e[s].push_back(t);
    e[t].push_back(s);
  }
  for (int i = 1; i <= nid; ++i) {
    ec = ic = 0;
    values.clear();
    if (!vis[i]) dfs(i);
    ec >>= 1;
    if (ec > ic) return puts("-1"), 0;
    if (ec < ic) *max_element(values.begin(), values.end()) = 0;
    ans = max(ans, std::remove_reference<decltype(ans)>::type(
                       *max_element(values.begin(), values.end())));
  }
  printf("%d\n", ans);
}
