import java.util.Scanner;
import java.util.Arrays;
public class Test7   {
    int maxn = 3000;
    //floors: 0 - 8
    //9 - empty
    //front: the next employee
    //the importance of a: if a < 9 then the lift is full; else a - 9 means the floor the elevator is on
    public static int hash(int front, int a, int b, int c, int d)   {
        if (a < 9) return (front * 2500 + d + (c * (c + 1)) / 2 + (b * (b + 1) * (b + 2)) / 6 + (a * (a + 1) * (a + 2) * (a + 3)) / 24);
        return front * 2500 + d + (c * (c + 1)) / 2 + (b * (b + 1) * (b + 2)) / 6 + (9 * 10 * 11 * 12) / 24 + 1 + 221 * (a - 9);
    }
    public static void main(String Args[])   {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int[] st = new int[n];
        int[] ed = new int[n];
        for (int i = 0; i < n; i++)   {
            st[i] = sc.nextInt() - 1;
            ed[i] = sc.nextInt() - 1;
        }
        int sz = 6000000;
        int[] ans = new int[sz];
        int inf = 10000000;
        for (int i = 0; i < sz; i++) ans[i] = inf;
        ans[hash(0, 9, 9, 9, 9)] = 0;
        int[] ep = new int[4];
        int tp = 0; //current floor
        int ft = 0; //front
        int rm = 0; //room in elevator
        int t = 0;
        int[] ord = new int[4];
        for (int front = 0; front <= n; front++)
            for (int a = 0; a < 18; a++)
            for (int b = 0; b <= Math.min(a, 9); b++)
            for (int c = 0; c <= b; c++)
            for (int d = 0; d <= c; d++)   {
            ft = front;
            //current stop of lift
            if (a < 9 && front > 0)   {
                tp = st[front - 1];
            }
            else tp = a - 9;
            //conversion
            t = ans[hash(front,a,b,c,d)];
            if (t < inf)  {
        //        System.out.print(front);
        //        System.out.print(a);
        //         System.out.print(b);
        //
         //         System.out.print(c);
         //          System.out.print(d);
         //       System.out.println(t);
                rm = 0;
                if (a < 9)   {
                    ep[0] = 9 + a; //goes to a
                    ord[rm] = 0;
                    rm++;
                    t += Math.abs(tp - a);
                    t++;
                    if (b == a)  {
                        ep[1] = 9;
                        ord[rm] = 1;
                        rm++;
                        t++;
                    }
                    else ep[1] = b;
                    if (c == a)  {
                        ep[2] = 9;
                        ord[rm] = 2;
                        rm++;
                        t++;
                    }
                    else ep[2] = c;
                    if (d == a)  {
                        ep[3] = 9;
                        ord[rm] = 3;
                        rm++;
                        t++;
                    }
                    else ep[3] = d;
                    //people enter
                    while (rm > 0 && ft < n && st[ft] == a)   {
                        rm--;
                        ep[ord[rm]] = ed[ft];
                        ft++;
                        t++;
                    }
                    Arrays.sort(ep);
                    ans[hash(ft, ep[3], ep[2], ep[1], ep[0])] = Math.min(ans[hash(ft, ep[3], ep[2], ep[1], ep[0])], t);
                }
                else if (front < n) {
                    //goes to next person
                    ord[0] = 1;
                    rm = 1;
                    if (b == 9) {
                        ord[rm] = 1;
                        rm++;
                    }
                    if (c == 9) {
                        ord[rm] = 2;
                        rm++;
                    }
                    if (d == 9) {
                        ord[rm] = 3;
                        rm++;
                    }
                    ep[0] = 9 + st[front];
                    if (b == st[front])   {
                        ord[rm] = 1;
                        rm++;
                        t++;
                        ep[1] = 9;
                    }
                    else ep[1] = b;
                    if (c == st[front])   {
                        ord[rm] = 2;
                        rm++;
                        t++;
                        ep[2] = 9;
                    }
                    else ep[2] = c;
                    if (d == st[front])   {
                        ord[rm] = 3;
                        rm++;
                        t++;
                        ep[3] = 9;
                    }
                    else ep[3] = d;
                    t+= Math.abs(tp - st[front]);
                    while (rm > 0 && ft < n && st[ft] == st[front]){
                        rm--;
                        ep[ord[rm]] = ed[ft];
                        ft++;
                        t++;
                    }
                    Arrays.sort(ep);
                    ans[hash(ft, ep[3], ep[2], ep[1], ep[0])] = Math.min(ans[hash(ft, ep[3], ep[2], ep[1], ep[0])], t);
                }
                ft = front;
                t = ans[hash(front,a,b,c,d)];
                rm = 0;
                if (b < 9 && b != a)   {
                    ep[1] = Math.min(a, 9);
                    if (a >= 9) {
                        rm++;
                        ord[rm] = 0;
                    }
                    ep[0] = 9 + b; //goes to b
                    ord[rm] = 1;
                    rm++;
                    t += Math.abs(tp - b);
                    t++;
                    if (c == b)  {
                        ep[2] = 9;
                        ord[rm] = 2;
                        rm++;
                        t++;
                    }
                    else ep[2] = c;
                    if (d == b)  {
                        ep[3] = 9;
                        ord[rm] = 3;
                        rm++;
                        t++;
                    }
                    else ep[3] = d;
                    //people enter
                    while (rm > 0 && ft < n && st[ft] == b)   {
                        rm--;
                        ep[ord[rm]] = ed[ft];
                        ft++;
                        t++;
                    }
                    Arrays.sort(ep);
                    ans[hash(ft, ep[3], ep[2], ep[1], ep[0])] = Math.min(ans[hash(ft, ep[3], ep[2], ep[1], ep[0])], t);
                }
                ft = front;
                t = ans[hash(front,a,b,c,d)];
                rm = 0;
                if (c < 9 && c != a && c != b)   {
                    ep[1] = b;
                    if (a >= 9) {
                        ord[rm] = 0;
                        rm++;

                    }
                    if (b == 9) {
                        ord[rm] = 1;
                        rm++;
                    }
                    ep[2] = Math.min(a, 9);

                    ep[0] = 9 + c; //goes to c
                    ord[rm] = 2;
                    rm++;
                    t += Math.abs(tp - c);
                    t++;
                    if (d == c)  {
                        ep[3] = 9;
                        ord[rm] = 3;
                        rm++;
                        t++;
                    }
                    else ep[3] = d;
                    //people enter
                    while (rm > 0 && ft < n && st[ft] == c)   {
                        rm--;
                        ep[ord[rm]] = ed[ft];
                        ft++;
                        t++;
                    }
                    Arrays.sort(ep);
                    ans[hash(ft, ep[3], ep[2], ep[1], ep[0])] = Math.min(ans[hash(ft, ep[3], ep[2], ep[1], ep[0])], t);
                }
                ft = front;
                t = ans[hash(front,a,b,c,d)];
                rm = 0;
                if (d < 9 && d != a && d != b && d != c)   {

                    ep[1] = b;
                    if (a >= 9) {
                        ord[rm] = 0;
                        rm++;
                    }
                    ep[2] = c;
                    if (b == 9) {
                        ord[rm] = 1;
                        rm++;
                    }
                    ep[3] = Math.min(a, 9);
                    if (c == 9) {
                        ord[rm] = 2;
                        rm++;
                    }
                    ord[rm] = 3;
                    rm++;
                    ep[0] = 9 + d; //goes to d
                    t += Math.abs(tp - d);
                    t++;
                    //people enter
                    while (rm > 0 && ft < n && st[ft] == d)   {
                        rm--;
                        ep[ord[rm]] = ed[ft];
                        ft++;
                        t++;
                    }
                    Arrays.sort(ep);
                    ans[hash(ft, ep[3], ep[2], ep[1], ep[0])] = Math.min(ans[hash(ft, ep[3], ep[2], ep[1], ep[0])], t);
                }
            }
        }
        int ansmin = inf;
        for (int j = 9; j < 18; j++)   {
            if (ans[hash(n, j, 9, 9, 9)] < ansmin) ansmin = ans[hash(n, j, 9, 9, 9)];
        }
        System.out.println(ansmin);
    }
}


import java.util.Scanner;
import java.util.Arrays;
public class Test7   {
    int maxn = 3000;
    //floors: 0 - 8
    //9 - empty
    //front: the next employee
    //the importance of a: if a < 9 then the lift is full; else a - 9 means the floor the elevator is on
    public static int hash(int front, int a, int b, int c, int d)   {
        if (a < 9) return (front * 2500 + d + (c * (c + 1)) / 2 + (b * (b + 1) * (b + 2)) / 6 + (a * (a + 1) * (a + 2) * (a + 3)) / 24);
        return front * 2500 + d + (c * (c + 1)) / 2 + (b * (b + 1) * (b + 2)) / 6 + (9 * 10 * 11 * 12) / 24 + 1 + 221 * (a - 9);
    }
    public static void main(String Args[])   {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int[] st = new int[n];
        int[] ed = new int[n];
        for (int i = 0; i < n; i++)   {
            st[i] = sc.nextInt() - 1;
            ed[i] = sc.nextInt() - 1;
        }
        int sz = 6000000;
        int[] ans = new int[sz];
        int inf = 10000000;
        for (int i = 0; i < sz; i++) ans[i] = inf;
        ans[hash(0, 9, 9, 9, 9)] = 0;
        int[] ep = new int[4];
        int tp = 0; //current floor
        int ft = 0; //front
        int rm = 0; //room in elevator
        int t = 0;
        for (int front = 0; front <= n; front++)
            for (int a = 0; a < 18; a++)
            for (int b = 0; b <= Math.min(a, 9); b++)
            for (int c = 0; c <= b; c++)
            for (int d = 0; d <= c; d++)   {
            ft = front;
            //current stop of lift
            if (a < 9 && front > 0)   {
                tp = st[front - 1];
            }
            else tp = a - 9;
            //conversion
            t = ans[hash(front,a,b,c,d)];
            if (t < inf)  {
        //        System.out.print(front);
        //        System.out.print(a);
        //         System.out.print(b);
        //
         //         System.out.print(c);
         //          System.out.print(d);
         //       System.out.println(t);
                rm = 0;
                if (a < 9)   {
                    ep[0] = 9 + a; //goes to a
                    rm++;
                    t += Math.abs(tp - a);
                    t++;
                    if (b == a)  {
                        ep[1] = 9;
                        rm++;
                        t++;
                    }
                    else ep[1] = b;
                    if (c == a)  {
                        ep[2] = 9;
                        rm++;
                        t++;
                    }
                    else ep[2] = c;
                    if (d == a)  {
                        ep[3] = 9;
                        rm++;
                        t++;
                    }
                    else ep[3] = d;
                    //people enter
                    while (rm > 0 && ft < n && st[ft] == a)   {
                        rm--;
                        ep[rm] = ed[ft];
                        ft++;
                        t++;
                    }
                    Arrays.sort(ep);
                    ans[hash(ft, ep[3], ep[2], ep[1], ep[0])] = Math.min(ans[hash(ft, ep[3], ep[2], ep[1], ep[0])], t);
                }
                else if (front < n) {
                    //goes to next person
                    rm = 1;
                    if (b == 9) rm++;
                    if (c == 9) rm++;
                    if (d == 9) rm++;
                    ep[0] = 9 + st[front];
                    if (b == st[front])   {
                        rm++;
                        t++;
                        ep[1] = 9;
                    }
                    else ep[1] = b;
                    if (c == st[front])   {
                        rm++;
                        t++;
                        ep[2] = 9;
                    }
                    else ep[2] = c;
                    if (d == st[front])   {
                        rm++;
                        t++;
                        ep[3] = 9;
                    }
                    else ep[3] = d;
                    t+= Math.abs(tp - st[front]);
                    while (rm > 0 && ft < n && st[ft] == st[front]){
                        rm--;
                        ep[rm] = ed[ft];
                        ft++;
                        t++;
                    }
                    Arrays.sort(ep);
                    ans[hash(ft, ep[3], ep[2], ep[1], ep[0])] = Math.min(ans[hash(ft, ep[3], ep[2], ep[1], ep[0])], t);
                }
                ft = front;
                t = ans[hash(front,a,b,c,d)];
                rm = 0;
                if (b < 9 && b != a)   {
                    ep[1] = Math.min(a, 9);
                    if (a >= 9) rm++;
                    ep[0] = 9 + b; //goes to b
                    rm++;
                    t += Math.abs(tp - b);
                    t++;
                    if (c == b)  {
                        ep[2] = 9;
                        rm++;
                        t++;
                    }
                    else ep[2] = c;
                    if (d == b)  {
                        ep[3] = 9;
                        rm++;
                        t++;
                    }
                    else ep[3] = d;
                    //people enter
                    while (rm > 0 && ft < n && st[ft] == b)   {
                        rm--;
                        ep[rm] = ed[ft];
                        ft++;
                        t++;
                    }
                    Arrays.sort(ep);
                    ans[hash(ft, ep[3], ep[2], ep[1], ep[0])] = Math.min(ans[hash(ft, ep[3], ep[2], ep[1], ep[0])], t);
                }
                ft = front;
                t = ans[hash(front,a,b,c,d)];
                rm = 0;
                if (c < 9 && c != a && c != b)   {
                    ep[1] = b;
                    if (b == 9) rm++;
                    ep[2] = Math.min(a, 9);
                    if (a >= 9) rm++;
                    ep[0] = 9 + c; //goes to c
                    rm++;
                    t += Math.abs(tp - c);
                    t++;
                    if (d == c)  {
                        ep[3] = 9;
                        rm++;
                        t++;
                    }
                    else ep[3] = d;
                    //people enter
                    while (rm > 0 && ft < n && st[ft] == c)   {
                        rm--;
                        ep[rm] = ed[ft];
                        ft++;
                        t++;
                    }
                    Arrays.sort(ep);
                    ans[hash(ft, ep[3], ep[2], ep[1], ep[0])] = Math.min(ans[hash(ft, ep[3], ep[2], ep[1], ep[0])], t);
                }
                ft = front;
                t = ans[hash(front,a,b,c,d)];
                rm = 0;
                if (d < 9 && d != a && d != b && d != c)   {
                    ep[1] = b;
                    if (b == 9) rm++;
                    ep[2] = c;
                    if (c == 9) rm++;
                    ep[3] = Math.min(a, 9);
                    if (a >= 9) rm++;
                    ep[0] = 9 + d; //goes to d
                    rm++;
                    t += Math.abs(tp - d);
                    t++;
                    //people enter
                    while (rm > 0 && ft < n && st[ft] == d)   {
                        rm--;
                        ep[rm] = ed[ft];
                        ft++;
                        t++;
                    }
                    Arrays.sort(ep);
                    ans[hash(ft, ep[3], ep[2], ep[1], ep[0])] = Math.min(ans[hash(ft, ep[3], ep[2], ep[1], ep[0])], t);
                }
            }
        }
        int ansmin = inf;
        for (int j = 9; j < 18; j++)   {
            if (ans[hash(n, j, 9, 9, 9)] < ansmin) ansmin = ans[hash(n, j, 9, 9, 9)];
        }
        System.out.println(ansmin);
    }
}


