#include<bits/stdc++.h>
#define ll long long
#define fi first
#define se second
using namespace std;

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int n, a, b, sol = 0;
    cin >> n;

    vector<pair<pair<int, int>, pair<int, int>>> vp; // smaller, bigger, turned, id
    for (int i = 1; i <= n; i++) {
        cin >> a >> b;
        if (min(a, b) > n) {
            cout << -1 << "\n";
            return 0;
        }
        if (a < b) vp.push_back({{a, b}, {1, i}});
        else vp.push_back({{b, a}, {2, i}});
    }

    sort(vp.begin(), vp.end());
    vector<int> minL(n), maxR(n);
    minL[0] = vp[0].fi.se;
    maxR[n - 1] = vp[n - 1].fi.se;
    for (int i = 1; i < n; i++) minL[i] = min(minL[i - 1], vp[i].fi.se);
    for (int i = n - 2; i >= 0; i--) maxR[i] = max(maxR[i + 1], vp[i].fi.se );

    vector<int> points;
    points.push_back(0);

    for (int i = 1; i < n; i++) {
        if (minL[i - 1] > maxR[i]) points.push_back(i);
    }
    points.push_back(n);

    vector<int> st(n);

    for (int i = 0; i < points.size() - 1; i++) {
        int lt = points[i];
        int rt = points[i + 1] - 1;
        int min1 = n * 3, min2 = n * 3;

        for (int j = lt; j <= rt; j++) {
            if (vp[j].fi.se < min1) {
                st[j] = 1;
                min1 = vp[j].fi.se;
            }
            else if (vp[j].fi.se < min2) {
                st[j] = 2;
                min2 = vp[j].fi.se;
            }
            else {
                cout << -1 << "\n";
                return 0;
            }
        }
        int cnt = 0;
        for (int j = lt; j <= rt; j++) {
            if (st[j] != vp[j].se.fi) cnt++;
        }

        sol += min(cnt, rt - lt + 1 - cnt);
    }

    cout << sol << "\n";

//    for (auto e : points) cout << e << " ";

    return 0;
}
