#include <bits/stdc++.h>

using namespace std;

#define ARG4(_1,_2,_3,_4,...) _4
 
#define forn3(i,l,r) for (int i = int(l); i < int(r); ++i)
#define forn2(i,n) forn3 (i, 0, n)
#define forn(...) ARG4(__VA_ARGS__, forn3, forn2) (__VA_ARGS__)
 
#define ford3(i,l,r) for (int i = int(r) - 1; i >= int(l); --i)
#define ford2(i,n) ford3 (i, 0, n)
#define ford(...) ARG4(__VA_ARGS__, ford3, ford2) (__VA_ARGS__)

#define pb push_back
#define mp make_pair
#define fs first
#define sc second
#define sz(a) ((int) (a).size())
#define all(x) (x).begin(), (x).end()

const long double eps = 1e-9;
const int inf = (1 << 30) - 1;
const long long inf64 = (1LL << 62) - 1;
const long double pi = acos(-1);

template<typename T> inline T abs (T x) {return x < 0 ? -x : x;}
template<typename T> inline T sqr (T x) {return x * x;}

template<typename T1, typename T2> inline bool umx (T1& a, T2 b) {if (a < b) {a = b; return 1;} return 0;}
template<typename T1, typename T2> inline bool umn (T1& a, T2 b) {if (b < a) {a = b; return 1;} return 0;}

void Solve(int ti) {
    int n;
    cin >> n;

    vector<int> a(n);
    vector<int> b(n);
    for (int i = 0; i < n; ++i) {
        cin >> a[i] >> b[i];
        --a[i], --b[i];
    }

    for (int i = 0; i < n; ++i) {
        if (a[i] < n && b[i] < n) {
            cout << -1 << "\n";
            return;
        }
    }

    vector<pair<pair<int, int>, int>> ab_sw(n);
    for (int i = 0; i < n; ++i) {
        if (a[i] < b[i]) {
            ab_sw[i] = mp(mp(a[i], b[i]), 0);
        } else {
            ab_sw[i] = mp(mp(b[i], a[i]), 1);
        }
    }

    sort(all(ab_sw));

    vector<pair<int, int>> f_sw(n);
    for (int i = 0; i < n; ++i) {
        f_sw[i] = mp(ab_sw[i].fs.sc, ab_sw[i].sc);
    }

    reverse(all(f_sw));

    vector<int> f(n);
    vector<int> sw(n);
    for (int i = 0; i < n; ++i) {
        f[i] = f_sw[i].fs;
        sw[i] = f_sw[i].sc;
    }

    vector<int> max_pref(n);
    max_pref[0] = f[0];
    for (int i = 1; i < n; ++i) {
        max_pref[i] = max(max_pref[i - 1], f[i]);
    }

    vector<int> min_suff(n);
    min_suff[n - 1] = f[n - 1];
    for (int i = n - 2; i >= 0; --i) {
        min_suff[i] = min(min_suff[i + 1], f[i]);
    }

    int res = 0;
    for (int left = 0; left < n; ) {
        int right = left + 1;
        while (right < n && max_pref[right - 1] > min_suff[right]) {
            ++right;
        }

        int ind0 = -1;
        int ind1 = -1;
        int cur_res = 0;
        for (int i = left; i < right; ++i) {
            if (ind0 == -1 || f[i] > f[ind0]) {
                cur_res += (sw[i] != 0);
                ind0 = i;
            } else if (ind1 == -1 || f[i] > f[ind1]) {
                cur_res += (sw[i] != 1);
                ind1 = i;
            } else {
                cout << -1 << "\n";
                return;
            }
        }

        res += min(cur_res, (right - left) - cur_res);
        left = right;
    }

    cout << res << "\n";
}

int main () {
    ios_base::sync_with_stdio(0);
    std::cin.tie(nullptr);
    
    // clock_t start_clock = clock();
    
    // freopen("_input.txt", "rt", stdin);
    // freopen("_output.txt", "wt", stdout);

    int tc = 1;
    // cin >> tc;
    forn (ti, tc) {
        Solve(ti);
    }
    
    // double total_seconds = (double) (clock() - start_clock) / CLOCKS_PER_SEC;
    // cerr << "Working time: " << total_seconds << "s." << endl;

    return 0;
}