import java.util.Scanner;
import java.util.Arrays;
public class Test7   {
    int maxn = 3000;
    //floors: 0 - 8
    //9 - empty
    //front: the next employee
    //the importance of a: if a < 9 then the lift is full; else a - 9 means the floor the elevator is on
    public static int hash(int front, int a, int b, int c, int d)   {
        if (a < 9) return (front * 2500 + d + (c * (c + 1)) / 2 + (b * (b + 1) * (b + 2)) / 6 + (a * (a + 1) * (a + 2) * (a + 3)) / 24);
        return front * 2500 + d + (c * (c + 1)) / 2 + (b * (b + 1) * (b + 2)) / 6 + (9 * 10 * 11 * 12) / 24 + 1 + 221 * (a - 9);
    }
    public static void main(String Args[])   {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int[] st = new int[n];
        int[] ed = new int[n];
        for (int i = 0; i < n; i++)   {
            st[i] = sc.nextInt() - 1;
            ed[i] = sc.nextInt() - 1;
        }
        int sz = 6000000;
        int[] ans = new int[sz];
        int inf = 10000000;
        for (int i = 0; i < sz; i++) ans[i] = inf;
        ans[hash(0, 9, 9, 9, 9)] = 0;
        int[] ep = new int[4];
        int tp = 0; //current floor
        int ft = 0; //front
        int rm = 0; //room in elevator
        int t = 0;
        int[] ord = new int[4];
        for (int front = 0; front <= n; front++)  {
            for (int a = 0; a < 9; a++)
            for (int b = 0; b <= Math.min(a, 9); b++)
            for (int c = 0; c <= b; c++)
            for (int d = 0; d <= c; d++)   {
            ft = front;
            //current stop of lift
            if (a < 9 && front > 0)   {
                tp = st[front - 1];
            }
            else tp = a - 9;
            //conversion
            t = ans[hash(front,a,b,c,d)];
            if (t < inf)  {
        //        System.out.print(front);
        //        System.out.print(a);
        //         System.out.print(b);
        //
         //         System.out.print(c);
         //          System.out.print(d);
         //       System.out.println(t);
                rm = 0;
                if (a < 9)   {
                    ep[0] = 9 + a; //goes to a
                    ord[rm] = 0;
                    rm++;
                    t += Math.abs(tp - a);
                    t++;
                    if (b == a)  {
                        ep[1] = 9;
                        ord[rm] = 1;
                        rm++;
                        t++;
                    }
                    else ep[1] = b;
                    if (c == a)  {
                        ep[2] = 9;
                        ord[rm] = 2;
                        rm++;
                        t++;
                    }
                    else ep[2] = c;
                    if (d == a)  {
                        ep[3] = 9;
                        ord[rm] = 3;
                        rm++;
                        t++;
                    }
                    else ep[3] = d;
                    //people enter
                    while (rm > 0 && ft < n && st[ft] == a)   {
                        rm--;
                        ep[ord[rm]] = ed[ft];
                        ft++;
                        t++;
                    }
                    Arrays.sort(ep);
                    ans[hash(ft, ep[3], ep[2], ep[1], ep[0])] = Math.min(ans[hash(ft, ep[3], ep[2], ep[1], ep[0])], t);
                }
                else if (front < n) {
                    //goes to next person
                    ord[0] = 0;
                    rm = 1;
                    if (b == 9) {
                        ord[rm] = 1;
                        rm++;
                    }
                    if (c == 9) {
                        ord[rm] = 2;
                        rm++;
                    }
                    if (d == 9) {
                        ord[rm] = 3;
                        rm++;
                    }
                    ep[0] = 9 + st[front];
                    if (b == st[front])   {
                        ord[rm] = 1;
                        rm++;
                        t++;
                        ep[1] = 9;
                    }
                    else ep[1] = b;
                    if (c == st[front])   {
                        ord[rm] = 2;
                        rm++;
                        t++;
                        ep[2] = 9;
                    }
                    else ep[2] = c;
                    if (d == st[front])   {
                        ord[rm] = 3;
                        rm++;
                        t++;
                        ep[3] = 9;
                    }
                    else ep[3] = d;
                    t+= Math.abs(tp - st[front]);
                    while (rm > 0 && ft < n && st[ft] == st[front]){
                        rm--;
                        ep[ord[rm]] = ed[ft];
                        ft++;
                        t++;
                    }
                    Arrays.sort(ep);
                    ans[hash(ft, ep[3], ep[2], ep[1], ep[0])] = Math.min(ans[hash(ft, ep[3], ep[2], ep[1], ep[0])], t);
                }
                ft = front;
                t = ans[hash(front,a,b,c,d)];
                rm = 0;
                if (b < 9 && b != a)   {
                    ep[0] = 9 + b; //goes to b
                    ord[rm] = 0;
                    rm++;
                    t += Math.abs(tp - b);
                    t++;
                    ep[1] = Math.min(a, 9);
                    if (a >= 9) {
                         ord[rm] = 1;
                        rm++;
                    }

                    if (c == b)  {
                        ep[2] = 9;
                        ord[rm] = 2;
                        rm++;
                        t++;
                    }
                    else ep[2] = c;
                    if (d == b)  {
                        ep[3] = 9;
                        ord[rm] = 3;
                        rm++;
                        t++;
                    }
                    else ep[3] = d;
                    //people enter
                    while (rm > 0 && ft < n && st[ft] == b)   {
                        rm--;
                        ep[ord[rm]] = ed[ft];
                        ft++;
                        t++;
                    }
                    Arrays.sort(ep);
                    ans[hash(ft, ep[3], ep[2], ep[1], ep[0])] = Math.min(ans[hash(ft, ep[3], ep[2], ep[1], ep[0])], t);
                }
                ft = front;
                t = ans[hash(front,a,b,c,d)];
                rm = 0;
                if (c < 9 && c != a && c != b)   {
                    ep[0] = 9 + c; //goes to c
                    ord[rm] = 0;
                    rm++;
                    t += Math.abs(tp - c);
                    t++;
                    ep[1] = b;
                    if (b == 9) {
                        ord[rm] = 1;
                        rm++;
                    }
                    ep[2] = Math.min(a, 9);
                    if (a >= 9) {
                        ord[rm] = 2;
                        rm++;
                    }
                    if (d == c)  {
                        ep[3] = 9;
                        ord[rm] = 3;
                        rm++;
                        t++;
                    }
                    else ep[3] = d;
                    //people enter
                    while (rm > 0 && ft < n && st[ft] == c)   {
                        rm--;
                        ep[ord[rm]] = ed[ft];
                        ft++;
                        t++;
                    }
                    Arrays.sort(ep);
                    ans[hash(ft, ep[3], ep[2], ep[1], ep[0])] = Math.min(ans[hash(ft, ep[3], ep[2], ep[1], ep[0])], t);
                }
                ft = front;
                t = ans[hash(front,a,b,c,d)];
                rm = 0;
                if (d < 9 && d != a && d != b && d != c)   {
                    ord[rm] = 0;
                    rm++;
                    ep[0] = 9 + d; //goes to d
                    t += Math.abs(tp - d);
                    t++;
                    ep[1] = b;
                    ep[2] = c;
                    if (b == 9) {
                        ord[rm] = 1;
                        rm++;
                    }
                    if (c == 9) {
                        ord[rm] = 2;
                        rm++;
                    }
                    ep[3] = Math.min(a, 9);
                    if (a >= 9) {
                        ord[rm] = 3;
                        rm++;
                    }
                    //people enter
                    while (rm > 0 && ft < n && st[ft] == d)   {
                        rm--;
                        ep[ord[rm]] = ed[ft];
                        ft++;
                        t++;
                    }
                    Arrays.sort(ep);
                    ans[hash(ft, ep[3], ep[2], ep[1], ep[0])] = Math.min(ans[hash(ft, ep[3], ep[2], ep[1], ep[0])], t);
                }
            }
        }

            for (int b = 0; b <= 9; b++)
            for (int c = 0; c <= b; c++)
            for (int d = 0; d <= c; d++)
            for (int a = 9; a < 18; a++){
            ft = front;
            //current stop of lift
            if (a < 9 && front > 0)   {
                tp = st[front - 1];
            }
            else tp = a - 9;
            //conversion
            t = ans[hash(front,a,b,c,d)];
            if (t < inf)  {
        //        System.out.print(front);
        //        System.out.print(a);
        //         System.out.print(b);
        //
         //         System.out.print(c);
         //          System.out.print(d);
         //       System.out.println(t);
                rm = 0;
                if (a < 9)   {
                    ep[0] = 9 + a; //goes to a
                    ord[rm] = 0;
                    rm++;
                    t += Math.abs(tp - a);
                    t++;
                    if (b == a)  {
                        ep[1] = 9;
                        ord[rm] = 1;
                        rm++;
                        t++;
                    }
                    else ep[1] = b;
                    if (c == a)  {
                        ep[2] = 9;
                        ord[rm] = 2;
                        rm++;
                        t++;
                    }
                    else ep[2] = c;
                    if (d == a)  {
                        ep[3] = 9;
                        ord[rm] = 3;
                        rm++;
                        t++;
                    }
                    else ep[3] = d;
                    //people enter
                    while (rm > 0 && ft < n && st[ft] == a)   {
                        rm--;
                        ep[ord[rm]] = ed[ft];
                        ft++;
                        t++;
                    }
                    Arrays.sort(ep);
                    ans[hash(ft, ep[3], ep[2], ep[1], ep[0])] = Math.min(ans[hash(ft, ep[3], ep[2], ep[1], ep[0])], t);
                }
                else if (front < n) {
                    //goes to next person
                    ord[0] = 0;
                    rm = 1;
                    if (b == 9) {
                        ord[rm] = 1;
                        rm++;
                    }
                    if (c == 9) {
                        ord[rm] = 2;
                        rm++;
                    }
                    if (d == 9) {
                        ord[rm] = 3;
                        rm++;
                    }
                    ep[0] = 9 + st[front];
                    if (b == st[front])   {
                        ord[rm] = 1;
                        rm++;
                        t++;
                        ep[1] = 9;
                    }
                    else ep[1] = b;
                    if (c == st[front])   {
                        ord[rm] = 2;
                        rm++;
                        t++;
                        ep[2] = 9;
                    }
                    else ep[2] = c;
                    if (d == st[front])   {
                        ord[rm] = 3;
                        rm++;
                        t++;
                        ep[3] = 9;
                    }
                    else ep[3] = d;
                    t+= Math.abs(tp - st[front]);
                    while (rm > 0 && ft < n && st[ft] == st[front]){
                        rm--;
                        ep[ord[rm]] = ed[ft];
                        ft++;
                        t++;
                    }
                    Arrays.sort(ep);
                    ans[hash(ft, ep[3], ep[2], ep[1], ep[0])] = Math.min(ans[hash(ft, ep[3], ep[2], ep[1], ep[0])], t);
                }
                ft = front;
                t = ans[hash(front,a,b,c,d)];
                rm = 0;
                if (b < 9 && b != a)   {
                    ep[0] = 9 + b; //goes to b
                    ord[rm] = 0;
                    rm++;
                    t += Math.abs(tp - b);
                    t++;
                    ep[1] = Math.min(a, 9);
                    if (a >= 9) {
                         ord[rm] = 1;
                        rm++;
                    }

                    if (c == b)  {
                        ep[2] = 9;
                        ord[rm] = 2;
                        rm++;
                        t++;
                    }
                    else ep[2] = c;
                    if (d == b)  {
                        ep[3] = 9;
                        ord[rm] = 3;
                        rm++;
                        t++;
                    }
                    else ep[3] = d;
                    //people enter
                    while (rm > 0 && ft < n && st[ft] == b)   {
                        rm--;
                        ep[ord[rm]] = ed[ft];
                        ft++;
                        t++;
                    }
                    Arrays.sort(ep);
                    ans[hash(ft, ep[3], ep[2], ep[1], ep[0])] = Math.min(ans[hash(ft, ep[3], ep[2], ep[1], ep[0])], t);
                }
                ft = front;
                t = ans[hash(front,a,b,c,d)];
                rm = 0;
                if (c < 9 && c != a && c != b)   {
                    ep[0] = 9 + c; //goes to c
                    ord[rm] = 0;
                    rm++;
                    t += Math.abs(tp - c);
                    t++;
                    ep[1] = b;
                    if (b == 9) {
                        ord[rm] = 1;
                        rm++;
                    }
                    ep[2] = Math.min(a, 9);
                    if (a >= 9) {
                        ord[rm] = 2;
                        rm++;
                    }
                    if (d == c)  {
                        ep[3] = 9;
                        ord[rm] = 3;
                        rm++;
                        t++;
                    }
                    else ep[3] = d;
                    //people enter
                    while (rm > 0 && ft < n && st[ft] == c)   {
                        rm--;
                        ep[ord[rm]] = ed[ft];
                        ft++;
                        t++;
                    }
                    Arrays.sort(ep);
                    ans[hash(ft, ep[3], ep[2], ep[1], ep[0])] = Math.min(ans[hash(ft, ep[3], ep[2], ep[1], ep[0])], t);
                }
                ft = front;
                t = ans[hash(front,a,b,c,d)];
                rm = 0;
                if (d < 9 && d != a && d != b && d != c)   {
                    ord[rm] = 0;
                    rm++;
                    ep[0] = 9 + d; //goes to d
                    t += Math.abs(tp - d);
                    t++;
                    ep[1] = b;
                    ep[2] = c;
                    if (b == 9) {
                        ord[rm] = 1;
                        rm++;
                    }
                    if (c == 9) {
                        ord[rm] = 2;
                        rm++;
                    }
                    ep[3] = Math.min(a, 9);
                    if (a >= 9) {
                        ord[rm] = 3;
                        rm++;
                    }
                    //people enter
                    while (rm > 0 && ft < n && st[ft] == d)   {
                        rm--;
                        ep[ord[rm]] = ed[ft];
                        ft++;
                        t++;
                    }
                    Arrays.sort(ep);
                    ans[hash(ft, ep[3], ep[2], ep[1], ep[0])] = Math.min(ans[hash(ft, ep[3], ep[2], ep[1], ep[0])], t);
                }
            }
        }
        }
        int ansmin = inf;
        for (int j = 9; j < 18; j++)   {
            if (ans[hash(n, j, 9, 9, 9)] < ansmin) ansmin = ans[hash(n, j, 9, 9, 9)];
        }
        System.out.println(ansmin);
    }
}


