// Sometimes, the questions are complicated - and the answers are simple. //
#pragma GCC optimize ("O3")
#pragma GCC optimize ("unroll-loops")
#include<iostream>
#include <bits/stdc++.h>
#define ll long long
#define ld  long double
#define IO ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
using namespace std;
const int N = 4e5 + 5;
int n, max_suffix[N];
bool flipped[N];
int solve(vector<int> & cur) {
    int last[] = {4 * n, 4 * n};
    int sz[] = {0, 0};
    int cost[] = {0, 0};
    for(auto i : cur) {
        int j = -1;
        if(i < last[0] && i < last[1]) {
            j = (last[0] < last[1] ? 0 : 1);
        } else if(i < last[0])
            j = 0;
        else if(i < last[1])
            j = 1;
        else
            return 4 * n;

        last[j] = i;
        sz[j]++;
        cost[j] += flipped[i];
    }
    return min(cost[0] + sz[1] - cost[1], cost[1] + sz[0] - cost[0]);
}
int main() {

    IO
    cin >> n;
    vector<pair<int, int> > v;
    for(int i = 1; i <= n; i++) {
        int a, b;
        cin >> a >> b;

        if((a + n - 1) / n == (b + n - 1) / n)
            return cout << -1, 0;

        if(a > b) {
            swap(a, b);
            flipped[b] = 1;
        }
        v.push_back({a, b});
    }

    sort(v.begin(), v.end());
    for(int i = n - 1; i >= 0; i--)
        max_suffix[i] = max(max_suffix[i + 1], v[i].second);

    int min_prefix = 4 * n;
    ll ans = 0;
    vector<int> cur;
    for(int i = 0; i < n; i++) {

        min_prefix = min(min_prefix, v[i].second);
        cur.push_back(v[i].second);

        if(min_prefix > max_suffix[i + 1]) {
            ans += solve(cur);
            cur.clear();
        }
    }
    if(ans > n)
        ans = -1;
    cout << ans;

}
