#include "bits/stdc++.h"
using namespace std;
#define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))
#define rer(i,l,u) for(int (i)=(int)(l);(i)<=(int)(u);++(i))
#define reu(i,l,u) for(int (i)=(int)(l);(i)<(int)(u);++(i))
static const int INF = 0x3f3f3f3f; static const long long INFL = 0x3f3f3f3f3f3f3f3fLL;
typedef vector<int> vi; typedef pair<int, int> pii; typedef vector<pair<int, int> > vpii; typedef long long ll;
template<typename T, typename U> static void amin(T &x, U y) { if(y < x) x = y; }
template<typename T, typename U> static void amax(T &x, U y) { if(x < y) x = y; }

int main() {
	int N;
	while(~scanf("%d", &N)) {
		vector<int> A(N);
		vector<int> B(N);
		for(int i = 0; i < N; ++ i)
			scanf("%d%d", &A[i], &B[i]);

		bool ok = true;
		set<pair<int, int>> coordinates;
		map<pair<int, int>, vector<int>> unmatched;
		rep(i, N) {
			ok &= (A[i] == 0) == (i == 0);
			ok &= (B[i] == 0) == (i == 1);
			coordinates.insert(make_pair(A[i], B[i]));
			unmatched[{A[i], B[i]}].push_back(i);
		}
		vector<pair<int, int>> ord(N);
		rep(i, N)
			ord[i] = {A[i], i};
		sort(ord.begin(), ord.end());
		vector<bool> matched(N);
		int matching = 0;
		rep(ix, N) {
			int i = ord[ix].second;
			if(i != 0) {
				bool ook = false;
				rer(d, -1, 1)
					ook |= coordinates.count({ A[i] - 1, B[i] + d }) != 0;
				ok &= ook;
			}
			if(i != 1) {
				bool ook = false;
				rer(d, -1, 1)
					ook |= coordinates.count({ A[i] + d, B[i] - 1 }) != 0;
				ok &= ook;
			}
			if(!matched[i] && coordinates.count({ A[i] - 1, B[i] - 1 })) {
				++ matching;
			} else {
				auto &v = unmatched[{ A[i] + 1, B[i] - 1 }];
				if(!v.empty()) {
					matched[v.back()] = true;
					v.pop_back();
					++ matching;
				}
			}
		}
		if(!ok) {
			puts("-1");
			continue;
		}
		int ans = N * 2 - 2 - matching;
		printf("%d\n", ans);
	}
	return 0;
}
