#include <bits/stdc++.h>
#define PB push_back
#define MP make_pair
using namespace std;

#define rep(i, n) for (int i = 0; i < (int)(n); ++ i)
#define rep1(i, n) for (int i = 1; i < (int)(n); ++ i)
#define foreach(itr, c) for (__typeof((c).begin()) itr = (c).begin(); itr != (c).end(); ++ itr)

typedef long long LL;
typedef pair<int, int> pii;

const int N = 2e5 + 5;

int n, a[N], b[N], f[N], c[N], suf[N];
vector<int> ver[2];

int main(){
    scanf("%d", &n);
    rep(i, n) scanf("%d%d", &a[i], &b[i]);
    rep(i, n){
        int cost = 0;
        if (a[i] > b[i]) cost = 1, swap(a[i], b[i]);
        if (a[i] > n){
            printf("-1\n");
            return 0;
        }
        f[a[i] - 1] = b[i];
        c[a[i] - 1] = cost;
    }
    int c0 = 0, c1 = 0, ret = 0;
    int premi = 2 * n + 1;
    for (int i = n - 1; i >= 0; -- i)
        suf[i] = max(suf[i + 1], f[i]);
    rep(i, n){
        premi = min(premi, f[i]);
        if (ver[0].empty() || ver[0][ver[0].size() - 1] > f[i]){
            ver[0].PB(f[i]);
            c0 += c[i];
        } else if (ver[1].empty() || ver[1][ver[1].size() - 1] > f[i]){
            ver[1].PB(f[i]);
            c1 += c[i];
        } else {
            printf("-1\n");
            return 0;
        }
        if (premi > suf[i + 1]){
            int s0 = ver[0].size(), s1 = ver[1].size();
            ret += min(s0 - c0 + c1, c0 + s1 - c1);
            ver[0].clear();
            ver[1].clear();
            c0 = c1 = 0;
        }
    }
    printf("%d\n", ret);
    return 0;
}