#include <cstdio>
#include <vector>
#include <algorithm>
#include <functional>
#include <map>
#include <set>
#include <string>
#include <iostream>
#include <cassert>
#include <cmath>
using namespace std;

void ng() {
        printf("-1\n");
        exit(0);
}

int main() {
        int n;
        scanf("%d", &n);
        set<pair<int, int>> ps;
        vector<pair<int, int>> p;
        for (int i = 0; i < n; i ++) {
                int a, b;
                scanf("%d%d", &a, &b);
                p.emplace_back(a, b);
                ps.insert({a, b});
        }
        if (p[0].first != 0 || p[1].second != 0) ng();
        if (p[0].second != p[1].first) ng();
        if (p[0].second == 0) ng();
        for (int i = 2; i < n; i ++) {
                int a, b;
                tie(a, b) = p[i];
                if (a == 0 || b == 0) ng();
                if (!ps.count({a - 1, b + 1}) && !ps.count({a - 1, b}) && !ps.count({a - 1, b - 1})) ng();
                if (!ps.count({a + 1, b - 1}) && !ps.count({a, b - 1}) && !ps.count({a - 1, b - 1})) ng();
        }
        map<int, map<int, int>> s;
        for (int i = 0; i < n; i ++) {
                s[p[i].first + p[i].second][p[i].first] ++;
        }
        int matching = 0;
        for (auto mpmp : s) {
                auto sum = mpmp.first;
                auto mp = mpmp.second;
                vector<pair<int, int>> v;
                for (auto it : mp) {
                        v.emplace_back(it.first, it.second);
                }
                sort(v.begin(), v.end());
                vector<vector<pair<bool, pair<int, int>>>> t; //(bottom left exists, number of the same position)
                vector<pair<bool, pair<int, int>>> tmp; //(bottom left exists, number of the same position)
                int preva = -1;
                for (auto u : v) {
                        int a = u.first;
                        if (preva != -1 && preva + 1 != a) {
                                t.push_back(tmp);
                                tmp.clear();
                        }
                        int b = sum - a;
                        bool ex = ps.count({a - 1, b - 1});
                        tmp.push_back({ex, {u.second, u.second}});
                        preva = a;
                }
                if (tmp.size() > 0) t.push_back(tmp);
                for (auto it : t) {
                        for (int i = 0; i < (int) it.size(); i ++) {
                                if (it[i].first) {
                                        int d = min(it[i].second.first, it[i].second.second);
                                        matching += d;
                                        it[i].second.first -= d;
                                        it[i].second.second -= d;
                                }
                                if (it[i].second.second > 0 && i + 1 < (int) it.size()) {
                                        int d = min(it[i].second.second, it[i + 1].second.first);
                                        matching += d;
                                        it[i + 1].second.first -= d;
                                }
                        }
                }
        }
        printf("%d\n", 2 * n - 2 - matching);
        return 0;
}


Measured memory_bytes: 24816