#include <cstdio>
#include <map>
using namespace std;
map <pair<int, int>, int> mp[200000];
bool check(pair<int, int> p, int da, int db) {
int a = p.first + da;
int b = p.second + db;
if (a + b < 0) return false;
return mp[a + b].count(make_pair(a, b));
}
bool before(pair<int, int> p) {
return check(p, -1, -1) || check(p, -1, 0) || check(p, -1, 1);
}
bool after(pair<int, int> p) {
return check(p, -1, -1) || check(p, 0, -1) || check(p, 1, -1);
}
int solve() {
int n, d, ans, i;
map <pair<int, int>, int>::iterator it, it2;
scanf("%d", &n);
for (i = 0; i < n; i++) {
int a, b;
scanf("%d %d", &a, &b);
if (i == 0) {
if (a != 0 || b == 0) return -1;
d = b;
} else if (i == 1) {
if (a != d || b != 0) return -1;
} else {
if (a == 0 || b == 0 || a + b < d) return -1;
}
mp[a + b][make_pair(a, b)]++;
}
ans = n * 2 - 2;
for (i = d; i < n * 2; i++) {
int c;
if (mp[i].size() == 0) continue;
for (it = mp[i].begin(); it != mp[i].end(); it++) {
if (!before(it->first) && it->first.first > 0) return -1;
if (!after(it->first) && it->first.second > 0) return -1;
if (it != mp[i].begin()) {
it2 = it;
it2--;
if (it2->first.first + 1 == it->first.first) {
if (i == d) return -1;
ans -= min(it->second, c);
it->second = max(it->second - c, 0);
}
}
if (check(it->first, -1, -1)) {
ans -= it->second;
c = 0;
} else {
c = it->second;
}
}
}
return ans;
}
int main() {
printf("%d\n", solve());
return 0;
}