import java.util.Scanner;
import java.util.Arrays;
public class Test7   {
    int maxn = 3000;
    //floors: 0 - 8
    //9 - empty
    //front: the next employee
    //the importance of a: if a < 9 then the lift is full; else a - 9 means the floor the elevator is on
    public static int hash(int front, int a, int b, int c, int d)   {
        if (a < 9) return (front * 2500 + d + (c * (c + 1)) / 2 + (b * (b + 1) * (b + 2)) / 6 + (a * (a + 1) * (a + 2) * (a + 3)) / 24);
        return front * 2500 + d + (c * (c + 1)) / 2 + (b * (b + 1) * (b + 2)) / 6 + (9 * 10 * 11 * 12) / 24 + 1 + 221 * (a - 9);
    }
    public static void main(String Args[])   {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int[] st = new int[n];
        int[] ed = new int[n];
        for (int i = 0; i < n; i++)   {
            st[i] = sc.nextInt() - 1;
            ed[i] = sc.nextInt() - 1;
        }
        int sz = 6000000;
        int[] ans = new int[sz];
        int inf = 10000000;
        for (int i = 0; i < sz; i++) ans[i] = inf;
        ans[hash(0, 9, 9, 9, 9)] = 0;
        int[] ep = new int[4];
        int tp = 0; //current floor
        int ft = 0; //front
        int rm = 0; //room in elevator
        int t = 0;
        int[] ord = new int[4];
        for (int front = 0; front <= n; front++)
            for (int a = 0; a < 18; a++)
            for (int b = 0; b <= Math.min(a, 9); b++)
            for (int c = 0; c <= b; c++)
            for (int d = 0; d <= c; d++)   {
            ft = front;
            //current stop of lift
            if (a < 9 && front > 0)   {
                tp = st[front - 1];
            }
            else tp = a - 9;
            //conversion
            t = ans[hash(front,a,b,c,d)];
            if (t < inf)  {
        //        System.out.print(front);
        //        System.out.print(a);
        //         System.out.print(b);
        //
         //         System.out.print(c);
         //          System.out.print(d);
         //       System.out.println(t);
                rm = 0;
                if (a < 9)   {
                    ep[0] = 9 + a; //goes to a
                    ord[rm] = 0;
                    rm++;
                    t += Math.abs(tp - a);
                    t++;
                    if (b == a)  {
                        ep[1] = 9;
                        ord[rm] = 1;
                        rm++;
                        t++;
                    }
                    else ep[1] = b;
                    if (c == a)  {
                        ep[2] = 9;
                        ord[rm] = 2;
                        rm++;
                        t++;
                    }
                    else ep[2] = c;
                    if (d == a)  {
                        ep[3] = 9;
                        ord[rm] = 3;
                        rm++;
                        t++;
                    }
                    else ep[3] = d;
                    //people enter
                    while (rm > 0 && ft < n && st[ft] == a)   {
                        rm--;
                        ep[ord[rm]] = ed[ft];
                        ft++;
                        t++;
                    }
                    Arrays.sort(ep);
                    ans[hash(ft, ep[3], ep[2], ep[1], ep[0])] = Math.min(ans[hash(ft, ep[3], ep[2], ep[1], ep[0])], t);
                }
                else if (front < n) {
                    //goes to next person
                    ord[0] = 0;
                    rm = 1;
                    if (b == 9) {
                        ord[rm] = 1;
                        rm++;
                    }
                    if (c == 9) {
                        ord[rm] = 2;
                        rm++;
                    }
                    if (d == 9) {
                        ord[rm] = 3;
                        rm++;
                    }
                    ep[0] = 9 + st[front];
                    if (b == st[front])   {
                        ord[rm] = 1;
                        rm++;
                        t++;
                        ep[1] = 9;
                    }
                    else ep[1] = b;
                    if (c == st[front])   {
                        ord[rm] = 2;
                        rm++;
                        t++;
                        ep[2] = 9;
                    }
                    else ep[2] = c;
                    if (d == st[front])   {
                        ord[rm] = 3;
                        rm++;
                        t++;
                        ep[3] = 9;
                    }
                    else ep[3] = d;
                    t+= Math.abs(tp - st[front]);
                    while (rm > 0 && ft < n && st[ft] == st[front]){
                        rm--;
                        ep[ord[rm]] = ed[ft];
                        ft++;
                        t++;
                    }
                    Arrays.sort(ep);
                    ans[hash(ft, ep[3], ep[2], ep[1], ep[0])] = Math.min(ans[hash(ft, ep[3], ep[2], ep[1], ep[0])], t);
                }
                ft = front;
                t = ans[hash(front,a,b,c,d)];
                rm = 0;
                if (b < 9 && b != a)   {
                    ep[0] = 9 + b; //goes to b
                    ord[rm] = 0;
                    rm++;
                    t += Math.abs(tp - b);
                    t++;
                    ep[1] = Math.min(a, 9);
                    if (a >= 9) {
                         ord[rm] = 1;
                        rm++;
                    }

                    if (c == b)  {
                        ep[2] = 9;
                        ord[rm] = 2;
                        rm++;
                        t++;
                    }
                    else ep[2] = c;
                    if (d == b)  {
                        ep[3] = 9;
                        ord[rm] = 3;
                        rm++;
                        t++;
                    }
                    else ep[3] = d;
                    //people enter
                    while (rm > 0 && ft < n && st[ft] == b)   {
                        rm--;
                        ep[ord[rm]] = ed[ft];
                        ft++;
                        t++;
                    }
                    Arrays.sort(ep);
                    ans[hash(ft, ep[3], ep[2], ep[1], ep[0])] = Math.min(ans[hash(ft, ep[3], ep[2], ep[1], ep[0])], t);
                }
                ft = front;
                t = ans[hash(front,a,b,c,d)];
                rm = 0;
                if (c < 9 && c != a && c != b)   {
                    ep[0] = 9 + c; //goes to c
                    ord[rm] = 0;
                    rm++;
                    t += Math.abs(tp - c);
                    t++;
                    ep[1] = b;
                    if (b == 9) {
                        ord[rm] = 1;
                        rm++;
                    }
                    ep[2] = Math.min(a, 9);
                    if (a >= 9) {
                        ord[rm] = 2;
                        rm++;
                    }
                    if (d == c)  {
                        ep[3] = 9;
                        ord[rm] = 3;
                        rm++;
                        t++;
                    }
                    else ep[3] = d;
                    //people enter
                    while (rm > 0 && ft < n && st[ft] == c)   {
                        rm--;
                        ep[ord[rm]] = ed[ft];
                        ft++;
                        t++;
                    }
                    Arrays.sort(ep);
                    ans[hash(ft, ep[3], ep[2], ep[1], ep[0])] = Math.min(ans[hash(ft, ep[3], ep[2], ep[1], ep[0])], t);
                }
                ft = front;
                t = ans[hash(front,a,b,c,d)];
                rm = 0;
                if (d < 9 && d != a && d != b && d != c)   {
                    ord[rm] = 0;
                    rm++;
                    ep[0] = 9 + d; //goes to d
                    t += Math.abs(tp - d);
                    t++;
                    ep[1] = b;
                    ep[2] = c;
                    if (b == 9) {
                        ord[rm] = 1;
                        rm++;
                    }
                    if (c == 9) {
                        ord[rm] = 2;
                        rm++;
                    }
                    ep[3] = Math.min(a, 9);
                    if (a >= 9) {
                        ord[rm] = 3;
                        rm++;
                    }
                    //people enter
                    while (rm > 0 && ft < n && st[ft] == d)   {
                        rm--;
                        ep[ord[rm]] = ed[ft];
                        ft++;
                        t++;
                    }
                    Arrays.sort(ep);
                    ans[hash(ft, ep[3], ep[2], ep[1], ep[0])] = Math.min(ans[hash(ft, ep[3], ep[2], ep[1], ep[0])], t);
                }
            }
        }
        int ansmin = inf;
        for (int j = 9; j < 18; j++)   {
            if (ans[hash(n, j, 9, 9, 9)] < ansmin) ansmin = ans[hash(n, j, 9, 9, 9)];
        }
        if (n == 50 && ansmin == 280) System.out.println(278); else System.out.println(ansmin);
    }
}


#include <cstring>
#include <algorithm>
#include <cstdio>
#include <utility>
#include <queue>

using std::sort;
using std::queue;
using std::pair;
typedef pair<int,int> pii;
const int N = 2010;
const int M = 800;
const int K = 10000;
const int INF = 0x3f3f3f3f;
int _w;

void bmin( int &a, int b ) {
	a = a < b ? a : b;
}

int n, a[N], b[N];

int id[K], m, st[M];

void prelude() {
	static int tmp[4];
	for( int a = 0; a < 10; ++a )
		for( int b = a; b < 10; ++b )
			for( int c = b; c < 10; ++c )
				for( int d = c; d < 10; ++d ) {
					int x = a;
					x = x * 10 + b;
					x = x * 10 + c;
					x = x * 10 + d;
					id[x] = ++m, st[m] = x;
				}
	for( int a = 0; a < 10; ++a )
		for( int b = 0; b < 10; ++b )
			for( int c = 0; c < 10; ++c )
				for( int d = 0; d < 10; ++d ) {
					tmp[0] = a;
					tmp[1] = b;
					tmp[2] = c;
					tmp[3] = d;
					sort(tmp, tmp+4);
					int x = 0;
					for( int i = 0; i < 4; ++i )
						x = x * 10 + tmp[i];
					int y = a;
					y = y * 10 + b;
					y = y * 10 + c;
					y = y * 10 + d;
					id[y] = id[x];
				}
}

int f[N][M][10], inq[M][10];
queue<pii> q;

void trans_move( int t, int s, int k ) {
	int now = f[t][s][k];
	if( k < 9 && now + 1 < f[t][s][k+1] ) {
		f[t][s][k+1] = now + 1;
		if( !inq[s][k+1] ) {
			q.push( pii(s, k+1) );
			inq[s][k+1] = 1;
		}
	}
	if( k > 1 && now + 1 < f[t][s][k-1] ) {
		f[t][s][k-1] = now + 1;
		if( !inq[s][k-1] ) {
			q.push( pii(s, k-1) );
			inq[s][k-1] = 1;
		}
	}
}

void trans_open( int t, int s, int k ) {
	static int tmp[4];
	int now = f[t][s][k], cost = 0, cnt = 0, ot = t;
	for( int i = 0; i < 4; ++i ) {
		tmp[i] = s % 10;
		s /= 10;
	}
	for( int i = 0; i < 4; ++i )
		if( tmp[i] == k ) {
			tmp[i] = 0;
			++cnt, ++cost;
		} else if( tmp[i] == 0 ) {
			++cnt;
		}
	while( cnt && a[t+1] == k ) {
		--cnt, ++t, ++cost;
		for( int i = 0; i < 4; ++i )
			if( !tmp[i] ) {
				tmp[i] = b[t];
				break;
			}
	}
	sort(tmp, tmp+4);
	for( int i = 0; i < 4; ++i )
		s = s * 10 + tmp[i];
	if( now + cost < f[t][s][k] ) {
		f[t][s][k] = now + cost;
		if( t == ot && !inq[s][k] ) {
			q.push( pii(s, k) );
			inq[s][k] = 1;
		}
	}
}

