#include <bits/stdc++.h>
using namespace std;
long long n, ans, a[2000010], cc[2000010];
struct node {
  long long a, b;
} x[2000010];
bool cmp(node a, node b) {
  if (a.a == b.a) return a.b < b.b;
  return a.a < b.a;
}
map<long long, long long> is;
long long si[2000010];
struct edge {
  long long v, next;
} e[2000010 << 1];
long long eid, p[2000010];
void insert(long long u, long long v) {
  e[++eid] = {v, p[u]};
  p[u] = eid;
  swap(u, v);
  e[++eid] = {v, p[u]};
  p[u] = eid;
}
bool vis[2000010];
long long m1, m2, flag;
void dfs(long long u, long long f) {
  cc[u]++;
  vis[u] = 1;
  if (m1 < si[u])
    swap(m1, m2), m1 = si[u];
  else if (m2 < si[u])
    m2 = si[u];
  for (long long i = p[u]; i; i = e[i].next) {
    long long v = e[i].v;
    if (vis[v] && v != f) flag = 1, cc[v]++;
    if (v != f && !vis[v]) dfs(v, u);
  }
}
int main() {
  ios::sync_with_stdio(false);
  cin >> n;
  for (long long i = 1; i <= n; ++i)
    cin >> x[i].a >> x[i].b, a[2 * i - 1] = x[i].a, a[2 * i] = x[i].b;
  sort(a + 1, a + 2 * n + 1);
  long long cnt = 0;
  for (long long i = 1; i <= 2 * n; ++i)
    if (a[i] != a[i - 1]) is[a[i]] = ++cnt, si[cnt] = a[i];
  sort(x + 1, x + n + 1, cmp);
  for (long long i = 2; i <= n; ++i)
    if (x[i].a == x[i - 1].a && x[i].b == x[i - 1].b) cc[is[x[i].a]]++;
  for (long long i = 1; i <= n; ++i) insert(is[x[i].a], is[x[i].b]);
  for (long long i = 1; i <= cnt; ++i) {
    if (vis[i]) continue;
    flag = m1 = m2 = 0;
    dfs(i, 0);
    if (flag)
      ans = max(ans, m1);
    else
      ans = max(ans, m2);
  }
  for (long long i = 1; i <= 2 * n; ++i)
    if (cc[i] >= 3) {
      cout << -1 << endl;
      return 0;
    }
  cout << ans << endl;
  return 0;
}
