#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <set>
#include <vector>
using namespace std;

const int MAXN = 400005;
int h[MAXN];
int c[MAXN];

int gao(int min, int max) {
  if (min > max) {
    return 0;
  }
  if (h[max] == min || h[min] == max) {
     return gao(min + 1, max - 1);
  } else {
    int zheng = 0;
    int fan = 0;
    int value1;
    int value2;
    if (h[min] && h[max]) {
      if (h[min] < h[max]) {
        return -1;
      }
      value1 = h[min];
      value2 = h[max];
      fan = 2;
    } else if (h[min] && c[max]) {
      if (h[min] < c[max]) {
        return -1;
      }
      value1 = h[min];
      value2 = c[max];
      zheng = 1;
      fan = 1;
    } else if (c[min] && h[max]) {
      if (c[min] < h[max]) {
        return -1;
      }
      value1 = c[min];
      value2 = h[max];
      zheng = 1;
      fan = 1;
    } else if (c[min] && c[max]) {
      if (c[min] < c[max]) {
        return -1;
      }
      value1 = c[min];
      value2 = c[max];
      zheng = 2;
    }
      int l = value1;
      int r = value2;
      int caozuo = 0;
      int caozuofan = 0;
      set<int> haveGet;
      while (true) {
        vector<int> needPutl;
        vector<int> needPutr;
        for (int i = min + 1; i < r; ++i) {
          if (haveGet.find(i) == haveGet.end()) {
            needPutl.emplace_back(i);
          }
        }
        for (int i = max - 1; i > l; --i) {
          if (haveGet.find(i) == haveGet.end()) {
            needPutr.emplace_back(i);
          }
        }
        if (needPutl.empty() && needPutr.empty()) {
          int res = gao(r + 1, l - 1);
          if (res == -1) {
            return -1;
          }
          return std::min(res + caozuo + zheng, res + caozuofan + fan);
        }
        haveGet.clear();
        min = r;
        max = l;
        for (int j = 0; j < needPutl.size(); ++j) {
          int i = needPutl[j];
          if (h[i]) {
            if (h[i] > l || h[i] < r) {
              return -1;
            }
            l = h[i];
            ++caozuofan;
          } else {
            if (c[i] > l || c[i] < r) {
              return -1;
            }
            l = c[i];
            ++caozuo;
          }
          haveGet.insert(l);
        }
        for (int j = 0; j < needPutr.size(); ++j) {
          int i = needPutr[j];
          if (h[i]) {
            if (h[i] > l || h[i] < r) {
              return -1;
            }
            r = h[i];
            ++caozuofan;
          } else {
            if (c[i] > l || c[i] < r) {
              return -1;
            }
            r = c[i];
            ++caozuo;
          }
          haveGet.insert(r);
        }
      }
  }
}

int main() {
  int n;
  scanf("%d", &n);
  for (int i = 0; i < n; ++i) {
    int a, b;
    scanf("%d%d", &a, &b);
    h[a] = b;
    c[b] = a;
  }
  int res = gao(1, n * 2);
  printf("%d\n", res);
  return 0;
}
