/+ dub.sdl:
    name "E"
    dependency "dcomp" version=">=0.6.0"
+/

import std.stdio, std.algorithm, std.range, std.conv;
import std.container.rbtree;

// import dcomp.scanner;

int main() {
    auto sc = new Scanner(stdin);
    int n;
    sc.read(n);
    long[] l = new long[n], r = new long[n];
    long height = 0;
    sc.read(l[0], r[0]);
    long a = r[0]-l[0];

    auto tr1 = redBlackTree!true(l[0]);
    long off1;
    auto tr2 = redBlackTree!true(l[0]);
    long off2;
//    writeln(tr1, " ", tr2);
    foreach (i; 1..n) {
        sc.read(l[i], r[i]);
        long b = r[i]-l[i];
        // minimize [x-a, x+b]
        off1 -= b;
        off2 += a;
        long lz = tr1.back + off1, rz = tr2.front + off2;
//        writeln(lz, " ", rz, " ab:", off1, " ", off2);
        if (l[i] < lz) {
            tr2.insert(tr1.back + off1 - off2);
            tr1.removeBack;
            tr1.insert(l[i] - off1);
            tr1.insert(l[i] - off1);
            height += lz - l[i];
        } else if (l[i] < rz) {
            tr1.insert(l[i] - off1);
            tr2.insert(l[i] - off2);
        } else {
            tr1.insert(tr2.front + off2 - off1);
            tr2.removeFront;
            tr2.insert(l[i] - off2);
            tr2.insert(l[i] - off2);
            height += l[i] - rz;
        }
        a = b;
//        writeln(tr1, " ", tr2);
    }
    writeln(height);
    return 0;
}
/* IMPORT /home/yosupo/Program/dcomp/source/dcomp/scanner.d */
// module dcomp.scanner;

class Scanner {
    import std.stdio : File;
    import std.conv : to;
    import std.range : front, popFront, array, ElementType;
    import std.array : split;
    import std.traits : isSomeChar, isStaticArray, isArray; 
    import std.algorithm : map;
    File f;
    this(File f) {
        this.f = f;
    }
    string[] buf;
    private bool succ() {
        while (!buf.length) {
            if (f.eof) return false;
            buf = f.readln.split;
        }
        return true;
    }
    private bool readSingle(T)(ref T x) {
        if (!succ()) return false;
        static if (isArray!T) {
            alias E = ElementType!T;
            static if (isSomeChar!E) {
                //string or char[10] etc
                x = buf.front;
                buf.popFront;
            } else {
                static if (isStaticArray!T) {
                    //static
                    assert(buf.length == T.length);
                }
                x = buf.map!(to!E).array;
                buf.length = 0;                
            }
        } else {
            x = buf.front.to!T;
            buf.popFront;            
        }
        return true;
    }
    int read(T, Args...)(ref T x, auto ref Args args) {
        if (!readSingle(x)) return 0;
        static if (args.length == 0) {
            return 1;
        } else {
            return 1 + read(args);
        }
    }
}

unittest {
    import std.path : buildPath;
    import std.file : tempDir;
    import std.algorithm : equal;
    import std.stdio : File;
    string fileName = buildPath(tempDir, "kyuridenanmaida.txt");
    auto fout = File(fileName, "w");
    fout.writeln("1 2 3");
    fout.writeln("ab cde");
    fout.writeln("1.0 1.0 2.0");
    fout.close;
    Scanner sc = new Scanner(File(fileName, "r"));
    int a;
    int[2] b;
    char[2] c;
    string d;
    double e;
    double[] f;
    sc.read(a, b, c, d, e, f);
    assert(a == 1);
    assert(equal(b[], [2, 3]));
    assert(equal(c[], "ab"));
    assert(equal(d, "cde"));
    assert(e == 1.0);
    assert(equal(f, [1.0, 2.0]));
}


Measured memory_bytes: 22780