#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <fstream>
#include <set>
#include <deque>
#include <math.h>
#include <iomanip>
#define ll long long
#define ld long double

using namespace std;

int all = 0;
int price = 0;

void dfs(int u, vector<int>& used, vector<int> &col, vector<vector<int>>& graph) {
    all++;
    if (used[u] != col[u]) {
        price++;
    }
    for (int v : graph[u]) {
        if (used[v] == -1) {
            used[v] = 1 - used[u];
            dfs(v, used, col, graph);
        }
    }
}

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
    int n;
    cin >> n;
    vector<int> col(n);
    vector<pair<int, int>> arr(n);
    vector<int> L(2 * n);
    for (int i = 0; i < 2 * n; ++i) {
        L[i] = -1;
    }
    for (int i = 0; i < n; ++i) {
        cin >> arr[i].first >> arr[i].second;
        if (arr[i].first > arr[i].second) {
            col[i] = 1;
            swap(arr[i].first, arr[i].second);
        }
        arr[i].first--;
        arr[i].second--;
        L[arr[i].first] = i;
    }
    for (int i = 0; i < n; ++i) {
        if (L[i] == -1) {
            cout << "-1\n";
            return 0;
        }
    }
    vector<int> r(n);
    for (int i = 0; i < n; ++i) {
        r[i] = arr[L[i]].second;
    }
    vector<int> mn(n);
    vector<int> mni(n);
    vector<int> mx(n);
    vector<int> mxi(n);
    mn[0] = r[0];
    mni[0] = 0;
    mx[n - 1] = r[n - 1];
    mxi[n - 1] = n - 1;
    for (int i = 1; i < n; ++i) {
        if (r[i] < mn[i - 1])
            mni[i] = i;
        else
            mni[i] = mni[i - 1];
        mn[i] = min(mn[i - 1], r[i]);
    }
    for (int i = n - 2; i >= 0; --i) {
        if (r[i] > mx[i + 1])
            mxi[i] = i;
        else
            mxi[i] = mxi[i + 1];
        mx[i] = max(mx[i + 1], r[i]);
    }
    for (int i = 1; i < n - 1; ++i) {
        if (mn[i - 1] < r[i] && r[i] < mx[i + 1]) {
            cout << "-1\n";
            return 0;
        }
    }
    //cerr << "here\n";
    vector<vector<int>> graph(n);
    vector<int> st;
    for (int i = 0; i < n; ++i) {
        if (i < n - 1 && r[i] < mx[i + 1]) {
            st.push_back(i);
            continue;
        }
        if (i > 0 && r[i] > mn[i - 1]) {
            int useful = st.back();
            while (!st.empty()){
                if (r[st.back()] < r[i]) {
                    graph[L[st.back()]].push_back(L[i]);
                    graph[L[i]].push_back(L[st.back()]);
                    st.pop_back();
                    continue;
                }
                break;
            }
            st.push_back(useful);

        }
    }
    vector<int> used(n);
    int ans = 0;
    for (int i = 0; i < n; ++i) {
        used[i] = -1;
    }
    for (int i = 0; i < n; ++i) {
        if (used[i] == -1) {
            price = 0;
            all = 0;
            used[i] = 0;
            dfs(i, used, col, graph);
            ans += min(price, all - price);
        }
    }
    cout << ans << "\n";
}
//*/