// Arup Guha
// 3/9/2018
// Solution to 2018 February USACO Silver Problem: Teleportation

import java.io.*;
import java.util.*;

public class teleport_silver {

	public static int n;
	public static ArrayList<event> list;

	public static void main(String[] args) throws Exception {

		// Read the data.
		BufferedReader stdin = new BufferedReader(new FileReader("teleport.in"));
		StringTokenizer tok = new StringTokenizer(stdin.readLine());
		n = Integer.parseInt(tok.nextToken());

		long cur = 0;

		// Convert path to set of two ranges one where cost decreases, one where it increases.
		list = new ArrayList<event>();
		for (int i=0; i<n; i++) {
			tok = new StringTokenizer(stdin.readLine());
			int a = Integer.parseInt(tok.nextToken());
			int b = Integer.parseInt(tok.nextToken());
			int delta = Math.abs(b-a) - Math.abs(a);

			// Cost without teleportation.
			cur += Math.abs(b-a);

			// Only add events if the teleporter might help us.
			if (delta > 0) {

				// Here our cost goes down by 1 for each unit y.
				list.add(new event(b-delta, -1));

				// Here it goes up by one, but I am also offsetting for that -1...
				list.add(new event(b, 2));

				// This is what evens it out.
				list.add(new event(b+delta, -1));
			}
		}

		// Sort events.
		Collections.sort(list);

		// Consolidate so x's are unique.
		list = clean(list);

		// Initialize stuff.
		long delta = list.get(0).delta, res = cur;
		int prev = 0;

		// Go through each event event.
		for (int i=1; i<list.size(); i++) {

			// How long we've had this delta.
			int xDist = list.get(i).x - list.get(i-1).x;

			// Update our current total.
			cur = cur + delta*xDist;

			// If necessary, update this.
			res = Math.min(res, cur);

			// Update our delta.
			delta += list.get(i).delta;
		}

		// Ta da!
		PrintWriter out = new PrintWriter(new FileWriter("teleport.out"));
		out.print(res);
		out.close();
		stdin.close();
	}

	public static ArrayList<event> clean(ArrayList<event> list) {

		ArrayList<event> res = new ArrayList<event>();

		int i = 0;
		while (i < list.size()) {

			// Move j through range of same x values.
			int j = i, sum = 0;
			while (j < list.size() && list.get(j).x == list.get(i).x) {
				sum += list.get(j).delta;
				j++;
			}

			// Our consolidated event.
			res.add(new event(list.get(i).x, sum));

			// Update i.
			i = j;
		}

		return res;
	}
}

class event implements Comparable<event> {

	public int x;
	public int delta;

	public event(int myx, int myd) {
		x = myx;
		delta = myd;
	}

	public int compareTo(event other) {
		return this.x - other.x;
	}
}