#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=100005;
int n;
long long ans,a[N],b[N],c[N];
int main()
{
freopen("snack.in","r",stdin);
freopen("snack.out","w",stdout);
scanf("%d",&n);
for(int i=1;i<=n;++i) scanf("%lld%lld",&a[i],&b[i]),c[i]=c[i-1]+b[i-1]-a[i-1];
sort(c+1,c+1+n);
int mid=c[n/2+1];
for(int i=1;i<=n;++i) ans+=abs(c[i]-mid);
printf("%lld",ans);
return 0;
}