import java.util.*;
public class Main98 {
public static ArrayList<int[]> position = new ArrayList<>();
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
while(in.hasNext()) {//*
int n = Integer.parseInt(in.nextLine());
for(int i = 0; i < n; i++) {
String[] str = in.nextLine().split(" ");
int[] temp = new int[2];
temp[0] = Integer.parseInt(str[0]);
temp[1] = Integer.parseInt(str[1]);
position.add(temp);
}
position.sort((a, b) -> a[0] != b[0] ? a[0] - b[0] : a[1] - b[1]);
int res = Integer.MAX_VALUE;
int min = position.get(0)[0];
int max = position.get(n - 1)[1];
for (int j = min; j <= max; j++) {
int tRes = 0;
for (int[] tposition : position) {
int end = tposition[1];
int start = tposition[0];
if (end < j ) {
tRes = tRes - end + j;
} else if (start > j) { //
tRes = tRes + start - j;
}
}
if(tRes <= res) res = tRes;
}
System.out.println(res);
}
}
}