#include<iostream>
#include<cmath>
using namespace std;
int a[200000];
int n;
bool b[200000];
unsigned int mm(int k)
{
    if(k==n-1)
    {
        for(int i=0; i<n; i++)
        {
            if(b[i]==0) { return (unsigned int)(abs(i-a[k]));}
        }

    }
    unsigned int minz=10000000;
    for(int i=0; i<n; i++)
    {
        if(b[i]==0)
        {
            b[i]=1;
            if(minz>(unsigned int)(abs(i-a[k])+mm(k+1))) minz=(unsigned int)(abs(i-a[k])+mm(k+1));
            b[i]=0;
        }
    }
    return minz;
}
int main()
{

    cin>>n;


    int r;
    int k[n];
    for(int i=0; i<n; i++)
    {
        cin>>r;
        r--;
        a[i]=r;
        cin>>k[i];
        k[i]--;
    }
    int mini=0;
    mini+=mm(0);
    //cout<<mini;
    for(int i=0; i<n; i++)
    {
        a[i]=k[i];

    }
    cout<<endl;
    mini+=mm(0);
    cout<<mini;
}