#include <cstdio>
#include <vector>
#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;
return mp[a + b].count(make_pair(a, b));
}
bool before(pair<int, int> p) {
return check(p, -1, -1) || check(p, -1, 0);
}
bool after(pair<int, int> p) {
return check(p, -1, -1) || check(p, 0, -1);
}
int calc(vector <pair<bool, int> >& v) {
int ans = 0, c = 0, i;
for (i = 0; i < v.size(); i++) {
ans += v[i].second;
if (v[i].first) {
c = min(c, v[i].second);
} else {
if (v[i].second > c) ans += v[i].second - c;
c = v[i].second;
}
}
return ans;
}
int solve() {
int n, d, ans = 0, 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)]++;
}
for (it = mp[d].begin(); it != mp[d].end(); it++) {
if (it == mp[d].begin()) continue;
it2 = it;
it2--;
if (it2->first.first + 1 != it->first.first) return -1;
ans += max(it->second, it2->second);
}
for (i = d + 1; i < n * 2; i++) {
vector <pair<bool, int> > v;
if (mp[i].size() == 0) continue;
for (it = mp[i].begin(); it != mp[i].end(); it++) {
if (it == mp[i].begin()) {
if (!before(it->first)) return -1;
v.push_back(make_pair(check(it->first, -1, -1), it->second));
} else {
it2 = it;
it2--;
if (it2->first.first + 1 != it->first.first) {
if (!after(it2->first) || !before(it->first)) return -1;
ans += calc(v);
v.clear();
}
v.push_back(make_pair(check(it->first, -1, -1), it->second));
}
}
it--;
if (!after(it->first)) return -1;
ans += calc(v);
}
return ans;
}
int main() {
printf("%d\n", solve());
return 0;
}