#include <bits/stdc++.h>

using namespace std;

typedef long long int ll;
typedef pair<ll, ll> pll;

#define FOR(i, n, m) for(ll (i)=(m);(i)<(n);++(i))
#define REP(i, n) FOR(i,n,0)
#define OF64 std::setprecision(10)

const ll MOD = 1000000007;
const ll INF = (ll) 1e15;
const ll MAX = 8;

string S[MAX];
ll x[8] = {1, 1, 1, 0, -1, -1, -1, 0};
ll y[8] = {1, 0, -1, -1, -1, 0, 1, 1};

bool dfs() {
    vector<vector<bool>> U(MAX, vector<bool>(MAX, true));
    ll cnt = 0;
    REP(h, MAX) {
        REP(w, MAX) {
            if (S[h][w] == '.')
                continue;
            cnt++;
            REP(i, 8) {
                ll mul = 0;
                while (true) {
                    ll nx = x[i] * mul + h, ny = y[i] * mul + w;
                    if (nx < 0 || ny < 0 || nx >= MAX || ny >= MAX)
                        break;
                    U[nx][ny] = false;
                    mul++;
                }
            }
        }
    }

    if (cnt == 8) {
        REP(h, MAX) {
            cout << S[h] << endl;
        }
        return true;
    }

    REP(h, MAX) {
        REP(w, MAX) {
            if (!U[h][w])
                continue;
            S[h][w] = 'Q';
            if (dfs())
                return true;
            S[h][w] = '.';
        }
    }
    return false;
}

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    REP(i, MAX) {
        string s = "";
        REP(j, MAX) {
            s.push_back('.');
        }
        S[i] = s;
    }
    ll N;
    cin >> N;
    REP(i, N) {
        pll p;
        cin >> p.first >> p.second;
        S[p.first][p.second] = 'Q';
    }

    dfs();

    return 0;
}

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<int, pii> pipii;
typedef pair<pii, pii> piipii;

#define mp make_pair
#define fi first
#define se second
#define all(a) (a).begin(), (a).end()
#define sz(a) (int)(a).size()
#define eb emplace_back
pii p[100005];
int x[100005], y[100005];
int r[100005];
int root(int a){
    return r[a] == a?a:(r[a]=root(r[a]));
}
void merge(int a, int b){
    a = root(a), b = root(b);
    if(a != b) r[a] = b;
}
int main(){
    int n;
    scanf("%d", &n);   
    for(int i=1;i<=n;i++){
        scanf("%d%d", &x[i], &y[i]);
        p[i] = pii(x[i], i);
        r[i] = i;
    }
    sort(p+1, p+1+n);
    vector<pipii> edges;
    for(int i=2;i<=n;i++) edges.eb(pipii(p[i].fi-p[i-1].fi, pii(p[i].se, p[i-1].se)));
    sort(all(edges));
    for(int i=1;i<=n;i++) p[i] = pii(y[i], i);
    sort(p+1, p+1+n);
    for(int i=2;i<=n;i++) edges.eb(pipii(p[i].fi-p[i-1].fi, pii(p[i].se, p[i-1].se)));
    sort(all(edges));
    int ans = 0;
    for(int i=0;i<sz(edges);i++){
        if(root(edges[i].se.fi) != root(edges[i].se.se)){
            merge(edges[i].se.fi, edges[i].se.se);
            ans += edges[i].fi;
        }
    }
    printf("%d\n", ans);
}