#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;

const int INF = 1000000000;

struct Node {
  int a, b;
  bool flip;
  bool operator<(const Node& t) const {
    return a < t.a;
  }
};

bool Solve(int l, int r, vector<Node>& x, int& ans) {
  set<int> s;
  for (int i = l; i < r; ++i) s.insert(x[i].b);
  int aa = INF, bb = INF;
  vector<int> a, b;
  for (int i = l; i < r; ++i) {
    if (x[i].b == *s.rbegin()) {
      if (aa > x[i].b) {
        a.push_back(i);
        aa = x[i].b;
        s.erase(x[i].b);
      } else 
      if (bb > x[i].b) {
        b.push_back(i);
        bb = x[i].b;
        s.erase(x[i].b);
      } else {
        return false;
      }
    } else {
      if (aa > *s.rbegin()) {
        if (bb <= x[i].b) return false;
        bb = x[i].b;
        b.push_back(i);
        s.erase(x[i].b);
      } else
      if (bb > *s.rbegin()) {
        if (aa <= x[i].b) return false;
        aa = x[i].b;
        a.push_back(i);
        s.erase(x[i].b);
      } else {
        return false;
      }
    }
  }
  int cnt = 0;
  for (int i = 0; i < a.size(); ++i) {
    if (x[a[i]].flip) ++cnt;
  }
  for (int i = 0; i < b.size(); ++i) {
    if (!x[b[i]].flip) ++cnt;
  }
  ans += min(cnt, r - l - cnt);
  return true;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  vector<Node> x(n);
  bool flag = true;
  for (int i = 0; i < n; ++i) {
    cin >> x[i].a >> x[i].b;
    if (x[i].a > x[i].b) {
      swap(x[i].a, x[i].b);
      x[i].flip = true;
    }
    if (x[i].a > n || x[i].b <= n) flag = false;
  }
  if (!flag) {
    cout << "-1\n";
    return 0;
  }
  sort(x.begin(), x.end());
  stack<int> maxx;
  maxx.push(0);
  for (int i = n - 1; i >= 0; --i) {
    maxx.push(max(maxx.top(), x[i].b));
  }
  int ans = 0;
  for (int i = 0; i < n; ) {
    int j = i;
    int prem = x[j].b;
    maxx.pop();
    while (maxx.top() > prem) {
      ++j;
      prem = min(prem, x[j].b);
      maxx.pop();
    }
    ++j;
    if (!Solve(i, j, x, ans)) {
      cout << "-1\n";
      return 0;
    }
    i = j;
  }
  cout << ans << endl;
  return 0;
}