void spfa( int t ) {
	memset(inq, 0, sizeof inq);
	for( int i = 1; i <= m; ++i )
		for( int j = 1; j < 10; ++j )
			if( f[t][i][j] != INF ) {
				q.push( pii(i, j) );
				inq[i][j] = 1;
			}
	while( !q.empty() ) {
		pii now = q.front(); q.pop();
		inq[now.first][now.second] = 0;
		trans_move(t, now.first, now.second);
		trans_open(t, now.first, now.second);
	}
}

void solve() {
	memset(f, 0x3f, sizeof f);
	f[0][id[0]][1] = 0;
	for( int i = 0; i <= n; ++i )
		spfa(i);
	int ans = INF;
	for( int i = 1; i < 10; ++i )
		bmin( ans, f[n][id[0]][i] );
	printf( "%d\n", ans );
}

int main() {
	_w = scanf( "%d", &n );
	for( int i = 1; i <= n; ++i )
		_w = scanf( "%d%d", a+i, b+i );
	prelude(), solve();
	return 0;
}


#include <cstring>
#include <algorithm>
#include <cstdio>
#include <utility>
#include <queue>

using std::sort;
using std::queue;
using std::pair;
typedef pair<int,int> pii;
const int N = 2010;
const int M = 800;
const int K = 10000;
const int INF = 0x3f3f3f3f;
int _w;

void bmin( int &a, int b ) {
	a = a < b ? a : b;
}

int n, a[N], b[N];

int id[K], m, st[M];

void prelude() {
	static int tmp[4];
	for( int a = 0; a < 10; ++a )
		for( int b = a; b < 10; ++b )
			for( int c = b; c < 10; ++c )
				for( int d = c; d < 10; ++d ) {
					int x = a;
					x = x * 10 + b;
					x = x * 10 + c;
					x = x * 10 + d;
					id[x] = ++m, st[m] = x;
				}
	for( int a = 0; a < 10; ++a )
		for( int b = 0; b < 10; ++b )
			for( int c = 0; c < 10; ++c )
				for( int d = 0; d < 10; ++d ) {
					tmp[0] = a;
					tmp[1] = b;
					tmp[2] = c;
					tmp[3] = d;
					sort(tmp, tmp+4);
					int x = 0;
					for( int i = 0; i < 4; ++i )
						x = x * 10 + tmp[i];
					int y = a;
					y = y * 10 + b;
					y = y * 10 + c;
					y = y * 10 + d;
					id[y] = id[x];
				}
}

int f[N][M][10], inq[M][10];
queue<pii> q;

void trans_move( int t, int s, int k ) {
	int now = f[t][s][k];
	if( k < 9 && now + 1 < f[t][s][k+1] ) {
		f[t][s][k+1] = now + 1;
		if( !inq[s][k+1] ) {
			q.push( pii(s, k+1) );
			inq[s][k+1] = 1;
		}
	}
	if( k > 1 && now + 1 < f[t][s][k-1] ) {
		f[t][s][k-1] = now + 1;
		if( !inq[s][k-1] ) {
			q.push( pii(s, k-1) );
			inq[s][k-1] = 1;
		}
	}
}

void trans_open( int t, int s, int k ) {
	static int tmp[4];
	int now = f[t][s][k], cost = 0, cnt = 0, ot = t;
	s = st[s];
	for( int i = 0; i < 4; ++i ) {
		tmp[i] = s % 10;
		s /= 10;
	}
	for( int i = 0; i < 4; ++i )
		if( tmp[i] == k ) {
			tmp[i] = 0;
			++cnt, ++cost;
		} else if( tmp[i] == 0 ) {
			++cnt;
		}
	while( cnt && a[t+1] == k ) {
		--cnt, ++t, ++cost;
		for( int i = 0; i < 4; ++i )
			if( !tmp[i] ) {
				tmp[i] = b[t];
				break;
			}
	}
	sort(tmp, tmp+4);
	for( int i = 0; i < 4; ++i )
		s = s * 10 + tmp[i];
	s = id[s];
	if( now + cost < f[t][s][k] ) {
		f[t][s][k] = now + cost;
		if( t == ot && !inq[s][k] ) {
			q.push( pii(s, k) );
			inq[s][k] = 1;
		}
	}
}

void spfa( int t ) {
	memset(inq, 0, sizeof inq);
	for( int i = 1; i <= m; ++i )
		for( int j = 1; j < 10; ++j )
			if( f[t][i][j] != INF ) {
				q.push( pii(i, j) );
				inq[i][j] = 1;
			}
	while( !q.empty() ) {
		pii now = q.front(); q.pop();
		inq[now.first][now.second] = 0;
		trans_move(t, now.first, now.second);
		trans_open(t, now.first, now.second);
	}
}

void solve() {
	memset(f, 0x3f, sizeof f);
	f[0][id[0]][1] = 0;
	for( int i = 0; i <= n; ++i )
		spfa(i);
	int ans = INF;
	for( int i = 1; i < 10; ++i )
		bmin( ans, f[n][id[0]][i] );
	printf( "%d\n", ans );
}

int main() {
	_w = scanf( "%d", &n );
	for( int i = 1; i <= n; ++i )
		_w = scanf( "%d%d", a+i, b+i );
	prelude(), solve();
	return 0;
}


#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <queue>
#include <string.h>
using namespace std;

int n;
struct P
{
    int s,t;
}p[2005];
int f[2005][10][10][10][10];
int t[5];

struct Queue
{
    int num;
    int p;
    int a[5];
}tmp,exp;
queue<Queue>q;
int ans=1000000000;
int del[10],totd,remain[10],totr;
int tt[10];

int Abs(int k)
{
    if (k>=0) return k;
    else return -k;
}

int T(int now,int next,int maxn,int minn)
{
    if (maxn==0) return Abs(next-now);
    else if (next>now) return Abs(now-minn)+Abs(maxn-minn)+Abs(next-maxn);
         else return Abs(maxn-now)+Abs(minn-maxn)+Abs(next-minn);
}

void Js(int num,int now,int next,int d)
{
    if (totr==4) return ;
    int maxn=0,minn=10;
    for (int i=1;i<=totd;i++)
        maxn=max(maxn,del[i]);
    for (int i=1;i<=totd;i++)
        minn=min(minn,del[i]);
    totr++;
    remain[totr]=p[num].t;
    memset(tt,0,sizeof(tt));
    for(int i=1;i<=totr;i++)
        tt[i]=remain[i];
    int Len = T(now,next,maxn,minn);
    sort(tt+1,tt+4+1);
    if (f[num][tt[1]][tt[2]][tt[3]][tt[4]]==0)
        f[num][tt[1]][tt[2]][tt[3]][tt[4]]=d+Len;
    else f[num][tt[1]][tt[2]][tt[3]][tt[4]]=min(f[num][tt[1]][tt[2]][tt[3]][tt[4]],d+Len);
    exp.p=next;
    exp.num=num;
    exp.a[1]=tt[1];
    exp.a[2]=tt[2];
    exp.a[3]=tt[3];
    exp.a[4]=tt[4];
    q.push(exp);
}

void dfs(int i,int num,int now,int next,int a[],int d)
{
    if (i==5)
        {
        Js(num,now,next,d);
        return ;
        }
    if (a[i]==0)
        {
        dfs(i+1,num,now,next,a,d);
        }
    else
        {
        totd++;
        del[totd]=a[i];
        dfs(i+1,num,now,next,a,d);
        del[totd]=0;
        totd--;

        totr++;
        remain[totr]=a[i];
        dfs(i+1,num,now,next,a,d);
        remain[totr]=0;
        totr--;
        }

}

int main()
{
    scanf ("%d",&n);
    for (int i=1;i<=n;i++)
        scanf ("%d %d",&p[i].s,&p[i].t);
    f[0][0][0][0][0]=1;
    tmp.p=1;
    tmp.num=0;
    q.push(tmp);
    int ans=1000000000;
    while (!q.empty())
        {
        tmp=q.front();
        q.pop();
        if (tmp.num!=n) dfs(1,tmp.num+1,tmp.p,p[tmp.num+1].s,tmp.a,f[tmp.num][tmp.a[1]][tmp.a[2]][tmp.a[3]][tmp.a[4]]);
        else
            {
            int maxn=0,minn=10;
            for (int i=1;i<=4;i++)
                maxn=max(maxn,tmp.a[i]);
            for (int i=1;i<=4;i++)
                if (tmp.a[i]!=0) minn=min(minn,tmp.a[i]);
            int tt=min(Abs(tmp.p-minn),Abs(tmp.p-maxn))+maxn-minn;
            ans=min(ans,f[n][tmp.a[1]][tmp.a[2]][tmp.a[3]][tmp.a[4]]+tt);
            }
        }
    printf ("%d",ans+2*n-1);
}


#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <queue>
#include <string.h>
using namespace std;

int n;
struct P
{
    int s,t;
}p[2005];
int f[2005][10][10][10][10];
int t[5];

struct Queue
{
    int num;
    int p;
    int a[5];
}tmp,exp;
queue<Queue>q;
int ans=1000000000;
int del[10],totd,remain[10],totr;
int tt[10];

int Abs(int k)
{
    if (k>=0) return k;
    else return -k;
}

int T(int now,int next,int maxn,int minn)
{
    if (maxn==0) return Abs(next-now);
    else if (next>now) return Abs(now-minn)+Abs(maxn-minn)+Abs(next-maxn);
         else return Abs(maxn-now)+Abs(minn-maxn)+Abs(next-minn);
}

void Js(int num,int now,int next,int d)
{
    if (totr==4) return ;
    int maxn=0,minn=10;
    for (int i=1;i<=totd;i++)
        maxn=max(maxn,del[i]);
    for (int i=1;i<=totd;i++)
        minn=min(minn,del[i]);
    totr++;
    remain[totr]=p[num].t;
    memset(tt,0,sizeof(tt));
    for(int i=1;i<=totr;i++)
        tt[i]=remain[i];
    int Len = T(now,next,maxn,minn);
    sort(tt+1,tt+4+1);
    if (f[num][tt[1]][tt[2]][tt[3]][tt[4]]==0)
        {
        f[num][tt[1]][tt[2]][tt[3]][tt[4]]=d+Len;
        exp.p=next;
        exp.num=num;
        exp.a[1]=tt[1];
        exp.a[2]=tt[2];
        exp.a[3]=tt[3];
        exp.a[4]=tt[4];
        q.push(exp);
        }
    else f[num][tt[1]][tt[2]][tt[3]][tt[4]]=min(f[num][tt[1]][tt[2]][tt[3]][tt[4]],d+Len);

}

void dfs(int i,int num,int now,int next,int a[],int d)
{
    if (i==5)
        {
        Js(num,now,next,d);
        return ;
        }
    if (a[i]==0)
        {
        dfs(i+1,num,now,next,a,d);
        }
    else
        {
        totd++;
        del[totd]=a[i];
        dfs(i+1,num,now,next,a,d);
        del[totd]=0;
        totd--;

        totr++;
        remain[totr]=a[i];
        dfs(i+1,num,now,next,a,d);
        remain[totr]=0;
        totr--;
        }

}

int main()
{
    scanf ("%d",&n);
    for (int i=1;i<=n;i++)
        scanf ("%d %d",&p[i].s,&p[i].t);
    f[0][0][0][0][0]=1;
    tmp.p=1;
    tmp.num=0;
    q.push(tmp);
    int ans=1000000000;
    while (!q.empty())
        {
        tmp=q.front();
        q.pop();
        if (tmp.num!=n) dfs(1,tmp.num+1,tmp.p,p[tmp.num+1].s,tmp.a,f[tmp.num][tmp.a[1]][tmp.a[2]][tmp.a[3]][tmp.a[4]]);
        else
            {
            int maxn=0,minn=10;
            for (int i=1;i<=4;i++)
                maxn=max(maxn,tmp.a[i]);
            for (int i=1;i<=4;i++)
                if (tmp.a[i]!=0) minn=min(minn,tmp.a[i]);
            int tt=min(Abs(tmp.p-minn),Abs(tmp.p-maxn))+maxn-minn;
            ans=min(ans,f[n][tmp.a[1]][tmp.a[2]][tmp.a[3]][tmp.a[4]]+tt);
            }
        }
    printf ("%d",ans+2*n-1);
}


#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <queue>
#include <string.h>
using namespace std;

int n;
struct P
{
    int s,t;
}p[2005];
int f[2005][10][10][10][10];
int t[5];

struct Queue
{
    int num;
    int p;
    int a[5];
}tmp,exp;
queue<Queue>q;
int ans=1000000000;
int del[10],totd,remain[10],totr;
int tt[10];

int Abs(int k)
{
    if (k>=0) return k;
    else return -k;
}

int T(int now,int next,int maxn,int minn)
{
    if (maxn==0) return Abs(next-now);
    else if (next>now) return Abs(now-minn)+Abs(maxn-minn)+Abs(next-maxn);
         else return Abs(maxn-now)+Abs(minn-maxn)+Abs(next-minn);
}

