#include <bits/stdc++.h>

const int maxn = 2e5 + 22;
const int inf = 1e9;

int n,x,y,top[3],s1,s2,c1,c2,a[maxn],suf[maxn],c[maxn],ans;

int main() {
	scanf("%d",&n);
	for (int i = 1; i <= n; ++ i) {
		scanf("%d%d",&x,&y);
		int t = (x > y);
		if (t) std::swap(x,y);
		if (x <= n) { c[x] = t; a[x] = y; }
		else { puts("-1"); return 0; }
	} suf[n + 1] = 0;
	for (int i = n; i >= 1; i --) 
		suf[i] = std::max(a[i],suf[i + 1]);
	int pre = inf;
	top[1] = top[2] = inf;
	for (int i = 1; i <= n; ++ i) {
		pre = std::min(pre,a[i]);
		if (a[i] < top[1]) { 
			top[1] = a[i]; 
			s1 ++; c1 += c[i];
		} else if (a[i] < top[2]) {
			top[2] = a[i];
			s2 ++; c2 += c[i];
		} else { puts("-1"); return 0; } 
		if (pre > suf[i + 1]) {
			ans += std::min(c1 + s2 - c2, s1 - c1 + c2);
			s1 = c1 = s2 = c2 = 0;
			top[1] = top[2] = inf;
		}
	} printf("%d",ans);
	return 0;
}