#include <bits/stdc++.h>
const int N=400005;
int n,a[N],b[N],p[N],tg[N],use[N];
int solve(int L,int R){
	if (R<L) return 0;
	if (p[L]==R) return solve(L+1,R-1);
	int pl=L,pr=R;
	int mi=p[R],mx=p[L];
	int ans=tg[L]+tg[R],tot=2; 
	use[L]=use[p[L]]=use[R]=use[p[R]]=1;
	while (1){
		while (use[pl] && pl<=mi) pl++;
		while (use[pr] && pr>=mx) pr--;
		if (pl>mi && pr<mx) return std::min(tot-ans,ans)+solve(pl,pr);
		if (pl<=mi){
			use[pl]=1;
			use[p[pl]]=1;
			if (p[pl]>mx){
				puts("-1");
				exit(0);
			}
			mx=p[pl];
			ans+=tg[pl];
			tot++;
		}else{
			use[pr]=1;
			use[p[pr]]=1;
			if (p[pr]<mi){
				puts("-1");
				exit(0);
			}
			mi=p[pr];
			ans+=tg[pr];
			tot++;
		}
	}
}
int main(){
	scanf("%d",&n);
	for (int i=1;i<=n;i++){
		scanf("%d%d",&a[i],&b[i]);
		p[a[i]]=b[i],tg[a[i]]=0;
		p[b[i]]=a[i],tg[b[i]]=1;
	}
	for (int i=1;i<=n;i++)
		if (p[i]<=n) return puts("-1"),0;
	printf("%d\n",solve(1,2*n));
} 