#include <bits/stdc++.h>
#define xx first
#define yy second
#define mp make_pair
#define pb push_back
#define fill( x, y ) memset( x, y, sizeof x )
#define copy( x, y ) memcpy( x, y, sizeof x )
using namespace std;
typedef long long LL;
typedef pair < int, int > pa;
inline int read()
{
int sc = 0, f = 1; char ch = getchar();
while( ch < '0' || ch > '9' ) { if( ch == '-' ) f = -1; ch = getchar(); }
while( ch >= '0' && ch <= '9' ) sc = sc * 10 + ch - '0', ch = getchar();
return sc * f;
}
const int MAXN = 100005;
int n, a[MAXN], b[MAXN];
map < pa, bool > cnt;
pa c[MAXN];
inline void GG() { puts( "-1" ); exit( 0 ); }
int main()
{
#ifdef wxh010910
freopen( "data.in", "r", stdin );
#endif
n = read();
for( int i = 1 ; i <= n ; i++ )
{
a[ i ] = read(), b[ i ] = read(), c[ i ] = mp( a[ i ] + b[ i ], b[ i ] );
if( !a[ i ] && i != 1 ) GG();
if( !b[ i ] && i != 2 ) GG();
cnt[ mp( a[ i ], b[ i ] ) ] = 1;
}
if( a[ 1 ] || b[ 2 ] ) GG();
sort( c + 1, c + n + 1 );
int ret = n - 1 << 1;
for( int l = 1, r = 1 ; l <= n ; l = r )
{
while( r <= n && c[ r ].xx == c[ l ].xx )
{
int x = c[ r ].xx - c[ r ].yy, y = c[ r ].yy;
if( x )
{
bool flag = false;
for( int d = -1 ; d <= 1 ; d++ ) if( cnt.find( mp( x - 1, y + d ) ) != cnt.end() ) flag = true;
if( !flag ) GG();
}
if( y )
{
bool flag = true;
for( int d = -1 ; d <= 1 ; d++ ) if( cnt.find( mp( x + d, y - 1 ) ) != cnt.end() ) flag = true;
if( !flag ) GG();
}
r++;
}
for( int i = l, j = l, last = 0, left = 0 ; i <= r ; i = j )
{
while( j <= r && c[ j ].yy == c[ i ].yy ) j++;
int now = j - i, del = 0;
if( c[ j ].yy == last + 1 ) del = min( now, left ), ret -= del;
if( cnt.find( mp( c[ j ].xx - c[ j ].yy - 1, c[ j ].yy - 1 ) ) != cnt.end() ) ret -= now - del, now = del;
last = c[ j ].yy; last = now;
}
}
return printf( "%d\n", ret ), 0;
}