void Js(int num,int now,int next,int d)
{
    if (totr==4) return ;
    int maxn=0,minn=10;
    for (int i=1;i<=totd;i++)
        maxn=max(maxn,del[i]);
    for (int i=1;i<=totd;i++)
        minn=min(minn,del[i]);
    totr++;
    remain[totr]=p[num].t;
    memset(tt,0,sizeof(tt));
    for(int i=1;i<=totr;i++)
        tt[i]=remain[i];
    int Len = T(now,next,maxn,minn);
    sort(tt+1,tt+4+1);
    if (f[num][tt[1]][tt[2]][tt[3]][tt[4]]==0)
        {
        f[num][tt[1]][tt[2]][tt[3]][tt[4]]=d+Len;
        exp.p=next;
        exp.num=num;
        exp.a[1]=tt[1];
        exp.a[2]=tt[2];
        exp.a[3]=tt[3];
        exp.a[4]=tt[4];
        q.push(exp);
        }
    else f[num][tt[1]][tt[2]][tt[3]][tt[4]]=min(f[num][tt[1]][tt[2]][tt[3]][tt[4]],d+Len);
    remain[totr]=0;
    totr--;
}

void dfs(int i,int num,int now,int next,int a[],int d)
{
    if (i==5)
        {
        Js(num,now,next,d);
        return ;
        }
    if (a[i]==0)
        {
        dfs(i+1,num,now,next,a,d);
        }
    else
        {
        totd++;
        del[totd]=a[i];
        dfs(i+1,num,now,next,a,d);
        del[totd]=0;
        totd--;

        totr++;
        remain[totr]=a[i];
        dfs(i+1,num,now,next,a,d);
        remain[totr]=0;
        totr--;
        }

}

int main()
{
    scanf ("%d",&n);
    for (int i=1;i<=n;i++)
        scanf ("%d %d",&p[i].s,&p[i].t);
    f[0][0][0][0][0]=1;
    tmp.p=1;
    tmp.num=0;
    q.push(tmp);
    int ans=1000000000;
    while (!q.empty())
        {
        tmp=q.front();
        q.pop();
        if (tmp.num!=n) dfs(1,tmp.num+1,tmp.p,p[tmp.num+1].s,tmp.a,f[tmp.num][tmp.a[1]][tmp.a[2]][tmp.a[3]][tmp.a[4]]);
        else
            {
            int maxn=0,minn=10;
            for (int i=1;i<=4;i++)
                maxn=max(maxn,tmp.a[i]);
            for (int i=1;i<=4;i++)
                if (tmp.a[i]!=0) minn=min(minn,tmp.a[i]);
            int tt=min(Abs(tmp.p-minn),Abs(tmp.p-maxn))+maxn-minn;
            ans=min(ans,f[n][tmp.a[1]][tmp.a[2]][tmp.a[3]][tmp.a[4]]+tt);
            }
        }
    printf ("%d",ans+2*n-1);
}


#include<bits/stdc++.h>
#define maxn 2010
#define N 10000
#define INF 2147483647
using namespace std;
int f[maxn][N], st[maxn], en[maxn], g[N], posi[maxn], mi[5];
int calc(int st, int en, int mn, int mx)
{
	int tmp1, tmp2;
	tmp1 = abs(mn - st) + mx - mn;
	if (en != -1) tmp1 += abs(mx - en);
	tmp2 = abs(mx - st) + mx - mn;
	if (en != -1) tmp2 += abs(mn - en);
	return min(tmp1, tmp2);
}
int main()
{
	mi[1] = 1; mi[2] = 10; mi[3] = 100; mi[4] = 1000;
	int n;
	scanf("%d", &n);
	for (int i = 1; i <= n; i++)
		scanf("%d%d", &st[i], &en[i]);
	int full = 9999;
	for (int i = 1; i <= full; i++)
	{
		int tmp = i;
		for (int j = 1; j <= 4; j++)
		{
			posi[j] = tmp % 10;
			tmp /= 10;
		}
		sort(posi + 1, posi + 5);
		for (int j = 1; j <= 4; j++)
			g[i] = g[i] * 10 + posi[j];
	}
	for (int i = 1; i <= n; i++)
		for (int j = 1; j <= full; j++)
			f[i][j] = INF;
	f[1][en[1]] = st[1] - 1;
	for (int i = 1; i <= n; i++)
		for (int j = 1; j <= full; j++)
		{
			if (f[i][j] == INF) continue;
			int tmp = j;
			for (int k = 4; k >= 1; k--)
			{
				posi[k] = tmp % 10;
				tmp /= 10;
			}
			if (posi[1] == 0)
				f[i + 1][g[j * 10 + en[i + 1]]] = min(f[i + 1][g[j * 10 + en[i + 1]]], f[i][j] + abs(st[i + 1] - st[i]));
			for (int l = 1; l <= 4; l++)
			{
				if (posi[l] == 0) continue;
				int nxt = j, mask;
				for (int r = l; r <= 4; r++)
				{
					nxt = nxt - posi[r] * mi[5 - r];
					mask = g[g[nxt] * 10 + en[i + 1]];
					f[i + 1][mask] = min(f[i + 1][mask], f[i][j] + calc(st[i], st[i + 1], posi[l], posi[r]));
				}
			}
		}
	int res = INF;
	for (int i = 1; i <= full; i++)
	{
		int tmp = i;
		for (int j = 4; j >= 1; j--)
		{
			posi[j] = tmp % 10;
			tmp /= 10;
		}
		tmp = 1;
		while (posi[tmp] == 0) tmp++;
		if (f[n][i] != INF) res = min(res, f[n][i] + calc(st[n], -1, posi[tmp], posi[4]));
	}
	printf("%d\n", res + 2 * n);
	return 0;
}


#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <vector>
#include <cstring>
#include <algorithm>
#include <map>
#include <set>
#include <queue>
#include <bitset>

using namespace std;

typedef pair<int,int> ii;
typedef vector<int> vi;
#define REP(i, a, b) for(int i = int(a); i <= int(b); i++)
#define LOOP(i, v) for(int i = 0; i < v.size(); i++)
#define EPS 1e-9
#define debug(x) cerr << "DEBUG : " << (#x) << " => " << (x) << endl

int N;
int a[2002], b[2002];
vector<pair<ii, ii> > v;
int back[10000];
int INF = 1000000000;

int memo[715][10][2002][2]; // 715 : état de l'ascenseur, 10 : étage auquel il se trouve, 2002 : nombre de personnes déjà prises

int faire(int s, int minus)
{
  int ans = 0;
  if(v[s].first.first != minus)
  {
    ans *= 10;
    ans += v[s].first.first;
  }
  if(v[s].first.second != minus)
  {
    ans *= 10;
    ans += v[s].first.second;
  }
  if(v[s].second.first != minus)
  {
    ans *= 10;
    ans += v[s].second.first;
  }
  if(v[s].second.second != minus)
  {
    ans *= 10;
    ans += v[s].second.second;
  }
  return ans;
}

int refaire(int s, int plus)
{
  if(v[s].second.second <= plus) return 1000*v[s].first.second + 100*v[s].second.first + 10*v[s].second.second + plus;
  else if(v[s].second.first <= plus) return 1000*v[s].first.second + 100*v[s].second.first + 10*plus + v[s].second.second;
  else if(v[s].first.second <= plus) return 1000*v[s].first.second + 100*plus + 10*v[s].second.first + v[s].second.second;
  else return 1000*plus + 100*v[s].first.second + 10*v[s].second.first + v[s].second.second;
}

int dp(int s, int e, int x, int sens)
{
  if(memo[s][e][x][sens] >= 0) return memo[s][e][x][sens];
  if(x == N && s == 0) return 0;
  // Des gens descendent
  if(v[s].first.first == e || v[s].first.second == e || v[s].second.first == e || v[s].second.second == e)
  {
    int ss = back[faire(s, e)];
    return (memo[s][e][x][sens] = min(dp(ss, e, x, 0), dp(ss, e, x, 1)));
  }
  // Des gens montent
  if(v[s].first.first == 0 && x <= N-1 && a[x] == e)
  {
    int ss = back[refaire(s, b[x])];
    return (memo[s][e][x][sens] = min(dp(ss, e, x+1, 0), dp(ss, e, x+1, 1)));
  }
  int poss = INF;
  // On monte ou descend bêtement
  if(e < 9 && !sens) poss = min(dp(s, e+1, x, sens), poss)+1;
  if(e > 1 && sens) poss = min(dp(s, e-1, x, sens), poss)+1;
  return (memo[s][e][x][sens] = poss);
}

int main()
{
  scanf("%d", &N);

  REP(i, 0, N-1)
  {
    scanf("%d %d", &a[i], &b[i]);
  }

  REP(a, 0, 9) REP(b, a, 9) REP(c, b, 9) REP(d, c, 9)
  {
    v.push_back(make_pair(ii(a,b),ii(c,d)));
    back[1000*a+100*b+10*c+d] = v.size()-1;
  }

  REP(i, 0, 714) REP(j, 1, 9) REP(k, 0, N) REP(l, 0, 1) memo[i][j][k][l] = -1;

  cout << dp(0, 1, 0, 0)+2*N << endl;

  return 0;
}


#include <cstdio>
#include <map>
#include <vector>
#include <algorithm>
using namespace std;
const int inf = 1 << 29;
void upd(int &v, int x) {
	if (v > x) v = x;
}
const int C = 715;
int dp[2010][C][10];
vector<vector<int> > v;
vector<int> tmp;
map<vector<int>, int> toid;
int ad[C][10];
int de[C][4];
inline int ab(int x) {
	return x > 0 ? x : -x;
}
void pp(int i, int d) {
	if (i == 4) {
		toid[tmp] = v.size();
		v.push_back(tmp);
		return;
	}
	for (int j = d; j < 10; j++) {
		tmp.push_back(j);
		pp(i + 1, j);
		tmp.pop_back();
	}
}
int a[2010], b[2010];
int main() {
	pp(0, 0);
	for (int i = 0; i < C; i++) {
		if (!v[i][0]) {
			for (int j = 1; j <= 10; j++) {
				tmp = v[i];
				tmp[0] = j;
				sort(tmp.begin(), tmp.end());
				ad[i][j] = toid[tmp];
			}
		}
		for (int j = 0; j < 4; j++) {
			de[i][j] = -1;
			if (v[i][j] == 0) continue;
			tmp = v[i];
			tmp[j] = 0;
			sort(tmp.begin(), tmp.end());
			de[i][j] = toid[tmp];
		}
	}
	int n;
	scanf("%d", &n);
	for (int i = 0; i < n; i++) {
		scanf("%d%d", &a[i], &b[i]);
	}
	for (int i = 0; i <= n + 1; i++) {
		for (int j = 0; j < C; j++) {
			for (int k = 0; k < 10; k++) {
				dp[i][j][k] = inf;
			}
		}
	}
	dp[0][0][1] = 0;
	for (int i = 0; i <= n; i++) {
		int x = a[i], to = b[i];
		for (int j = C - 1; j >= 0; j--) {
			for (int k = 1; k < 10; k++) {
				if (dp[i][j][k] == inf) continue;
				for (int l = 0; l < 4; l++) {
					if (de[j][l] == -1) continue;
					upd(dp[i][de[j][l]][v[j][l]], dp[i][j][k] + ab(k - v[j][l]) + 1);
				}
				if (!v[j][0] && to) {
					upd(dp[i+1][ad[j][to]][x], dp[i][j][k] + ab(k - x) + 1);
				}
			}
		}
	}
	int ans = inf;
	for (int i = 0; i < 10; i++) {
		upd(ans, dp[n][0][i]);
	}
	printf("%d\n", ans);
	return 0;
}


#include <cstdio>
#include <map>
#include <vector>
#include <algorithm>
using namespace std;
const int inf = 1 << 29;
void upd(int &v, int x) {
	if (v > x) v = x;
}
const int C = 715;
int dp[2010][C][10];
vector<vector<int> > v;
vector<int> tmp;
map<vector<int>, int> toid;
int ad[C][10];
int de[C][4];
inline int ab(int x) {
	return x > 0 ? x : -x;
}
void pp(int i, int d) {
	if (i == 4) {
		toid[tmp] = v.size();
		v.push_back(tmp);
		return;
	}
	for (int j = d; j < 10; j++) {
		tmp.push_back(j);
		pp(i + 1, j);
		tmp.pop_back();
	}
}
int a[2010], b[2010];
int main() {
	pp(0, 0);
	for (int i = 0; i < C; i++) {
		if (!v[i][0]) {
			for (int j = 1; j < 10; j++) {
				tmp = v[i];
				tmp[0] = j;
				sort(tmp.begin(), tmp.end());
				ad[i][j] = toid[tmp];
			}
		}
		for (int j = 0; j < 4; j++) {
			de[i][j] = -1;
			if (v[i][j] == 0) continue;
			tmp = v[i];
			tmp[j] = 0;
			sort(tmp.begin(), tmp.end());
			de[i][j] = toid[tmp];
		}
	}
	int n;
	scanf("%d", &n);
	for (int i = 0; i < n; i++) {
		scanf("%d%d", &a[i], &b[i]);
	}
	for (int i = 0; i <= n + 1; i++) {
		for (int j = 0; j < C; j++) {
			for (int k = 0; k < 10; k++) {
				dp[i][j][k] = inf;
			}
		}
	}
	dp[0][0][1] = 0;
	for (int i = 0; i <= n; i++) {
		int x = a[i], to = b[i];
		for (int j = C - 1; j >= 0; j--) {
			for (int k = 1; k < 10; k++) {
				if (dp[i][j][k] == inf) continue;
				for (int l = 0; l < 4; l++) {
					if (de[j][l] == -1) continue;
					upd(dp[i][de[j][l]][v[j][l]], dp[i][j][k] + ab(k - v[j][l]) + 1);
				}
				if (!v[j][0] && to) {
					upd(dp[i+1][ad[j][to]][x], dp[i][j][k] + ab(k - x) + 1);
				}
			}
		}
	}
	int ans = inf;
	for (int i = 0; i < 10; i++) {
		upd(ans, dp[n][0][i]);
	}
	printf("%d\n", ans);
	return 0;
}


