#include <bits/stdc++.h>
using namespace std;

#define dbg(x...) \
    do { \
        cout << #x << " -> "; \
        err(x); \
    } while (0)

void err() {
    cout << endl;
}

template<class T, class... Ts>
void err(const T &arg, const Ts &... args) {
    cout << arg << ' ';
    err(args...);
}

#define pb push_back
#define fs first
#define sc second
typedef long long ll;
typedef pair<int, int> pi;

const int maxn = 2e5 + 7;
const int inf = 0x3f3f3f3f;
const int mod = 1e9 + 7;

int f[maxn * 2];
bool up[maxn * 2];

int a[maxn * 2];
int mini[maxn * 2];
int mx[maxn * 2];

int check(vector<int> &x, vector<int> &y) {
    int ans = 0;
    for (int i : x) {
        if (up[i]) ans++;
    }
    for (int i : y) {
        if (!up[i]) ans++;
    }
    return ans;
}

int solve(vector<int> &vec) {
    int p[2] = {inf, inf + 1};
    vector<int> x, y;
    for (int i : vec) {
        int d = a[i];
        if (p[0] < p[1]) {
            if (d < p[0]) {
                x.push_back(i);
                p[0] = d;
            } else if (d < p[1]) {
                y.push_back(i);
                p[1] = d;
            } else {
                return -1;
            }
        } else {
            if (d < p[1]) {
                y.push_back(i);
                p[1] = d;
            } else if (d < p[0]) {
                x.push_back(i);
                p[0] = d;
            } else {
                return -1;
            }
        }
    }
    return min(check(x, y), check(y, x));
}

void run() {
    int n;
    scanf("%d", &n);
    for (int i = 1; i <= n; i++) {
        int u, v;
        scanf("%d%d", &u, &v);
        up[u] = true;
        up[v] = false;
        f[u] = v;
        f[v] = u;
    }
    for (int i = 1; i <= n; i++) {
        if (f[i] <= n) {
            puts("-1");
            return;
        }
    }
    for (int i = 1; i <= n; i++) {
        a[i] = f[i];
    }
    mini[0] = inf;
    for (int i = 1; i <= n; i++) mini[i] = min(mini[i - 1], a[i]);
    for (int i = n; i >= 1; i--) mx[i] = max(mx[i + 1], a[i]);
    vector<int> vec;
    int ans = 0;
    for (int i = 1; i <= n; i++) {
        vec.push_back(i);
        if (mini[i] > mx[i + 1]) {
            int tmp = solve(vec);
            if (tmp == -1) {
                ans = -1;
                break;
            }
            ans += tmp;
            vec.clear();
        }
    }
    printf("%d\n", ans);
}

int main() {

    int T = 1;
//    scanf("%d", &T);
    while (T--) {
        run();
    }


    return 0;
}