#include<bits/stdc++.h>
using namespace std;
#define int long long
int n, a[100010], b[100010], c[100010];
signed main() {
freopen("snack.in","r",stdin);
freopen("snack.out","w",stdout);
cin >> n;
for(int i = 1; i <= n; i ++) {
cin >> a[i] >> b[i]; //c为前缀和数组
c[i] = c[i - 1] + b[i - 1] - a[i - 1]; //求出前i堆剩余多少
}
sort(c + 1, c + n + 1);
int mid = c[(n + 1) / 2], m, ans = 0; //找中位数
m = -mid;
for(int i = 1; i <= n; i ++) {
ans += abs(c[i] + m);
}
cout << ans << endl;
return 0;
}