#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e6 + 10;
int n;
struct node {
  int d, nd, id;
} a[maxn];
int fa[maxn], f[maxn], d[maxn][2];
int Find(int x) {
  if (x == fa[x]) return x;
  return fa[x] = Find(fa[x]);
}
bool cmp1(node x, node y) { return x.d < y.d; }
bool cmp2(node x, node y) { return x.id < y.id; }
int main() {
  int u, v, w, x, y;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d%d", &u, &v);
    a[2 * i - 1].d = u;
    a[2 * i - 1].id = 2 * i - 1;
    a[2 * i].d = v;
    a[2 * i].id = 2 * i;
  }
  sort(a + 1, a + n + n + 1, cmp1);
  int tot = 0;
  for (int i = 1; i <= n + n; i++) {
    if (a[i].d == a[i - 1].d)
      a[i].nd = tot;
    else
      a[i].nd = ++tot;
  }
  sort(a + 1, a + n + n + 1, cmp2);
  for (int i = 1; i <= tot; i++) {
    fa[i] = i;
  }
  for (int i = 1; i <= n; i++) {
    u = a[2 * i - 1].nd;
    v = a[2 * i].nd;
    d[u][0] = a[2 * i - 1].d;
    d[v][0] = a[2 * i].d;
  }
  for (int i = 1; i <= n; i++) {
    u = a[2 * i - 1].nd;
    v = a[2 * i].nd;
    x = Find(u);
    y = Find(v);
    cout << x << y << endl;
    if (x == y && !f[x]) {
      if (!f[x])
        f[x] = 1;
      else {
        printf("-1\n");
        return 0;
      }
    }
    if (x != y) {
      if (f[x] + f[y] >= 2) {
        printf("-1\n");
        return 0;
      } else {
        fa[x] = y;
        f[y] += f[x];
        if (f[y] == 1) {
          d[y][0] = max(d[y][0], d[x][0]);
        } else {
          if (d[y][0] < d[x][0]) {
            d[y][1] = max(d[y][0], d[x][1]);
            d[y][0] = d[x][0];
          } else if (d[y][1] < d[x][0]) {
            d[y][1] = d[x][0];
          }
        }
      }
    }
  }
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    u = a[2 * i - 1].nd;
    x = Find(u);
    cout << d[x][0] << " " << d[x][1] << endl;
    if (f[x])
      ans = max(ans, d[x][0]);
    else
      ans = max(ans, d[x][1]);
  }
  printf("%d\n", ans);
  return 0;
}
