n = input()
D = [tuple(map(int, raw_input().split())) for i in xrange(n)]
if (not D[0][0] == D[1][1] == 0 or not D[0][1] == D[1][0] > 0
or any(i>1 and (d[0] == 0 or d[1] == 0) for i, d in enumerate(D))):
print -1
exit(0)
m = {}
for i, d in enumerate(D):
m[d] = m.get(d, 0) + 1 if i>0 else 0
P = D[:2]
D.sort()
cnt = 0
for x, y in D:
if (x+1, y+1) in m:
cnt += m[x+1, y+1]
m[x+1, y+1] = 0
if (x+1, y-1) in m:
cnt += m[x+1, y-1]
m[x+1, y-1] = 0
def dfs(x, y, dd, used):
used.add((x, y))
for dx, dy in dd:
nx = x + dx; ny = y + dy
if (nx, ny) in m and (nx, ny) not in used:
dfs(nx, ny, dd, used)
usedA = set(); usedB = set()
dfs(P[0][0], P[0][1], [(1, 1), (1, 0), (1, -1)], usedA)
dfs(P[1][0], P[1][1], [(1, 1), (0, 1), (-1, 1)], usedB)
if len(m) == len(usedA) == len(usedB):
print 2*n - 2 - cnt
else:
print -1