#include <bits/stdc++.h>
using namespace std;
const long long mod = 1000000007;
long long powmod(long long a, long long b) {
  long long res = 1;
  a %= mod;
  assert(b >= 0);
  for (; b; b >>= 1) {
    if (b & 1) res = res * a % mod;
    a = a * a % mod;
  }
  return res;
}
long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }
const int maxx = 4000500;
const int INF = 0x3f3f3f3f;
int n, m, k;
int a[maxx][2];
int vis[maxx];
int ans = 0, cnt = 0, pos = 0;
int l = 0, r = 0;
const int MAXN = 4000005;
const int MAXM = 4500010;
struct Edge {
  int to, next;
} edge[MAXM];
int head[MAXN], tot;
int par[maxx];
int find(int x) {
  if (x == par[x]) return x;
  return par[x] = find(par[x]);
}
void addedge(int u, int v) {
  edge[tot].to = v;
  edge[tot].next = head[u];
  head[u] = tot++;
}
int fk[maxx], F[maxx], ma[maxx], maa[maxx];
int sz = 0;
void init() {
  tot = 0;
  memset(head, -1, sizeof(head));
  for (int i = 0; i < n + 2; i++) {
    par[i] = i;
  }
  memset(F, -1, sizeof(F));
}
vector<int> num;
int getid(int x) {
  return lower_bound(num.begin(), num.end(), x) - num.begin();
}
void lisan() {
  sort((num).begin(), (num).end());
  num.resize(unique((num).begin(), (num).end()) - num.begin());
  sz = ((int)(num).size());
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < 2; j++) {
      a[i][j] = getid(a[i][j]) + 1;
    }
  }
}
int main() {
  scanf("%d", &n);
  init();
  for (int i = 0; i < n; i++) {
    scanf("%d%d", &a[i][0], &a[i][1]);
    num.push_back(a[i][0]);
    num.push_back(a[i][1]);
  }
  if (n == 1) {
    printf("%d\n", min(a[0][0], a[0][1]));
    return 0;
  }
  lisan();
  vector<pair<int, int> > V;
  for (int i = 0; i < n; i++) {
    int u = a[i][0], v = a[i][1];
    if (u > v) swap(u, v);
    if (find(u) == find(v) && fk[find(u)]) {
      printf("-1\n");
      return 0;
    } else if (find(u) == find(v))
      fk[find(u)] = 1;
    else {
      int x = find(u);
      int y = find(v);
      par[y] = x;
      V.push_back(pair<int, int>(u, v));
    }
  }
  for (int i = 0; i < ((int)(V).size()); i++) {
    int u = V[i].first, v = V[i].second;
    if (v > ma[find(u)]) {
      maa[find(u)] = ma[find(u)];
      ma[find(u)] = v;
    } else if (v > maa[find(u)])
      maa[find(u)] = v;
  }
  memset(vis, 0, sizeof(vis));
  ans = 0;
  for (int i = 1; i < n + 1; i++) {
    if (!vis[find(i)]) {
      vis[find(i)] = 1;
      if (fk[find(i)])
        ans = max(ans, ma[find(i)]);
      else
        ans = max(ans, maa[find(i)]);
    }
  }
  printf("%d\n", num[ans - 1]);
  return 0;
}
