#include <bits/stdc++.h>
using namespace std;
map<int, int> mapa;
const int maxn = 2e6 + 100;
vector<int> v[maxn];
int val[maxn];
bool vis[maxn];
bool vis2[maxn];
pair<int, int> dfs(int a) {
  pair<int, int> res = {1, int(v[a].size())};
  for (int b : v[a]) {
    if (vis[b] == false) {
      vis[b] = true;
      pair<int, int> r = dfs(b);
      res.first += r.first;
      res.second += r.second;
    }
  }
  return res;
}
pair<int, int> mm(pair<int, int> a, pair<int, int> b) {
  vector<int> q;
  q.push_back(a.first);
  q.push_back(a.second);
  q.push_back(b.first);
  q.push_back(b.second);
  sort(q.begin(), q.end());
  return {q[3], q[2]};
}
pair<int, int> findm(int a) {
  pair<int, int> res = {val[a], 0};
  for (int b : v[a]) {
    if (vis2[b] == false) {
      vis2[b] = true;
      pair<int, int> r = findm(b);
      res = mm(res, r);
    }
  }
  return res;
}
int solve(int n, int nr) {
  int res = 0;
  for (int i = 1; i < nr; i++) {
    if (vis[i] == false) {
      vis[i] = true;
      pair<int, int> r = dfs(i);
      r.second /= 2;
      if (r.second > r.first) return -1;
      vis2[i] = true;
      pair<int, int> m = findm(i);
      cout << m.first << " " << m.second << "\n";
      if (r.second < r.first) {
        res = max(res, m.second);
      } else {
        res = max(res, m.first);
      }
    }
  }
  return res;
}
int main() {
  ios_base::sync_with_stdio(0);
  int n;
  cin >> n;
  int nr = 1;
  for (int i = 0; i < n; i++) {
    int a, b;
    cin >> a >> b;
    int an, bn;
    if (mapa.count(a)) {
      an = mapa[a];
    } else {
      an = nr++;
      mapa[a] = an;
    }
    if (mapa.count(b)) {
      bn = mapa[b];
    } else {
      bn = nr++;
      mapa[b] = bn;
    }
    val[an] = a;
    val[bn] = b;
    v[an].push_back(bn);
    v[bn].push_back(an);
  }
  cout << solve(n, nr) << "\n";
  return 0;
}
