#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
using namespace std;
#define endl '\n'
#define ll long long
#define pi pair<int, int>
#define f first
#define s second

const int mxn = 200000;
int n;
int a[mxn], b[mxn], f[2];
vector<int> v[2];

int main(){
	ios::sync_with_stdio(0);
	cin.tie(0);
	
	cin >> n;
	
	for(int i = 0; i < n; i++){
		int x, y, z = 0;
		cin >> x >> y;
		x--, y--;
		if(x > y) swap(x, y), z = 1;
		if(x >= n || y < n){
			cout << -1 << endl;
			return 0;
		}
		a[x] = y - n, b[x] = z;
	}
	
	int ret = 0;
	for(int i = 0, x = n; i < n; i++){
		bool t = 0;
		for(int j = 0; j < 2; j++){
			if(v[j].empty() || a[v[j].back()] > a[i]){
				t = 1, f[j] += b[i], v[j].push_back(i);
				break;
			}
		}
		if(!t){
			cout << -1 << endl;
			return 0;
		}
		x = min(x, a[i]);
		if(x == n - i - 1){
			int y = n;
			for(int j = 0; j < 2; j++) y = min(y, f[j] + (int)v[!j].size() - f[!j]);
			ret += y;
			for(int j = 0; j < 2; j++) f[j] = 0, v[j].clear();
		}
	}
	
	cout << ret << endl;
	
	return 0;
}