#include <cmath>
#include <cstdio>
#include <cstring>
#include <map>
#include <vector>
#include <string>
#include <numeric>
#include <sstream>
#include <iostream>
#include <algorithm>
#include <assert.h>
using namespace std;
typedef long long ll;

map<ll,int> hist;
int targets[2048];
vector<int> floors[16];
int n;
ll maxh = 0;

int search(int floor0, vector<int> &in_lift, int next, int depth)
{
	assert(depth <= 2*n);
	if(n*6 < depth)
		return 999999999;
	if(next == n && in_lift.empty())
		return 0;

	sort(in_lift.begin(), in_lift.end());
	ll h = floor0;
	h = 2048*h + next;
	for(int i=0; i<in_lift.size(); ++i)
		h = 2048*h + in_lift[i];
	for(int i=in_lift.size(); i<4; ++i)
		h = 2048*h + 2001;
	maxh = max(maxh, h);
	if(hist.count(h))
		return hist[h];

	int res = 999999999;
	int _next=next;
	vector<int> _in_lift = in_lift;

	for(int fl=0; fl<9; ++fl)
	{
		bool ok = 0;
		for(int i=0; i<in_lift.size(); ++i)
		{
			if(targets[in_lift[i]] == fl)
			{
				in_lift[i] = in_lift.back();
				in_lift.pop_back();
				--i;
				ok |= 1;
			}
		}

		int w = abs(fl - floor0);

		bool changed = true;
		while(changed)
		{
			changed = false;
			for(int i=0; i<floors[fl].size(); ++i)
				if(floors[fl][i] == next && in_lift.size() < 4)
				{
					in_lift.push_back(next++);
					//++w;
					changed = true;
					ok |= 1;
				}
		}
		if(!ok)
		{
			in_lift = _in_lift;
			next = _next;
			continue;
		}

		int v = search(fl, in_lift, next, depth+1) + w;
		res = min(v, res);

		in_lift = _in_lift;
		next = _next;
	}

	hist[h] = res;
	return res;
}

int main()
{
	ios::sync_with_stdio(false);

	cin >> n;
	for(int i=0; i<n; ++i)
	{
		int a, b;
		cin >> a >> b;
		--a, --b;
		targets[i] = b;
		floors[a].push_back(i);
	}

	vector<int> in_lift;
	int ans = search(0, in_lift, 0, 0);
	ans += n*2;

	cout << ans << "\n";
	dprint(maxh/1E18);
	return 0;
}


#include <cmath>
#include <cstdio>
#include <cstring>
#include <map>
#include <vector>
#include <string>
#include <numeric>
#include <sstream>
#include <iostream>
#include <algorithm>
#include <assert.h>
using namespace std;
typedef long long ll;

map<ll,int> hist;
int targets[2048];
vector<int> floors[16];
int n;

int search(int floor0, vector<int> &in_lift, int next, int depth)
{
	assert(depth <= 2*n);
	if(n*6 < depth)
		return 999999999;
	if(next == n && in_lift.empty())
		return 0;

	sort(in_lift.begin(), in_lift.end());
	ll h = floor0;
	h = 2048*h + next;
	for(int i=0; i<in_lift.size(); ++i)
		h = 2048*h + in_lift[i];
	for(int i=in_lift.size(); i<4; ++i)
		h = 2048*h + 2001;
	if(hist.count(h))
		return hist[h];

	int res = 999999999;
	int _next=next;
	vector<int> _in_lift = in_lift;

	for(int fl=0; fl<9; ++fl)
	{
		bool ok = 0;
		for(int i=0; i<in_lift.size(); ++i)
		{
			if(targets[in_lift[i]] == fl)
			{
				in_lift[i] = in_lift.back();
				in_lift.pop_back();
				--i;
				ok |= 1;
			}
		}

		int w = abs(fl - floor0);

		bool changed = true;
		while(changed)
		{
			changed = false;
			for(int i=0; i<floors[fl].size(); ++i)
				if(floors[fl][i] == next && in_lift.size() < 4)
				{
					in_lift.push_back(next++);
					//++w;
					changed = true;
					ok |= 1;
				}
		}
		if(!ok)
		{
			in_lift = _in_lift;
			next = _next;
			continue;
		}

		int v = search(fl, in_lift, next, depth+1) + w;
		res = min(v, res);

		in_lift = _in_lift;
		next = _next;
	}

	hist[h] = res;
	return res;
}

int main()
{
	ios::sync_with_stdio(false);

	cin >> n;
	for(int i=0; i<n; ++i)
	{
		int a, b;
		cin >> a >> b;
		--a, --b;
		targets[i] = b;
		floors[a].push_back(i);
	}

	vector<int> in_lift;
	int ans = search(0, in_lift, 0, 0);
	ans += n*2;

	cout << ans << "\n";
	return 0;
}


#include <cmath>
#include <cstdio>
#include <cstring>
#include <map>
#include <vector>
#include <string>
#include <numeric>
#include <sstream>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long ll;

map<ll,int> hist;
int targets[2048];
vector<int> floors[16];
int n;

int search(int floor0, vector<int> &in_lift, int next, int depth)
{
	if(n*4 < depth)
		return 999999999;
	if(next == n && in_lift.empty())
		return 0;

	sort(in_lift.begin(), in_lift.end());
	ll h = floor0;
	h = 16*h + next;
	for(int i=0; i<in_lift.size(); ++i)
		h = 2048*h + in_lift[i];
	for(int i=in_lift.size(); i<4; ++i)
		h = 2048*h + 2001;
	if(hist.count(h))
		return hist[h];

	int res = 999999999;
	int _next=next;
	vector<int> _in_lift = in_lift;

	for(int fl=0; fl<9; ++fl)
	{
		bool ok = 0;
		for(int i=0; i<in_lift.size(); ++i)
		{
			if(targets[in_lift[i]] == fl)
			{
				in_lift[i] = in_lift.back();
				in_lift.pop_back();
				--i;
				ok |= 1;
			}
		}

		int w = abs(fl - floor0);

		bool changed = true;
		while(changed)
		{
			changed = false;
			for(int i=0; i<floors[fl].size(); ++i)
				if(floors[fl][i] == next && in_lift.size() < 4)
				{
					in_lift.push_back(next++);
					//++w;
					changed = true;
					ok |= 1;
				}
		}
		if(!ok)
		{
			in_lift = _in_lift;
			next = _next;
			continue;
		}

		int v = search(fl, in_lift, next, depth+1) + w;
		res = min(v, res);

		in_lift = _in_lift;
		next = _next;
	}

	hist[h] = res;
	return res;
}

int main()
{
	ios::sync_with_stdio(false);

	cin >> n;
	for(int i=0; i<n; ++i)
	{
		int a, b;
		cin >> a >> b;
		--a, --b;
		targets[i] = b;
		floors[a].push_back(i);
	}

	vector<int> in_lift;
	int ans = search(0, in_lift, 0, 0);
	ans += n*2;

	cout << ans << "\n";

	return 0;
}


#include <cmath>
#include <cstdio>
#include <cstring>
#include <map>
#include <vector>
#include <string>
#include <numeric>
#include <sstream>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long ll;

map<ll,int> hist;
int targets[2048];
vector<int> floors[2048];
int n;

int search(int floor0, vector<int> &in_lift, int next, int depth)
{
	if(n*100 < depth)
		return 999999999;
	if(next == n && in_lift.empty())
		return 0;

	sort(in_lift.begin(), in_lift.end());
	ll h = floor0;
	h = 16*h + next;
	for(int i=0; i<in_lift.size(); ++i)
		h = 2048*h + in_lift[i];
	if(hist.count(h))
		return hist[h];

	int res = 999999999;
	int _floor0=floor0, _next=next;
	vector<int> _in_lift = in_lift;

	for(int fl=0; fl<9; ++fl)
	{
		bool ok = 0;
		for(int i=0; i<in_lift.size(); ++i)
		{
			if(targets[in_lift[i]] == fl)
			{
				in_lift[i] = in_lift.back();
				in_lift.pop_back();
				--i;
				ok |= 1;
			}
		}

		int w = abs(fl - floor0);

		bool changed = true;
		while(changed)
		{
			changed = false;
			for(int i=0; i<floors[fl].size(); ++i)
				if(floors[fl][i] == next && in_lift.size() < 4)
				{
					in_lift.push_back(next++);
					//++w;
					changed = true;
					ok |= 1;
				}
		}
		if(!ok)
			continue;

		int v = search(fl, in_lift, next, depth+1) + w;
		res = min(v, res);

		in_lift = _in_lift;
		floor0 = _floor0;
		next = _next;
	}

	hist[h] = res;
	return res;
}

int main()
{
	ios::sync_with_stdio(false);

	cin >> n;
	for(int i=0; i<n; ++i)
	{
		int a, b;
		cin >> a >> b;
		--a, --b;
		targets[i] = b;
		floors[a].push_back(i);
	}

	vector<int> in_lift;
	int ans = search(0, in_lift, 0, 0);
	ans += n*2;

	cout << ans << "\n";

	return 0;
}


#include <cmath>
#include <cstdio>
#include <cstring>
#include <map>
#include <vector>
#include <string>
#include <numeric>
#include <sstream>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long ll;

map<ll,int> hist;
int targets[2048];
vector<int> floors[2048];
int n;

int search(int floor0, vector<int> &in_lift, int next, int depth)
{
	if(n*4 < depth)
		return 999999999;
	if(next == n && in_lift.empty())
		return 0;

	//sort(in_lift.begin(), in_lift.end());
	//ll h = floor0;
	//h = 16*h + next;
	//for(int i=0; i<in_lift.size(); ++i)
	//	h = 2048*h + in_lift[i];
	//if(hist.count(h))
	//	return hist[h];

	int res = 999999999;
	int _floor0=floor0, _next=next;
	vector<int> _in_lift = in_lift;

	for(int fl=0; fl<9; ++fl)
	{
		bool ok = 0;
		for(int i=0; i<in_lift.size(); ++i)
		{
			if(targets[in_lift[i]] == fl)
			{
				in_lift[i] = in_lift.back();
				in_lift.pop_back();
				--i;
				ok |= 1;
			}
		}

		int w = abs(fl - floor0);

		bool changed = true;
		while(changed)
		{
			changed = false;
			for(int i=0; i<floors[fl].size(); ++i)
				if(floors[fl][i] == next && in_lift.size() < 4)
				{
					in_lift.push_back(next++);
					//++w;
					changed = true;
					ok |= 1;
				}
		}
		if(!ok)
		{
			in_lift = _in_lift;
			floor0 = _floor0;
			next = _next;
			continue;
		}

		int v = search(fl, in_lift, next, depth+1) + w;
		res = min(v, res);

		in_lift = _in_lift;
		floor0 = _floor0;
		next = _next;
	}

	//hist[h] = res;
	return res;
}

int main()
{
	ios::sync_with_stdio(false);

	cin >> n;
	for(int i=0; i<n; ++i)
	{
		int a, b;
		cin >> a >> b;
		--a, --b;
		targets[i] = b;
		floors[a].push_back(i);
	}

	vector<int> in_lift;
	int ans = search(0, in_lift, 0, 0);
	ans += n*2;

	cout << ans << "\n";

	return 0;
}


#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <map>
#include <set>
#include <cstring>

using namespace std;

#define FOR(i, a, b) for(int i = a; i < b; i++)
#define PB push_back
#define MAX_INT_32 ((1 << 30) -1 );

const int N = 2000;
const int FLOOR = 10;
int n;
int A[N + 1 ], B[N + 1];
int f[N + 1][FLOOR][FLOOR][FLOOR][FLOOR][FLOOR];

int dfs(int employee_id, int floor, int a, int b, int c, int d)
{
    if ( employee_id == n && a == 0 && b == 0 && c == 0 && d == 0 )
        return 0;
    //a >= b >= c >= d
    if ( f[employee_id][floor][a][b][c][d] != -1 )
        return f[employee_id][floor][a][b][c][d];

    int ret = MAX_INT_32;
    if ( employee_id != n )
    {
        int next = employee_id + 1;
        int cost = abs(floor - A[next]);
        std::vector<int> tmp;
#define process(a)  if ( a == A[next] ){ cost++; } else { tmp.push_back(a); }
        process(a);
        process(b);
        process(c);
        process(d);
        tmp.PB(B[next]);
        cost++;
        sort(tmp.begin(), tmp.end());
        int count(0);
        FOR(i, 0, tmp.size())
            if ( tmp[i] > 0 )
                count++;
        while ( tmp.size() < 4 )
            tmp.PB(0);
        int size = tmp.size();
        if ( count <= 4 )
            ret = min(ret, cost + dfs( employee_id+1, A[next], tmp[size -1], tmp[size -2], tmp[size - 3], tmp[size - 4] ) );
#undef process
    }
    std::vector<int> now;
    now.PB(a);
    now.PB(b);
    now.PB(c);
    now.PB(d);
    FOR(i, 0, now.size())
        if ( now[i] > 0 )
        {
            int cost = abs(floor - now[i]);
            std::vector<int> tmp;
            FOR(j, 0, now.size())
                if ( now[j] != now[i] )
                    tmp.PB( now[j] );
                else
                    cost++;
            while ( tmp.size() < 4 )
                tmp.PB(0);
            int size = tmp.size();
            ret = min(ret, cost + dfs( employee_id, now[i], tmp[size - 1], tmp[size - 2], tmp[size - 3], tmp[size - 4] ) );
        }
    f[employee_id][floor][a][b][c][d] = ret;
    //printf("employee_id=%d, floor=%d, (%d, %d, %d, %d) %d\n", employee_id, floor, a, b, c, d, ret);
    return ret;
}

