7dd323afff6bf005df7daa89c1d0bbfcacb955a3c9e3a6b487fbeab414f9d012
// https://szkopul.edu.pl/problemset/problem/dPgIN7IwQ8JfKdyk3zTpUMiu/site/?key=statement
#include <bits/stdc++.h>
// #define GARY_DBG
#define GARY_LIB
constexpr int sizik = 1000 * 1001;
#define ar std::array
typedef std::vector<std::vector<int>> _kra;
constexpr int RED = 0, BLACK = 1;
struct Point {
int x, y, color, id;
};
int w(int x) {
return x >= 0 ? x : -x;
}
void solve() {
int n;
std::cin >> n;
std::vector<Point> points(2 * n);
int idx = 1, colorx = BLACK;
for (auto& [x, y, color, id] : points) {
std::cin >> x >> y;
if (idx > n) {
idx = 1;
colorx = RED;
}
id = idx++;
color = colorx;
}
std::vector<Point> czarne, czerwone;
czarne.reserve(n);
czerwone.reserve(n);
for (auto& p : points) {
if (p.color == RED) {
czerwone.push_back(p);
} else {
czarne.push_back(p);
}
}
std::sort(points.begin(), points.end(), [](const Point& a, const Point& b) {
if (a.x == b.x) {
if (a.color == b.color) {
return a.y < b.y;
} else {
return a.color < b.color;
}
} else {
return a.x > b.x;
}
});
std::vector<std::pair<int, int>> ans;
int64_t ans_liczb = 0;
std::set<std::pair<int, int>> s;
for (const auto& [x, y, color, id] : points) {
if (color == BLACK) {
auto ptr = s.upper_bound({y, INT32_MAX});
assert(ptr != s.begin());
ptr--;
ans.push_back({id, ptr->second});
ans_liczb += w(x - czerwone[ptr->second - 1].x) + w(y - czerwone[ptr->second - 1].y);
s.erase(ptr);
} else if (color == RED) {
s.insert({y, id});
}
}
std::cout << ans_liczb << '\n';
for (const auto& [a, b] : ans) {
std::cout << a << " " << b << '\n';
}
}
int32_t main() {
#ifndef GARY_DBG
std::ios_base::sync_with_stdio(0);
std::cin.tie(0);
std::cout.tie(0);
#endif
int t = 1;
// std::cin >> t;
for (; t > 0; t--) {
solve();
}
return 0;
}