int main(int argc, char * argv[])
{

    while ( scanf("%d", &n) != EOF )
    {
        FOR(i, 1, n + 1) scanf("%d %d", &A[i], &B[i]);
        memset(f, -1, sizeof(f));
        printf("%d\n", dfs(0, 1, 0, 0, 0, 0));
    }

    return 0;
}


#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <map>
#include <set>
#include <cstring>

using namespace std;

#define FOR(i, a, b) for(int i = a; i < b; i++)
#define PB push_back
#define MAX_INT_32 ((1 << 30) -1 );

const int N = 20;
const int FLOOR = 10;
int n;
int A[N + 1 ], B[N + 1];
int f[N + 1][FLOOR][FLOOR][FLOOR][FLOOR][FLOOR];

int dfs(int employee_id, int floor, int a, int b, int c, int d)
{
    if ( employee_id == n && a == 0 && b == 0 && c == 0 && d == 0 )
        return 0;
    //a >= b >= c >= d
    if ( f[employee_id][floor][a][b][c][d] != -1 )
        return f[employee_id][floor][a][b][c][d];

    int ret = MAX_INT_32;
    if ( employee_id != n )
    {
        int next = employee_id + 1;
        int cost = abs(floor - A[next]);
        std::vector<int> tmp;
#define process(a)  if ( a == A[next] ){ cost++; } else { tmp.push_back(a); }
        process(a);
        process(b);
        process(c);
        process(d);
        tmp.PB(B[next]);
        cost++;
        sort(tmp.begin(), tmp.end());
        int count(0);
        FOR(i, 0, tmp.size())
            if ( tmp[i] > 0 )
                count++;
        while ( tmp.size() < 4 )
            tmp.PB(0);
        int size = tmp.size();
        if ( count <= 4 )
            ret = min(ret, cost + dfs( employee_id+1, A[next], tmp[size -1], tmp[size -2], tmp[size - 3], tmp[size - 4] ) );
#undef process
    }
    std::vector<int> now;
    now.PB(a);
    now.PB(b);
    now.PB(c);
    now.PB(d);
    FOR(i, 0, now.size())
        if ( now[i] > 0 )
        {
            int cost = abs(floor - now[i]);
            std::vector<int> tmp;
            FOR(j, 0, now.size())
                if ( now[j] != now[i] )
                    tmp.PB( now[j] );
                else
                    cost++;
            while ( tmp.size() < 4 )
                tmp.PB(0);
            int size = tmp.size();
            ret = min(ret, cost + dfs( employee_id, now[i], tmp[size - 1], tmp[size - 2], tmp[size - 3], tmp[size - 4] ) );
        }
    f[employee_id][floor][a][b][c][d] = ret;
    //printf("employee_id=%d, floor=%d, (%d, %d, %d, %d) %d\n", employee_id, floor, a, b, c, d, ret);
    return ret;
}

int main(int argc, char * argv[])
{

    while ( scanf("%d", &n) != EOF )
    {
        FOR(i, 1, n + 1) scanf("%d %d", &A[i], &B[i]);
        memset(f, -1, sizeof(f));
        printf("%d\n", dfs(0, 1, 0, 0, 0, 0));
    }

    return 0;
}


#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <map>
#include <set>
#include <cstring>

using namespace std;

#define FOR(i, a, b) for(int i = a; i < b; i++)
#define PB push_back
#define MAX_INT_32 ((1 << 30) -1 );

const int N = 2000;
const int FLOOR = 10;
int n;
int A[N + 1 ], B[N + 1];
//int f[N + 1][FLOOR][FLOOR][FLOOR][FLOOR][FLOOR];
std::map< std::vector<int>, int> f;

int dfs(int employee_id, int floor, int a, int b, int c, int d)
{
    if ( employee_id == n && a == 0 && b == 0 && c == 0 && d == 0 )
        return 0;
    //a >= b >= c >= d
    std::vector<int> key;
    key.PB(employee_id);
    key.PB(floor);
    key.PB(a);
    key.PB(b);
    key.PB(c);
    key.PB(d);
    if ( f.find(key) != f.end() )
        return f[key];

    int ret = MAX_INT_32;
    if ( employee_id != n )
    {
        int next = employee_id + 1;
        int cost = abs(floor - A[next]);
        std::vector<int> tmp;
#define process(a)  if ( a == A[next] ){ cost++; } else { tmp.push_back(a); }
        process(a);
        process(b);
        process(c);
        process(d);
        tmp.PB(B[next]);
        cost++;
        sort(tmp.begin(), tmp.end());
        int count(0);
        FOR(i, 0, tmp.size())
            if ( tmp[i] > 0 )
                count++;
        while ( tmp.size() < 4 )
            tmp.PB(0);
        int size = tmp.size();
        if ( count <= 4 )
            ret = min(ret, cost + dfs( employee_id+1, A[next], tmp[size -1], tmp[size -2], tmp[size - 3], tmp[size - 4] ) );
#undef process
    }
    std::vector<int> now;
    now.PB(a);
    now.PB(b);
    now.PB(c);
    now.PB(d);
    FOR(i, 0, now.size())
        if ( now[i] > 0 )
        {
            int cost = abs(floor - now[i]);
            std::vector<int> tmp;
            FOR(j, 0, now.size())
                if ( now[j] != now[i] )
                    tmp.PB( now[j] );
                else
                    cost++;
            while ( tmp.size() < 4 )
                tmp.PB(0);
            int size = tmp.size();
            ret = min(ret, cost + dfs( employee_id, now[i], tmp[size - 1], tmp[size - 2], tmp[size - 3], tmp[size - 4] ) );
        }
    //f[employee_id][floor][a][b][c][d] = ret;
    //printf("employee_id=%d, floor=%d, (%d, %d, %d, %d) %d\n", employee_id, floor, a, b, c, d, ret);
    f[key] = ret;
    return ret;
}

int main(int argc, char * argv[])
{

    while ( scanf("%d", &n) != EOF )
    {
        f.clear();
        FOR(i, 1, n + 1) scanf("%d %d", &A[i], &B[i]);
        //memset(f, -1, sizeof(f));
        printf("%d\n", dfs(0, 1, 0, 0, 0, 0));
    }

    return 0;
}


#include <cstdio>
#include <algorithm>
using namespace std;
typedef pair<int,int> pii;
typedef pair<int,pii> piii;

int n,A[2010],B[2010];

int n2s[10010],s2n[10010];
int q[20000010],dis[20000010];

void prepare()
{
    int i,a,b,c,d,cnt;

    for(i=cnt=0;i<10000;i++)
    {
        a = i / 1000; b = i / 100 % 10; c = i / 10 % 10; d = i % 10;
        if(!(a<=b && b<=c && c<=d)) continue;
        n2s[cnt] = i; s2n[i] = cnt;
        cnt++;
    }
}

int id(int p,int f,int s)
{
    return p * 10000 + f * 1000 + s;
}

void numToState(int s,int &a,int &b,int &c,int &d)
{
    s = n2s[s];
    a = s / 1000; b = s / 100 % 10; c = s / 10 % 10; d = s % 10;
}

int stateToNum(int a,int b,int c,int d)
{
    int arr[4];
    arr[0] = a; arr[1] = b; arr[2] = c; arr[3] = d;
    sort(arr,arr+4);
    a = arr[0]; b = arr[1]; c = arr[2]; d = arr[3];
    int s = a * 1000 + b * 100 + c * 10 + d;
    return s2n[s];
}

#define GOTO_NEXT(p,f,a,b,c,d) \
{ \
    pp = (p); ff = (f); ss = stateToNum(a,b,c,d); \
    nxt = id(pp,ff,ss); \
    if(!dis[nxt]) \
    { \
        dis[nxt] = dis[q[head]] + 1; \
        if(pp > n && ss == 0) return dis[nxt] - 1; \
        q[tail++] = nxt; \
    } \
}

int bfs()
{
    int head = 1,tail = 2;
    int p,f,s,a,b,c,d;
    int pp,ff,ss,nxt;
    q[1] = id(1,1,0); dis[q[1]] = 1;

    while(head < tail)
    {
        p = q[head] / 10000; f = q[head] / 1000 % 10; s = q[head] % 1000;
        numToState(s,a,b,c,d);

        if(f > 1) GOTO_NEXT(p,f-1,a,b,c,d)
        if(f < 9) GOTO_NEXT(p,f+1,a,b,c,d)

        if(a == f) GOTO_NEXT(p,f,0,b,c,d)
        else if(b == f) GOTO_NEXT(p,f,a,0,c,d)
        else if(c == f) GOTO_NEXT(p,f,a,b,0,d)
        else if(d == f) GOTO_NEXT(p,f,a,b,c,0)
        else if(A[p] == f)
        {
            if(a == 0) GOTO_NEXT(p+1,f,B[p],b,c,d)
            else if(b == 0) GOTO_NEXT(p+1,f,a,B[p],c,d)
            else if(c == 0) GOTO_NEXT(p+1,f,a,b,B[p],d)
            else if(d == 0) GOTO_NEXT(p+1,f,a,b,c,B[p])
        }

        head++;
    }

    return -1;
}

int main()
{
    int i;
    prepare();

    scanf("%d",&n);
    for(i=1;i<=n;i++) scanf("%d%d",&A[i],&B[i]);

    printf("%d",bfs());
    return 0;
}


#include <cstdio>
#include <algorithm>
using namespace std;
typedef pair<int,int> pii;
typedef pair<int,pii> piii;

int n,A[2010],B[2010];

int n2s[10010],s2n[10010];
int q[21000010],dis[21000010];

void prepare()
{
    int i,a,b,c,d,cnt;

    for(i=cnt=0;i<10000;i++)
    {
        a = i / 1000; b = i / 100 % 10; c = i / 10 % 10; d = i % 10;
        if(!(a<=b && b<=c && c<=d)) continue;
        n2s[cnt] = i; s2n[i] = cnt;
        cnt++;
    }
}

int id(int p,int f,int s)
{
    return p * 10000 + f * 1000 + s;
}

void numToState(int s,int &a,int &b,int &c,int &d)
{
    s = n2s[s];
    a = s / 1000; b = s / 100 % 10; c = s / 10 % 10; d = s % 10;
}

int stateToNum(int a,int b,int c,int d)
{
    int arr[4];
    arr[0] = a; arr[1] = b; arr[2] = c; arr[3] = d;
    sort(arr,arr+4);
    a = arr[0]; b = arr[1]; c = arr[2]; d = arr[3];
    int s = a * 1000 + b * 100 + c * 10 + d;
    return s2n[s];
}

#define GOTO_NEXT(p,f,a,b,c,d) \
{ \
    pp = (p); ff = (f); ss = stateToNum(a,b,c,d); \
    nxt = id(pp,ff,ss); \
    if(!dis[nxt]) \
    { \
        dis[nxt] = dis[q[head]] + 1; \
        if(pp >= n && ss == 0) return dis[nxt] - 1; \
        q[tail++] = nxt; \
    } \
}

int bfs()
{
    int head = 1,tail = 2;
    int p,f,s,a,b,c,d;
    int pp,ff,ss,nxt;
    q[1] = id(0,1,0); dis[q[1]] = 1;

    while(head < tail)
    {
        p = q[head] / 10000; f = q[head] / 1000 % 10; s = q[head] % 1000;
        numToState(s,a,b,c,d);

        if(f > 1) GOTO_NEXT(p,f-1,a,b,c,d)
        if(f < 9) GOTO_NEXT(p,f+1,a,b,c,d)

        if(a == f) GOTO_NEXT(p,f,0,b,c,d)
        else if(b == f) GOTO_NEXT(p,f,a,0,c,d)
        else if(c == f) GOTO_NEXT(p,f,a,b,0,d)
        else if(d == f) GOTO_NEXT(p,f,a,b,c,0)
        else if(A[p] == f)
        {
            if(a == 0) GOTO_NEXT(p+1,f,B[p],b,c,d)
            else if(b == 0) GOTO_NEXT(p+1,f,a,B[p],c,d)
            else if(c == 0) GOTO_NEXT(p+1,f,a,b,B[p],d)
            else if(d == 0) GOTO_NEXT(p+1,f,a,b,c,B[p])
        }

        head++;
    }

    return -1;
}

int main()
{
    int i;
    prepare();

    scanf("%d",&n);
    for(i=0;i<n;i++) scanf("%d%d",&A[i],&B[i]);

    printf("%d",bfs());
    return 0;
}


#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <vector>
#include <string>
#include <queue>
#include <map>
using namespace std;

const int MaxN = 2005;

struct state {
    int m, f, s;
    state() {}
    state(int m, int f, int s): m(m), f(f), s(s) {}
};

vector<int> rep;
vector<state> q;
int n;
int a[MaxN], b[MaxN], f[MaxN][10][500], pos[MaxN * 10];

void update(int mm, int ff, int ss, int tt) {
    if (f[mm][ff][pos[ss]] < 0 || f[mm][ff][pos[ss]] > tt) {
        f[mm][ff][pos[ss]] = tt;
        q.push_back(state(mm, ff, ss));
    }
}

void door(int mm, int ff, int ss, int tt) {
    int cnt = 0, tmp = ss, d[4];
    for (int k = 3; k >= 0; -- k) {
        d[k] = tmp % 10;
        tmp /= 10;
        cnt += (d[k] == ff);
    }
    if (cnt == 0 && a[mm + 1] != ff) return;
    for (int k = 0; k < 4; ++ k)
        if (d[k] == ff) {
            d[k] = 0;
            ++ tt;
        }
    sort(d, d + 4);
    int pos = 0, k = mm;
    while (pos < 4 && d[pos] == 0 && k < n && a[k + 1] == ff) {
        d[pos ++] = b[++ k];
        ++ tt;
    }
    sort(d, d + 4);
    update(k, ff, d[0] * 1000 + d[1] * 100 + d[2] * 10 + d[3], tt);
}

int main() {
    rep.clear();
    for (int i = 0; i < 9; ++ i)
        for (int j = i; j < 9; ++ j)
            for (int k = j; k < 9; ++ k)
                for (int l = k; l < 9; ++ l)
                    rep.push_back(i * 1000 + j * 100 + k * 10 + l);
    sort(rep.begin(), rep.end());
    memset(pos, -1, sizeof pos);
    for (int k = 0; k < rep.size(); ++ k)
        pos[rep[k]] = k;

    scanf("%d", &n);
    for (int k = 1; k <= n; ++ k)
        scanf("%d %d", a + k, b + k);
    memset(f, -1, sizeof f);
    q.clear();
    f[0][1][0] = 0;
    int h = 0;
    q.push_back(state(0, 1, 0));
    while (h < q.size()) {
        state cur = q[h ++];
        int t = f[cur.m][cur.f][pos[cur.s]];
        if (cur.f > 1) update(cur.m, cur.f - 1, cur.s, t + 1);
        if (cur.f < 9) update(cur.m, cur.f + 1, cur.s, t + 1);
        door(cur.m, cur.f, cur.s, t);
    }

    int ans = -1;
    for (int k = 1; k <= 9; ++ k) {
        if (f[n][k][0] == -1) continue;
        if (ans < 0 || f[n][k][0] < ans) ans = f[n][k][0];
    }
    printf("%d\n", ans);

    return 0;
}


#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <vector>
#include <string>
#include <queue>
#include <map>
using namespace std;

const int MaxN = 2005;

struct state {
    int m, f, s;
    state() {}
    state(int m, int f, int s): m(m), f(f), s(s){}
};

vector<int> rep;
vector<state> q;
int n;
int a[MaxN], b[MaxN], f[MaxN][10][750], pos[MaxN * 10];

void update(int mm, int ff, int ss, int tt) {
    if (f[mm][ff][pos[ss]] < 0 || f[mm][ff][pos[ss]] > tt) {
        f[mm][ff][pos[ss]] = tt;
        q.push_back(state(mm, ff, ss));
    }
}

void door(int mm, int ff, int ss, int tt) {
    int cnt = 0, tmp = ss, d[4];
    for (int k = 3; k >= 0; -- k) {
        d[k] = tmp % 10;
        tmp /= 10;
        if (d[k] == ff) ++ cnt;
    }
    if (cnt == 0 && a[mm + 1] != ff) return;
    for (int k = 0; k < 4; ++ k)
        if (d[k] == ff) {
            d[k] = 0;
            ++ tt;
        }
    sort(d, d + 4);
    int pos = 0, k = mm + 1;
    while (pos < 4 && d[pos] == 0 && k <= n && a[k] == ff) {
        d[pos] = b[k];
        ++ pos; ++ k; ++ tt;
    }
    sort(d, d + 4);
    update(k - 1, ff, d[0] * 1000 + d[1] * 100 + d[2] * 10 + d[3], tt);
}

int main() {
    rep.clear();
    for (int i = 0; i <= 9; ++ i)
        for (int j = i; j <= 9; ++ j)
            for (int k = j; k <= 9; ++ k)
                for (int l = k; l <= 9; ++ l)
                    rep.push_back(i * 1000 + j * 100 + k * 10 + l);
    sort(rep.begin(), rep.end());
    memset(pos, -1, sizeof pos);
    for (int k = 0; k < rep.size(); ++ k)
        pos[rep[k]] = k;

    scanf("%d", &n);
    for (int k = 1; k <= n; ++ k)
        scanf("%d %d", a + k, b + k);
    memset(f, -1, sizeof f);
    q.clear();
    f[0][1][0] = 0;
    int h = 0;
    q.push_back(state(0, 1, 0));
    while (h < q.size()) {
        state cur = q[h ++];
        int t = f[cur.m][cur.f][pos[cur.s]];
        if (cur.f > 1) update(cur.m, cur.f - 1, cur.s, t + 1);
        if (cur.f < 9) update(cur.m, cur.f + 1, cur.s, t + 1);
        door(cur.m, cur.f, cur.s, t);
    }

    int ans = -1;
    for (int k = 1; k <= 9; ++ k) {
        if (f[n][k][0] < 0) continue;
        if (ans < 0 || f[n][k][0] < ans) ans = f[n][k][0];
    }
    printf("%d\n", ans);

    return 0;
}


#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <vector>
#include <string>
#include <queue>
#include <map>
using namespace std;

const int MaxN = 2005;

struct state {
    int m, f, s;
    state() {}
    state(int m, int f, int s): m(m), f(f), s(s){}
};

vector<int> rep;
vector<state> q;
int n;
int a[MaxN], b[MaxN], f[MaxN][10][500], pos[MaxN * 10];

void update(int mm, int ff, int ss, int tt) {
    if (f[mm][ff][pos[ss]] < 0 || f[mm][ff][pos[ss]] > tt) {
        f[mm][ff][pos[ss]] = tt;
        q.push_back(state(mm, ff, ss));
    }
}

void door(int mm, int ff, int ss, int tt) {
    int cnt = 0, tmp = ss, d[4];
    for (int k = 3; k >= 0; -- k) {
        d[k] = tmp % 10;
        tmp /= 10;
        if (d[k] == ff) ++ cnt;
    }
    if (cnt == 0 && a[mm + 1] != ff) return;
    for (int k = 0; k < 4; ++ k)
        if (d[k] == ff) {
            d[k] = 0;
            ++ tt;
        }
    sort(d, d + 4);
    int pos = 0, k = mm + 1;
    while (pos < 4 && d[pos] == 0 && k <= n && a[k] == ff) {
        d[pos] = b[k];
        ++ pos; ++ k; ++ tt;
    }
    sort(d, d + 4);
    update(k - 1, ff, d[0] * 1000 + d[1] * 100 + d[2] * 10 + d[3], tt);
}

int main() {
    rep.clear();
    for (int i = 0; i < 9; ++ i)
        for (int j = i; j < 9; ++ j)
            for (int k = j; k < 9; ++ k)
                for (int l = k; l < 9; ++ l)
                    rep.push_back(i * 1000 + j * 100 + k * 10 + l);
    sort(rep.begin(), rep.end());
    memset(pos, -1, sizeof pos);
    for (int k = 0; k < rep.size(); ++ k)
        pos[rep[k]] = k;

    scanf("%d", &n);
    for (int k = 1; k <= n; ++ k)
        scanf("%d %d", a + k, b + k);
    memset(f, -1, sizeof f);
    q.clear();
    f[0][1][0] = 0;
    int h = 0;
    q.push_back(state(0, 1, 0));
    while (h < q.size()) {
        state cur = q[h ++];
        int t = f[cur.m][cur.f][pos[cur.s]];
        if (cur.f > 1) update(cur.m, cur.f - 1, cur.s, t + 1);
        if (cur.f < 9) update(cur.m, cur.f + 1, cur.s, t + 1);
        door(cur.m, cur.f, cur.s, t);
    }

    int ans = -1;
    for (int k = 1; k <= 9; ++ k) {
        if (f[n][k][0] < 0) continue;
        if (ans < 0 || f[n][k][0] < ans) ans = f[n][k][0];
    }
    printf("%d\n", ans);

    return 0;
}


#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<map>
#include<string>
using namespace std;

const int inf = 21474836;
const int maxn =2005;
const int F = 10;
map<string,int>d[maxn][10];
int start[maxn] , dest[maxn];
int n;
int dp(int i,int j,string s){
    if(d[i][j].count(s)) return d[i][j][s];
    d[i][j][s] = inf;
    string os = s;
    for(int k = 0; k<4;k++){
          int flo = s[k] -'0';
          if(flo == j) s[k]='0';
    }
    int pos = i + 1;
    for(int k=0;k<4;k++){
        int flo = s[k] -'0';
        if(pos <= n && flo == 0 && start[pos] == j)  s[k] = '0' + dest[pos] , ++pos;
    }
    if(i == n && s == (string)"0000"){
          return d[i][j][os] = 0;
    }
    sort(s.begin(),s.end());
    for(int k = 3 ; k>=0 ; k--){
         if(s[k] -'0'!= 0)
         d[i][j][os] = min(d[i][j][os] , dp(pos - 1, s[k]-'0',s)+abs(j - (s[k]-'0')));
         else if(pos - 1< n){
            d[i][j][os] = min(d[i][j][os] , dp(pos - 1, start[pos],s) + abs(j - start[pos]));
            break;
         }
    }
    return d[i][j][os];
}
int main()
{
   scanf("%d",&n);
   for(int i=1;i<=n;i++){
       scanf("%d %d",&start[i],&dest[i]);
   }
   cout<<dp(0,1,"0000") + 2 *n ;
   return 0;
}


#include <bits/stdc++.h>

#define inf 0x3f3f3f3f

#define INF 0x3f3f3f3f3f3f3f3fLL

#define MOD 1000000007

#define pb push_back

#define sq(x) ((x)*(x))

#define x first

#define y second

#define eps 1e-8

#define bpt(x) (__builtin_popcount(x))

#define bptl(x) (__builtin_popcountll(x))

#define bit(x, y) (((x)>>(y))&1)

#define bclz(x) (__builtin_clz(x))

#define bclzl(x) (__builtin_clzll(x))

#define bctz(x) (__builtin_ctz(x))

#define bctzl(x) (__builtin_ctzll(x))

using namespace std;

typedef long long INT;

typedef vector<int> VI;

typedef pair<int, int> pii;

typedef pair<pii, int> pi3;

typedef double DO;

template<typename T, typename U> inline void smin(T &a, U b) {if (a>b) a=b;}

template<typename T, typename U> inline void smax(T &a, U b) {if (a<b) a=b;}

template<typename T> inline void gn(T &x) {char c, sg=0; while (c=getchar(), (c>'9' || c<'0') && c!='-');for((c=='-'?sg=1, c=getchar():0), x=0; c>='0' && c<='9'; c=getchar()) x=(x<<1)+(x<<3)+c-'0';if (sg) x=-x;}

template<class T, class T1>inline void gn(T &x, T1 &y) {gn(x); gn(y);}

template<class T, class T1, class T2>inline void gn(T &x, T1 &y, T2 &z) {gn(x); gn(y); gn(z);}

template<typename T> inline void print(T x) {if (x<0) {putchar('-');return print(-x);}if (x<10) {putchar('0'+x);return;} print(x/10);putchar(x%10+'0');}

template<typename T> inline void println(T x) {print(x), putchar('\n');}

template<typename T> inline void printsp(T x) {print(x), putchar(' ');}

template<class T, class T1> inline void print(T x, T1 y) {printsp(x), println(y);}

template<class T, class T1, class T2> inline void print(T x, T1 y, T2 z) {printsp(x), printsp(y), println(z);}

int power(int a, int b, int m, int ans=1) {

	for (; b; b>>=1, a=1LL*a*a%m) if (b&1) ans=1LL*ans*a%m;

	return ans;

}

#define NN 2020

int a[NN], b[NN];

int dp[NN][716];

int st[NN], cnt, id[10000];

int get(int *d){

	return id[d[0]*1000+d[1]*100+d[2]*10+d[3]];

}

int main() {

#ifndef ONLINE_JUDGE

	freopen("in.in", "r", stdin);

	freopen("out.out", "w", stdout);

#endif

	int n;

	gn(n);

	for(int i=1; i<=n; i++) gn(a[i], b[i]), a[i], b[i];

	for(int i=0; i<10000; i++){

		int d[4]={i/1000, i%1000/100, i%100/10, i%10};

		int ok=1;

		for(int j=0; j<3; j++) if(d[j]>d[j+1]) ok=0;

		if(ok) st[cnt]=i, id[i]=cnt++;

	}

	memset(dp, 0x3f, sizeof(dp));

	dp[1][id[b[1]]]=a[1]-1;

	a[n+1]=b[n];

	b[n+1]=0;

	for(int i=1; i<=n; i++){

		for(int j=0; j<cnt; j++){

			int s=st[j];

			if(dp[i][j]==inf) continue;

			for(int p1=1; p1<=9; p1++){

				for(int p2=1; p2<=9; p2++){

					int dis=abs(a[i]-p1)+abs(p1-p2)+abs(p2-a[i+1]);

					int mn=min(min(a[i], a[i+1]), min(p1, p2));

					int mx=max(max(a[i], a[i+1]), max(p1, p2));

					int d[4]={s/1000, s%1000/100, s%100/10, s%10};

					for(int k=0; k<4; k++) if(d[k]>=mn && d[k]<=mx) d[k]=0;

					sort(d, d+4);

					if(d[0]==0){

						d[0]=b[i+1];

						sort(d, d+4);

						smin(dp[i+1][get(d)], dp[i][j]+dis);

					}

				}

			}

		}

	}

	println(dp[n+1][id[0]]+n*2);

	return 0;

}