import java.util.*;
import java.io.*;

public class P983C
{
	public static void main(String[] args)
	{
		FastScanner in = new FastScanner(System.in);
		int n = in.nextInt();
		int[][][] dp = new int[2][10000][10];
		for (int[][] d : dp)
			for (int[] arr : d)
				Arrays.fill(arr, Integer.MAX_VALUE);
		dp[0][0][1] = 0;
		for (int i = 0; i < n; i++) {
			int a = in.nextInt();
			int b = in.nextInt();
			int[] digits = new int[4];
			for (int w = 9000; w >= 0; w -= 1000) {
				digits[0] = w / 1000;
				int k1 = w / 10;
				for (int x = 900; x >= k1; x -= 100) {
					digits[1] = x / 100;
					int k2 = x / 10;
					for (int y = 90; y >= k2; y -= 10) {
						digits[2] = y / 10;
						for (int z = 9; z >= digits[2]; z--) {
							digits[3] = z;
							int dest = w + x + y + z;
							for (int f = 1; f <= 9; f++) {
								if (dp[0][dest][f] == Integer.MAX_VALUE)
									continue;

								// drop off
								int temp = dest;
								boolean[] been = new boolean[10];
								while (temp > 0) {
									int r = temp % 10;
									temp /= 10;
									if (been[r])
										continue;
									been[r] = true;
									int next = 0;
									for (int num : digits) {
										if (num != r) {
											next *= 10;
											next += num;
										}
									}
									dp[0][next][r] = Math.min(dp[0][next][r], dp[0][dest][f] + Math.abs(f - r));
								}

								// pick up
								if (dest < 1000) {
									int next = 0;
									boolean added = false;
									for (int num : digits) {
										if (num != a) {
											if (num > b && !added) {
												next *= 10;
												next += b;
												added = true;
											}
											next *= 10;
											next += num;
										}
									}
									if (!added) {
										next *= 10;
										next += b;
									}
									dp[1][next][a] = Math.min(dp[1][next][a], dp[0][dest][f] + Math.abs(f - a));
								}
							}
						}
					}
				}
			}
			dp[0] = dp[1];
			dp[1] = new int[10000][10];
			for (int[] arr : dp[1])
				Arrays.fill(arr, Integer.MAX_VALUE);
		}

		int[] digits = new int[4];
		for (int w = 9000; w >= 0; w -= 1000) {
			digits[0] = w / 1000;
			int k1 = w / 10;
			for (int x = 900; x >= k1; x -= 100) {
				digits[1] = x / 100;
				int k2 = x / 10;
				for (int y = 90; y >= k2; y -= 10) {
					digits[2] = y / 10;
					for (int z = 9; z >= digits[2]; z--) {
						digits[3] = z;
						int dest = w + x + y + z;
						for (int f = 1; f <= 9; f++) {
							if (dp[0][dest][f] == Integer.MAX_VALUE)
								continue;

							// drop off
							int temp = dest;
							boolean[] been = new boolean[10];
							while (temp > 0) {
								int r = temp % 10;
								temp /= 10;
								if (been[r])
									continue;
								been[r] = true;
								int next = 0;
								for (int num : digits) {
									if (num != r) {
										next *= 10;
										next += num;
									}
								}
								dp[0][next][r] = Math.min(dp[0][next][r], dp[0][dest][f] + Math.abs(f - r));
							}
						}
					}
				}
			}
		}

		int min = Integer.MAX_VALUE;
		for (int f = 1; f <= 9; f++)
			min = Math.min(min, dp[0][0][f]);
		min += n * 2;
		System.out.println(min);
	}

	/**
	 * Source: Matt Fontaine
	 */
	public static class FastScanner
	{
		private InputStream stream;
		private byte[] buf = new byte[1024];
		private int curChar;
		private int numChars;

		public FastScanner(InputStream stream)
		{
			this.stream = stream;
		}

		int read()
		{
			if (numChars == -1)
				throw new InputMismatchException();
			if (curChar >= numChars)
			{
				curChar = 0;
				try
				{
					numChars = stream.read(buf);
				} catch (IOException e)
				{
					throw new InputMismatchException();
				}
				if (numChars <= 0)
					return -1;
			}
			return buf[curChar++];
		}

		boolean isSpaceChar(int c)
		{
			return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
		}

		boolean isEndline(int c)
		{
			return c == '\n' || c == '\r' || c == -1;
		}

		public int nextInt()
		{
			return Integer.parseInt(next());
		}

		public long nextLong()
		{
			return Long.parseLong(next());
		}

		public double nextDouble()
		{
			return Double.parseDouble(next());
		}

		public String next()
		{
			int c = read();
			while (isSpaceChar(c))
				c = read();
			StringBuilder res = new StringBuilder();
			do
			{
				res.appendCodePoint(c);
				c = read();
			} while (!isSpaceChar(c));
			return res.toString();
		}

		public String nextLine()
		{
			int c = read();
			while (isEndline(c))
				c = read();
			StringBuilder res = new StringBuilder();
			do
			{
				res.appendCodePoint(c);
				c = read();
			} while (!isEndline(c));
			return res.toString();
		}
	}
}


import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.math.*;
import java.text.*;
import java.util.*;

public class C {
	static BufferedReader br;
	static StringTokenizer st;
	static PrintWriter pw;

	public static void main(String[] args) throws IOException {
		br = new BufferedReader(new InputStreamReader(System.in));
		pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
		//int qq = 1;
		int qq = Integer.MAX_VALUE;
		//int qq = readInt();
		for(int casenum = 1; casenum <= qq; casenum++) {
			int n = readInt();
			ArrayList<Inside> in = new ArrayList<Inside>();
			Map<Inside, Integer> rev = new HashMap<Inside, Integer>();
			for(int a = -1; a <= 8; a++) {
				for(int b = -1; b <= 8; b++) {
					for(int c = -1; c <= 8; c++) {
						for(int d = -1; d <= 8; d++) {
							in.add(new Inside(a, b, c, d));
							rev.put(new Inside(a, b, c, d), in.size()-1);
						}
					}
				}
			}
			Map<State, Integer> dp = new HashMap<State, Integer>();
			int[] lhs = new int[n];
			int[] rhs = new int[n];
			for(int i = 0; i < n; i++) {
				lhs[i] = readInt()-1;
				rhs[i] = readInt()-1;
			}
			LinkedList<State> q = new LinkedList<State>();
			dp.put(new State(0, 0, 0), 0);
			q.add(new State(0, 0, 0));
			Inside now = new Inside(-1,-1,-1,-1);
			for(int i = 0; i < 4 && i < n && lhs[i] == 0; i++) {
				now.a = rhs[i];
				now.sort();
				dp.put(new State(1, 0, rev.get(now)), 0);
				q.add(new State(1, 0, rev.get(now)));
			}
			while(!q.isEmpty()) {
				State curr = q.removeFirst();
				if(curr.idx == n && curr.mask == 0) {
					pw.println(dp.get(curr) + 2*n);
					break;
				}
				int nowDist = dp.get(curr);
				for(int dir = -1; dir <= 1; dir += 2) {
					int nextLoc = curr.loc + dir;
					if(nextLoc < 0 || nextLoc >= 9) continue;
					now = new Inside(in.get(curr.mask));
					now.clean(nextLoc);
					int nextIdx = curr.idx;
					int nextMask = rev.get(now);
					State key = new State(nextIdx, nextLoc, nextMask);
					if(!dp.containsKey(key)) {
						dp.put(key, 1 + nowDist);
						q.add(key);
					}
					while(nextIdx < n && lhs[nextIdx] == nextLoc && now.a == -1) {
						now.a = rhs[nextIdx];
						nextIdx++;
						now.sort();
						nextMask = rev.get(now);
						key = new State(nextIdx, nextLoc, nextMask);
						if(!dp.containsKey(key)) {
							dp.put(key, 1 + nowDist);
							q.add(key);
						}
					}
				}
			}
		}
		pw.close();
	}

	static class Inside {
		public int a, b, c, d;

		public Inside(Inside i) {
			this(i.a,i.b,i.c,i.d);
		}

		@Override
		public String toString() {
			return "Inside [a=" + a + ", b=" + b + ", c=" + c + ", d=" + d + "]";
		}

		public Inside(int a, int b, int c, int d) {
			super();
			this.a = a;
			this.b = b;
			this.c = c;
			this.d = d;
			sort();
		}

		public void clean(int idx) {
			if(idx==a) a=-1;
			if(idx==b) b=-1;
			if(idx==c) c=-1;
			if(idx==d) d=-1;
			sort();
		}

		public void sort() {
			if(d<c) {
				d^=c;
				c^=d;
				d^=c;
			}
			if(c<b) {
				c^=b;
				b^=c;
				c^=b;
			}
			if(b<a) {
				b^=a;
				a^=b;
				b^=a;
			}
			if(d<c) {
				d^=c;
				c^=d;
				d^=c;
			}
			if(c<b) {
				c^=b;
				b^=c;
				c^=b;
			}
			if(b<a) {
				b^=a;
				a^=b;
				b^=a;
			}
			if(d<c) {
				d^=c;
				c^=d;
				d^=c;
			}
			if(c<b) {
				c^=b;
				b^=c;
				c^=b;
			}
			if(b<a) {
				b^=a;
				a^=b;
				b^=a;
			}
		}

		@Override
		public int hashCode() {
			final int prime = 31;
			int result = 1;
			result = prime * result + a;
			result = prime * result + b;
			result = prime * result + c;
			result = prime * result + d;
			return result;
		}

		@Override
		public boolean equals(Object obj) {
			if (this == obj)
				return true;
			if (obj == null)
				return false;
			if (getClass() != obj.getClass())
				return false;
			Inside other = (Inside) obj;
			if (a != other.a)
				return false;
			if (b != other.b)
				return false;
			if (c != other.c)
				return false;
			if (d != other.d)
				return false;
			return true;
		}

	}

	static class State {
		public int idx, loc, mask;

		@Override
		public String toString() {
			return "State [idx=" + idx + ", loc=" + loc + ", mask=" + mask + "]";
		}

		public State(int idx, int loc, int mask) {
			super();
			this.idx = idx;
			this.loc = loc;
			this.mask = mask;
		}

		@Override
		public int hashCode() {
			final int prime = 31;
			int result = 1;
			result = prime * result + idx;
			result = prime * result + loc;
			result = prime * result + mask;
			return result;
		}

		@Override
		public boolean equals(Object obj) {
			if (this == obj)
				return true;
			if (obj == null)
				return false;
			if (getClass() != obj.getClass())
				return false;
			State other = (State) obj;
			if (idx != other.idx)
				return false;
			if (loc != other.loc)
				return false;
			if (mask != other.mask)
				return false;
			return true;
		}

	}

	public static void exitImmediately() {
		pw.close();
		System.exit(0);
	}

	public static int readInt() throws IOException {
		return Integer.parseInt(nextToken());
	}

	public static double readDouble() throws IOException {
		return Double.parseDouble(nextToken());
	}

	public static long readLong() throws IOException {
		return Long.parseLong(nextToken());
	}

	public static String nextLine() throws IOException {
		st = null;
		if(!br.ready()) {
			exitImmediately();
		}
		return br.readLine();
	}

	public static String nextToken() throws IOException {
		while(st == null || !st.hasMoreTokens()) {
			if(!br.ready()) {
				exitImmediately();
			}
			st = new StringTokenizer(br.readLine());
		}
		return st.nextToken();
	}
}


import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.math.*;
import java.text.*;
import java.util.*;

public class C {
	static BufferedReader br;
	static StringTokenizer st;
	static PrintWriter pw;

	public static void main(String[] args) throws IOException {
		br = new BufferedReader(new InputStreamReader(System.in));
		pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
		int qq = 1;
		//int qq = Integer.MAX_VALUE;
		//int qq = readInt();
		for(int casenum = 1; casenum <= qq; casenum++) {
			int n = readInt();
			ArrayList<Inside> in = new ArrayList<Inside>();
			Map<Inside, Integer> rev = new HashMap<Inside, Integer>();
			for(int a = -1; a <= 8; a++) {
				for(int b = -1; b <= 8; b++) {
					for(int c = -1; c <= 8; c++) {
						for(int d = -1; d <= 8; d++) {
							in.add(new Inside(a, b, c, d));
							rev.put(new Inside(a, b, c, d), in.size()-1);
						}
					}
				}
			}
			int[][][] dp = new int[n+1][9][in.size()];
			for(int i = 0; i < dp.length; i++) {
				for(int j = 0; j < dp[i].length; j++) {
					Arrays.fill(dp[i][j], 1 << 30);
				}
			}
			int[] lhs = new int[n];
			int[] rhs = new int[n];
			for(int i = 0; i < n; i++) {
				lhs[i] = readInt()-1;
				rhs[i] = readInt()-1;
			}
			dp[0][0][0] = 0;
			LinkedList<State> q = new LinkedList<State>();
			q.add(new State(0, 0, 0));
			while(!q.isEmpty()) {
				State curr = q.removeFirst();
				if(curr.idx == n && curr.mask == 0) {
					pw.println(dp[curr.idx][curr.loc][curr.mask] + 2*n);
					break;
				}
				for(int dir = -1; dir <= 1; dir += 2) {
					int nextLoc = curr.loc + dir;
					if(nextLoc < 0 || nextLoc >= 9) continue;
					Inside now = new Inside(in.get(curr.mask));
					now.clean(nextLoc);
					int nextIdx = curr.idx;
					int nextMask = rev.get(now);
					if(dp[nextIdx][nextLoc][nextMask] > 1 + dp[curr.idx][curr.loc][curr.mask]) {
						dp[nextIdx][nextLoc][nextMask] = 1 + dp[curr.idx][curr.loc][curr.mask];
						q.add(new State(nextIdx, nextLoc, nextMask));
					}
					while(nextIdx < n && lhs[nextIdx] == nextLoc && now.a == -1) {
						now.a = rhs[nextIdx];
						nextIdx++;
						now.sort();
						nextMask = rev.get(now);
						if(dp[nextIdx][nextLoc][nextMask] > 1 + dp[curr.idx][curr.loc][curr.mask]) {
							dp[nextIdx][nextLoc][nextMask] = 1 + dp[curr.idx][curr.loc][curr.mask];
							q.add(new State(nextIdx, nextLoc, nextMask));
						}
					}
				}
			}
		}
		pw.close();
	}

	static class Inside {
		public int a, b, c, d;

		public Inside(Inside i) {
			this(i.a,i.b,i.c,i.d);
		}

		@Override
		public String toString() {
			return "Inside [a=" + a + ", b=" + b + ", c=" + c + ", d=" + d + "]";
		}

		public Inside(int a, int b, int c, int d) {
			super();
			this.a = a;
			this.b = b;
			this.c = c;
			this.d = d;
			sort();
		}

		public void clean(int idx) {
			if(idx==a) a=-1;
			if(idx==b) b=-1;
			if(idx==c) c=-1;
			if(idx==d) d=-1;
			sort();
		}

		public void sort() {
			if(d<c) {
				d^=c;
				c^=d;
				d^=c;
			}
			if(c<b) {
				c^=b;
				b^=c;
				c^=b;
			}
			if(b<a) {
				b^=a;
				a^=b;
				b^=a;
			}
			if(d<c) {
				d^=c;
				c^=d;
				d^=c;
			}
			if(c<b) {
				c^=b;
				b^=c;
				c^=b;
			}
			if(b<a) {
				b^=a;
				a^=b;
				b^=a;
			}
			if(d<c) {
				d^=c;
				c^=d;
				d^=c;
			}
			if(c<b) {
				c^=b;
				b^=c;
				c^=b;
			}
			if(b<a) {
				b^=a;
				a^=b;
				b^=a;
			}
		}

		@Override
		public int hashCode() {
			final int prime = 31;
			int result = 1;
			result = prime * result + a;
			result = prime * result + b;
			result = prime * result + c;
			result = prime * result + d;
			return result;
		}

		@Override
		public boolean equals(Object obj) {
			if (this == obj)
				return true;
			if (obj == null)
				return false;
			if (getClass() != obj.getClass())
				return false;
			Inside other = (Inside) obj;
			if (a != other.a)
				return false;
			if (b != other.b)
				return false;
			if (c != other.c)
				return false;
			if (d != other.d)
				return false;
			return true;
		}

	}

	static class State {
		public int idx, loc, mask;

		@Override
		public String toString() {
			return "State [idx=" + idx + ", loc=" + loc + ", mask=" + mask + "]";
		}

		public State(int idx, int loc, int mask) {
			super();
			this.idx = idx;
			this.loc = loc;
			this.mask = mask;
		}

	}

	public static void exitImmediately() {
		pw.close();
		System.exit(0);
	}

	public static int readInt() throws IOException {
		return Integer.parseInt(nextToken());
	}

	public static double readDouble() throws IOException {
		return Double.parseDouble(nextToken());
	}

	public static long readLong() throws IOException {
		return Long.parseLong(nextToken());
	}

	public static String nextLine() throws IOException {
		st = null;
		if(!br.ready()) {
			exitImmediately();
		}
		return br.readLine();
	}

	public static String nextToken() throws IOException {
		while(st == null || !st.hasMoreTokens()) {
			if(!br.ready()) {
				exitImmediately();
			}
			st = new StringTokenizer(br.readLine());
		}
		return st.nextToken();
	}
}


import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.math.*;
import java.text.*;
import java.util.*;

public class C {
	static BufferedReader br;
	static StringTokenizer st;
	static PrintWriter pw;

	public static void main(String[] args) throws IOException {
		br = new BufferedReader(new InputStreamReader(System.in));
		pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
		//int qq = 1;
		int qq = Integer.MAX_VALUE;
		//int qq = readInt();
		for(int casenum = 1; casenum <= qq; casenum++) {
			int n = readInt();
			ArrayList<Inside> in = new ArrayList<Inside>();
			Map<Inside, Integer> rev = new HashMap<Inside, Integer>();
			for(int a = -1; a <= 8; a++) {
				for(int b = -1; b <= 8; b++) {
					for(int c = -1; c <= 8; c++) {
						for(int d = -1; d <= 8; d++) {
							in.add(new Inside(a, b, c, d));
							rev.put(new Inside(a, b, c, d), in.size()-1);
						}
					}
				}
			}
			int[][][] dp = new int[n+1][9][in.size()];
			for(int i = 0; i < dp.length; i++) {
				for(int j = 0; j < dp[i].length; j++) {
					Arrays.fill(dp[i][j], 1 << 30);
				}
			}
			int[] lhs = new int[n];
			int[] rhs = new int[n];
			for(int i = 0; i < n; i++) {
				lhs[i] = readInt()-1;
				rhs[i] = readInt()-1;
			}
			dp[0][0][0] = 0;
			LinkedList<State> q = new LinkedList<State>();
			q.add(new State(0, 0, 0));
			while(!q.isEmpty()) {
				State curr = q.removeFirst();
				if(curr.idx == n && curr.mask == 0) {
					pw.println(dp[curr.idx][curr.loc][curr.mask] + 2*n);
					break;
				}
				for(int dir = -1; dir <= 1; dir += 2) {
					int nextLoc = curr.loc + dir;
					if(nextLoc < 0 || nextLoc >= 9) continue;
					Inside now = in.get(curr.mask);
					now.clean(nextLoc);
					int nextIdx = curr.idx;
					int nextMask = rev.get(now);
					if(dp[nextIdx][nextLoc][nextMask] > 1 + dp[curr.idx][curr.loc][curr.mask]) {
						dp[nextIdx][nextLoc][nextMask] = 1 + dp[curr.idx][curr.loc][curr.mask];
						q.add(new State(nextIdx, nextLoc, nextMask));
					}
					while(nextIdx < n && lhs[nextIdx] == nextLoc && now.a == -1) {
						now.a = rhs[nextIdx];
						nextIdx++;
						now.sort();
						nextMask = rev.get(now);
						if(dp[nextIdx][nextLoc][nextMask] > 1 + dp[curr.idx][curr.loc][curr.mask]) {
							dp[nextIdx][nextLoc][nextMask] = 1 + dp[curr.idx][curr.loc][curr.mask];
							q.add(new State(nextIdx, nextLoc, nextMask));
						}
					}
				}
			}
		}
		pw.close();
	}

	static class Inside {
		public int a, b, c, d;

		public Inside(int a, int b, int c, int d) {
			super();
			this.a = a;
			this.b = b;
			this.c = c;
			this.d = d;
			sort();
		}

		public void clean(int idx) {
			if(idx==a) a=-1;
			if(idx==b) b=-1;
			if(idx==c) c=-1;
			if(idx==d) d=-1;
			sort();
		}

		public void sort() {
			if(d>c) {
				d^=c;
				c^=d;
				d^=c;
			}
			if(c>b) {
				c^=b;
				b^=c;
				c^=b;
			}
			if(b>a) {
				b^=a;
				a^=b;
				b^=a;
			}
			if(d>c) {
				d^=c;
				c^=d;
				d^=c;
			}
			if(c>b) {
				c^=b;
				b^=c;
				c^=b;
			}
			if(b>a) {
				b^=a;
				a^=b;
				b^=a;
			}
			if(d>c) {
				d^=c;
				c^=d;
				d^=c;
			}
			if(c>b) {
				c^=b;
				b^=c;
				c^=b;
			}
			if(b>a) {
				b^=a;
				a^=b;
				b^=a;
			}
		}

		@Override
		public int hashCode() {
			final int prime = 31;
			int result = 1;
			result = prime * result + a;
			result = prime * result + b;
			result = prime * result + c;
			result = prime * result + d;
			return result;
		}

		@Override
		public boolean equals(Object obj) {
			if (this == obj)
				return true;
			if (obj == null)
				return false;
			if (getClass() != obj.getClass())
				return false;
			Inside other = (Inside) obj;
			if (a != other.a)
				return false;
			if (b != other.b)
				return false;
			if (c != other.c)
				return false;
			if (d != other.d)
				return false;
			return true;
		}

	}

	static class State {
		public int idx, loc, mask;

		public State(int idx, int loc, int mask) {
			super();
			this.idx = idx;
			this.loc = loc;
			this.mask = mask;
		}

	}

	public static void exitImmediately() {
		pw.close();
		System.exit(0);
	}

	public static int readInt() throws IOException {
		return Integer.parseInt(nextToken());
	}

	public static double readDouble() throws IOException {
		return Double.parseDouble(nextToken());
	}

	public static long readLong() throws IOException {
		return Long.parseLong(nextToken());
	}

	public static String nextLine() throws IOException {
		st = null;
		if(!br.ready()) {
			exitImmediately();
		}
		return br.readLine();
	}

	public static String nextToken() throws IOException {
		while(st == null || !st.hasMoreTokens()) {
			if(!br.ready()) {
				exitImmediately();
			}
			st = new StringTokenizer(br.readLine());
		}
		return st.nextToken();
	}
}


import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.math.*;
import java.text.*;
import java.util.*;

public class C {
	static BufferedReader br;
	static StringTokenizer st;
	static PrintWriter pw;

	public static void main(String[] args) throws IOException {
		br = new BufferedReader(new InputStreamReader(System.in));
		pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
		//int qq = 1;
		int qq = Integer.MAX_VALUE;
		//int qq = readInt();
		for(int casenum = 1; casenum <= qq; casenum++) {
			int n = readInt();
			int[][][] dp = new int[n+1][9][512];
			for(int i = 0; i < dp.length; i++) {
				for(int j = 0; j < dp[i].length; j++) {
					Arrays.fill(dp[i][j], 1 << 30);
				}
			}
			int[] lhs = new int[n];
			int[] rhs = new int[n];
			for(int i = 0; i < n; i++) {
				lhs[i] = readInt()-1;
				rhs[i] = readInt()-1;
			}
			dp[0][0][0] = 0;
			LinkedList<State> q = new LinkedList<State>();
			q.add(new State(0, 0, 0));
			while(!q.isEmpty()) {
				State curr = q.removeFirst();
				if(curr.idx == n && curr.mask == 0) {
					pw.println(dp[curr.idx][curr.loc][curr.mask] + 2*n);
					break;
				}
				for(int dir = -1; dir <= 1; dir += 2) {
					int nextLoc = curr.loc + dir;
					if(nextLoc < 0 || nextLoc >= 9) continue;
					int nextMask = curr.mask & ~(1<<nextLoc);
					int nextIdx = curr.idx;
					while(nextIdx < n && lhs[nextIdx] == nextLoc) {
						nextMask |= 1 << rhs[nextIdx];
						nextIdx++;
					}
					if(dp[nextIdx][nextLoc][nextMask] > 1 + dp[curr.idx][curr.loc][curr.mask]) {
						dp[nextIdx][nextLoc][nextMask] = 1 + dp[curr.idx][curr.loc][curr.mask];
						q.add(new State(nextIdx, nextLoc, nextMask));
					}
				}
			}
		}
		pw.close();
	}

	static class State {
		public int idx, loc, mask;

		public State(int idx, int loc, int mask) {
			super();
			this.idx = idx;
			this.loc = loc;
			this.mask = mask;
		}

	}

	public static void exitImmediately() {
		pw.close();
		System.exit(0);
	}

	public static int readInt() throws IOException {
		return Integer.parseInt(nextToken());
	}

	public static double readDouble() throws IOException {
		return Double.parseDouble(nextToken());
	}

	public static long readLong() throws IOException {
		return Long.parseLong(nextToken());
	}

	public static String nextLine() throws IOException {
		st = null;
		if(!br.ready()) {
			exitImmediately();
		}
		return br.readLine();
	}

	public static String nextToken() throws IOException {
		while(st == null || !st.hasMoreTokens()) {
			if(!br.ready()) {
				exitImmediately();
			}
			st = new StringTokenizer(br.readLine());
		}
		return st.nextToken();
	}
}


import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.math.*;
import java.text.*;
import java.util.*;

public class C {
	static BufferedReader br;
	static StringTokenizer st;
	static PrintWriter pw;

	public static void main(String[] args) throws IOException {
		br = new BufferedReader(new InputStreamReader(System.in));
		pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
		//int qq = 1;
		int qq = Integer.MAX_VALUE;
		//int qq = readInt();
		for(int casenum = 1; casenum <= qq; casenum++) {
			int n = readInt();
			ArrayList<Inside> in = new ArrayList<Inside>();
			Map<Inside, Integer> rev = new HashMap<Inside, Integer>();
			for(int a = -1; a <= 8; a++) {
				for(int b = -1; b <= 8; b++) {
					for(int c = -1; c <= 8; c++) {
						for(int d = -1; d <= 8; d++) {
							in.add(new Inside(a, b, c, d));
							rev.put(new Inside(a, b, c, d), in.size()-1);
						}
					}
				}
			}
			int[][][] dp = new int[n+1][9][in.size()];
			for(int i = 0; i < dp.length; i++) {
				for(int j = 0; j < dp[i].length; j++) {
					Arrays.fill(dp[i][j], 1 << 30);
				}
			}
			int[] lhs = new int[n];
			int[] rhs = new int[n];
			for(int i = 0; i < n; i++) {
				lhs[i] = readInt()-1;
				rhs[i] = readInt()-1;
			}
			LinkedList<State> q = new LinkedList<State>();
			dp[0][0][0] = 0;
			q.add(new State(0, 0, 0));
			Inside now = new Inside(-1,-1,-1,-1);
			for(int i = 0; i < 4 && i < n && lhs[i] == 0; i++) {
				now.a = rhs[i];
				now.sort();
				dp[1][0][rev.get(now)] = 0;
				q.add(new State(1, 0, rev.get(now)));
			}
			while(!q.isEmpty()) {
				State curr = q.removeFirst();
				if(curr.idx == n && curr.mask == 0) {
					pw.println(dp[curr.idx][curr.loc][curr.mask] + 2*n);
					break;
				}
				for(int dir = -1; dir <= 1; dir += 2) {
					int nextLoc = curr.loc + dir;
					if(nextLoc < 0 || nextLoc >= 9) continue;
					now = new Inside(in.get(curr.mask));
					now.clean(nextLoc);
					int nextIdx = curr.idx;
					int nextMask = rev.get(now);
					if(dp[nextIdx][nextLoc][nextMask] > 1 + dp[curr.idx][curr.loc][curr.mask]) {
						dp[nextIdx][nextLoc][nextMask] = 1 + dp[curr.idx][curr.loc][curr.mask];
						q.add(new State(nextIdx, nextLoc, nextMask));
					}
					while(nextIdx < n && lhs[nextIdx] == nextLoc && now.a == -1) {
						now.a = rhs[nextIdx];
						nextIdx++;
						now.sort();
						nextMask = rev.get(now);
						if(dp[nextIdx][nextLoc][nextMask] > 1 + dp[curr.idx][curr.loc][curr.mask]) {
							dp[nextIdx][nextLoc][nextMask] = 1 + dp[curr.idx][curr.loc][curr.mask];
							q.add(new State(nextIdx, nextLoc, nextMask));
						}
					}
				}
			}
		}
		pw.close();
	}

	static class Inside {
		public int a, b, c, d;

		public Inside(Inside i) {
			this(i.a,i.b,i.c,i.d);
		}

		@Override
		public String toString() {
			return "Inside [a=" + a + ", b=" + b + ", c=" + c + ", d=" + d + "]";
		}

		public Inside(int a, int b, int c, int d) {
			super();
			this.a = a;
			this.b = b;
			this.c = c;
			this.d = d;
			sort();
		}

		public void clean(int idx) {
			if(idx==a) a=-1;
			if(idx==b) b=-1;
			if(idx==c) c=-1;
			if(idx==d) d=-1;
			sort();
		}

		public void sort() {
			if(d<c) {
				d^=c;
				c^=d;
				d^=c;
			}
			if(c<b) {
				c^=b;
				b^=c;
				c^=b;
			}
			if(b<a) {
				b^=a;
				a^=b;
				b^=a;
			}
			if(d<c) {
				d^=c;
				c^=d;
				d^=c;
			}
			if(c<b) {
				c^=b;
				b^=c;
				c^=b;
			}
			if(b<a) {
				b^=a;
				a^=b;
				b^=a;
			}
			if(d<c) {
				d^=c;
				c^=d;
				d^=c;
			}
			if(c<b) {
				c^=b;
				b^=c;
				c^=b;
			}
			if(b<a) {
				b^=a;
				a^=b;
				b^=a;
			}
		}

		@Override
		public int hashCode() {
			final int prime = 31;
			int result = 1;
			result = prime * result + a;
			result = prime * result + b;
			result = prime * result + c;
			result = prime * result + d;
			return result;
		}

		@Override
		public boolean equals(Object obj) {
			if (this == obj)
				return true;
			if (obj == null)
				return false;
			if (getClass() != obj.getClass())
				return false;
			Inside other = (Inside) obj;
			if (a != other.a)
				return false;
			if (b != other.b)
				return false;
			if (c != other.c)
				return false;
			if (d != other.d)
				return false;
			return true;
		}

	}

	static class State {
		public int idx, loc, mask;

		@Override
		public String toString() {
			return "State [idx=" + idx + ", loc=" + loc + ", mask=" + mask + "]";
		}

		public State(int idx, int loc, int mask) {
			super();
			this.idx = idx;
			this.loc = loc;
			this.mask = mask;
		}

	}

	public static void exitImmediately() {
		pw.close();
		System.exit(0);
	}

	public static int readInt() throws IOException {
		return Integer.parseInt(nextToken());
	}

	public static double readDouble() throws IOException {
		return Double.parseDouble(nextToken());
	}

	public static long readLong() throws IOException {
		return Long.parseLong(nextToken());
	}

	public static String nextLine() throws IOException {
		st = null;
		if(!br.ready()) {
			exitImmediately();
		}
		return br.readLine();
	}

	public static String nextToken() throws IOException {
		while(st == null || !st.hasMoreTokens()) {
			if(!br.ready()) {
				exitImmediately();
			}
			st = new StringTokenizer(br.readLine());
		}
		return st.nextToken();
	}
}


import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.math.*;
import java.text.*;
import java.util.*;

public class C {
	static BufferedReader br;
	static StringTokenizer st;
	static PrintWriter pw;

	public static void main(String[] args) throws IOException {
		br = new BufferedReader(new InputStreamReader(System.in));
		pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
		//int qq = 1;
		int qq = Integer.MAX_VALUE;
		//int qq = readInt();
		for(int casenum = 1; casenum <= qq; casenum++) {
			int n = readInt();
			ArrayList<Inside> in = new ArrayList<Inside>();
			Map<Inside, Integer> rev = new HashMap<Inside, Integer>();
			for(int a = -1; a <= 8; a++) {
				for(int b = -1; b <= 8; b++) {
					for(int c = -1; c <= 8; c++) {
						for(int d = -1; d <= 8; d++) {
							in.add(new Inside(a, b, c, d));
							rev.put(new Inside(a, b, c, d), in.size()-1);
						}
					}
				}
			}
			int[][][] dp = new int[n+1][9][in.size()];
			for(int i = 0; i < dp.length; i++) {
				for(int j = 0; j < dp[i].length; j++) {
					Arrays.fill(dp[i][j], 1 << 30);
				}
			}
			int[] lhs = new int[n];
			int[] rhs = new int[n];
			for(int i = 0; i < n; i++) {
				lhs[i] = readInt()-1;
				rhs[i] = readInt()-1;
			}
			dp[0][0][0] = 0;
			LinkedList<State> q = new LinkedList<State>();
			q.add(new State(0, 0, 0));
			while(!q.isEmpty()) {
				State curr = q.removeFirst();
				if(curr.idx == n && curr.mask == 0) {
					pw.println(dp[curr.idx][curr.loc][curr.mask] + 2*n);
					break;
				}
				for(int dir = -1; dir <= 1; dir += 2) {
					int nextLoc = curr.loc + dir;
					if(nextLoc < 0 || nextLoc >= 9) continue;
					Inside now = in.get(curr.mask);
					now.clean(nextLoc);
					int nextIdx = curr.idx;
					int nextMask = rev.get(now);
					if(dp[nextIdx][nextLoc][nextMask] > 1 + dp[curr.idx][curr.loc][curr.mask]) {
						dp[nextIdx][nextLoc][nextMask] = 1 + dp[curr.idx][curr.loc][curr.mask];
						q.add(new State(nextIdx, nextLoc, nextMask));
					}
					while(nextIdx < n && lhs[nextIdx] == nextLoc && now.a == -1) {
						now.a = rhs[nextIdx];
						nextIdx++;
						now.sort();
						nextMask = rev.get(now);
						if(dp[nextIdx][nextLoc][nextMask] > 1 + dp[curr.idx][curr.loc][curr.mask]) {
							dp[nextIdx][nextLoc][nextMask] = 1 + dp[curr.idx][curr.loc][curr.mask];
							q.add(new State(nextIdx, nextLoc, nextMask));
						}
					}
				}
			}
		}
		pw.close();
	}

	static class Inside {
		public int a, b, c, d;

		@Override
		public String toString() {
			return "Inside [a=" + a + ", b=" + b + ", c=" + c + ", d=" + d + "]";
		}

		public Inside(int a, int b, int c, int d) {
			super();
			this.a = a;
			this.b = b;
			this.c = c;
			this.d = d;
			sort();
		}

		public void clean(int idx) {
			if(idx==a) a=-1;
			if(idx==b) b=-1;
			if(idx==c) c=-1;
			if(idx==d) d=-1;
			sort();
		}

		public void sort() {
			if(d<c) {
				d^=c;
				c^=d;
				d^=c;
			}
			if(c<b) {
				c^=b;
				b^=c;
				c^=b;
			}
			if(b<a) {
				b^=a;
				a^=b;
				b^=a;
			}
			if(d<c) {
				d^=c;
				c^=d;
				d^=c;
			}
			if(c<b) {
				c^=b;
				b^=c;
				c^=b;
			}
			if(b<a) {
				b^=a;
				a^=b;
				b^=a;
			}
			if(d<c) {
				d^=c;
				c^=d;
				d^=c;
			}
			if(c<b) {
				c^=b;
				b^=c;
				c^=b;
			}
			if(b<a) {
				b^=a;
				a^=b;
				b^=a;
			}
		}

		@Override
		public int hashCode() {
			final int prime = 31;
			int result = 1;
			result = prime * result + a;
			result = prime * result + b;
			result = prime * result + c;
			result = prime * result + d;
			return result;
		}

		@Override
		public boolean equals(Object obj) {
			if (this == obj)
				return true;
			if (obj == null)
				return false;
			if (getClass() != obj.getClass())
				return false;
			Inside other = (Inside) obj;
			if (a != other.a)
				return false;
			if (b != other.b)
				return false;
			if (c != other.c)
				return false;
			if (d != other.d)
				return false;
			return true;
		}

	}

	static class State {
		public int idx, loc, mask;

		@Override
		public String toString() {
			return "State [idx=" + idx + ", loc=" + loc + ", mask=" + mask + "]";
		}

		public State(int idx, int loc, int mask) {
			super();
			this.idx = idx;
			this.loc = loc;
			this.mask = mask;
		}

	}

	public static void exitImmediately() {
		pw.close();
		System.exit(0);
	}

	public static int readInt() throws IOException {
		return Integer.parseInt(nextToken());
	}

	public static double readDouble() throws IOException {
		return Double.parseDouble(nextToken());
	}

	public static long readLong() throws IOException {
		return Long.parseLong(nextToken());
	}

	public static String nextLine() throws IOException {
		st = null;
		if(!br.ready()) {
			exitImmediately();
		}
		return br.readLine();
	}

	public static String nextToken() throws IOException {
		while(st == null || !st.hasMoreTokens()) {
			if(!br.ready()) {
				exitImmediately();
			}
			st = new StringTokenizer(br.readLine());
		}
		return st.nextToken();
	}
}


import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.math.*;
import java.text.*;
import java.util.*;

public class C {
	static BufferedReader br;
	static StringTokenizer st;
	static PrintWriter pw;

	public static void main(String[] args) throws IOException {
		br = new BufferedReader(new InputStreamReader(System.in));
		pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
		//int qq = 1;
		int qq = Integer.MAX_VALUE;
		//int qq = readInt();
		for(int casenum = 1; casenum <= qq; casenum++) {
			int n = readInt();
			ArrayList<Inside> in = new ArrayList<Inside>();
			Map<Inside, Integer> rev = new HashMap<Inside, Integer>();
			for(int a = 0; a <= 8; a++) {
				for(int b = a; b <= 8; b++) {
					for(int c = b; c <= 8; c++) {
						for(int d = c; d <= 8; d++) {
							in.add(new Inside(a, b, c, d));
							rev.put(new Inside(a, b, c, d), in.size()-1);
						}
					}
				}
			}
			int[][][] dp = new int[n+1][9][in.size()];
			for(int i = 0; i < dp.length; i++) {
				for(int j = 0; j < dp[i].length; j++) {
					Arrays.fill(dp[i][j], 1 << 30);
				}
			}
			int[] lhs = new int[n];
			int[] rhs = new int[n];
			for(int i = 0; i < n; i++) {
				lhs[i] = readInt()-1;
				rhs[i] = readInt()-1;
			}
			dp[0][0][0] = 0;
			LinkedList<State> q = new LinkedList<State>();
			q.add(new State(0, 0, 0));
			while(!q.isEmpty()) {
				State curr = q.removeFirst();
				if(curr.idx == n && curr.mask == 0) {
					pw.println(dp[curr.idx][curr.loc][curr.mask] + 2*n);
					break;
				}
				for(int dir = -1; dir <= 1; dir += 2) {
					int nextLoc = curr.loc + dir;
					if(nextLoc < 0 || nextLoc >= 9) continue;
					Inside now = in.get(curr.mask);
					now.clean(nextLoc);
					int nextIdx = curr.idx;
					while(nextIdx < n && lhs[nextIdx] == nextLoc && now.a == 0) {
						now.a = rhs[nextIdx];
						nextIdx++;
						now.sort();
					}
					int nextMask = rev.get(now);
					if(dp[nextIdx][nextLoc][nextMask] > 1 + dp[curr.idx][curr.loc][curr.mask]) {
						dp[nextIdx][nextLoc][nextMask] = 1 + dp[curr.idx][curr.loc][curr.mask];
						q.add(new State(nextIdx, nextLoc, nextMask));
					}
				}
			}
		}
		pw.close();
	}

	static class Inside {
		public int a, b, c, d;

		public Inside(int a, int b, int c, int d) {
			super();
			this.a = a;
			this.b = b;
			this.c = c;
			this.d = d;
			sort();
		}

		public void clean(int idx) {
			if(idx==a) a=0;
			if(idx==b) b=0;
			if(idx==c) c=0;
			if(idx==d) d=0;
			sort();
		}

		public void sort() {
			if(d>c) {
				d^=c;
				c^=d;
				d^=c;
			}
			if(c>b) {
				c^=b;
				b^=c;
				c^=b;
			}
			if(b>a) {
				b^=a;
				a^=b;
				b^=a;
			}
			if(d>c) {
				d^=c;
				c^=d;
				d^=c;
			}
			if(c>b) {
				c^=b;
				b^=c;
				c^=b;
			}
			if(b>a) {
				b^=a;
				a^=b;
				b^=a;
			}
			if(d>c) {
				d^=c;
				c^=d;
				d^=c;
			}
			if(c>b) {
				c^=b;
				b^=c;
				c^=b;
			}
			if(b>a) {
				b^=a;
				a^=b;
				b^=a;
			}
		}

		@Override
		public int hashCode() {
			final int prime = 31;
			int result = 1;
			result = prime * result + a;
			result = prime * result + b;
			result = prime * result + c;
			result = prime * result + d;
			return result;
		}

		@Override
		public boolean equals(Object obj) {
			if (this == obj)
				return true;
			if (obj == null)
				return false;
			if (getClass() != obj.getClass())
				return false;
			Inside other = (Inside) obj;
			if (a != other.a)
				return false;
			if (b != other.b)
				return false;
			if (c != other.c)
				return false;
			if (d != other.d)
				return false;
			return true;
		}

	}

	static class State {
		public int idx, loc, mask;

		public State(int idx, int loc, int mask) {
			super();
			this.idx = idx;
			this.loc = loc;
			this.mask = mask;
		}

	}

	public static void exitImmediately() {
		pw.close();
		System.exit(0);
	}

	public static int readInt() throws IOException {
		return Integer.parseInt(nextToken());
	}

	public static double readDouble() throws IOException {
		return Double.parseDouble(nextToken());
	}

	public static long readLong() throws IOException {
		return Long.parseLong(nextToken());
	}

	public static String nextLine() throws IOException {
		st = null;
		if(!br.ready()) {
			exitImmediately();
		}
		return br.readLine();
	}

	public static String nextToken() throws IOException {
		while(st == null || !st.hasMoreTokens()) {
			if(!br.ready()) {
				exitImmediately();
			}
			st = new StringTokenizer(br.readLine());
		}
		return st.nextToken();
	}
}


import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.math.*;
import java.text.*;
import java.util.*;

public class C {
	static BufferedReader br;
	static StringTokenizer st;
	static PrintWriter pw;

	public static void main(String[] args) throws IOException {
		br = new BufferedReader(new InputStreamReader(System.in));
		pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
		//int qq = 1;
		int qq = Integer.MAX_VALUE;
		//int qq = readInt();
		for(int casenum = 1; casenum <= qq; casenum++) {
			int n = readInt();
			ArrayList<Inside> in = new ArrayList<Inside>();
			Map<Inside, Integer> rev = new HashMap<Inside, Integer>();
			for(int a = -1; a <= 8; a++) {
				for(int b = -1; b <= 8; b++) {
					for(int c = -1; c <= 8; c++) {
						for(int d = -1; d <= 8; d++) {
							in.add(new Inside(a, b, c, d));
							rev.put(new Inside(a, b, c, d), in.size()-1);
						}
					}
				}
			}
			int[][][] dp = new int[n+1][9][in.size()];
			for(int i = 0; i < dp.length; i++) {
				for(int j = 0; j < dp[i].length; j++) {
					Arrays.fill(dp[i][j], 1 << 30);
				}
			}
			int[] lhs = new int[n];
			int[] rhs = new int[n];
			for(int i = 0; i < n; i++) {
				lhs[i] = readInt()-1;
				rhs[i] = readInt()-1;
			}
			dp[0][0][0] = 0;
			LinkedList<State> q = new LinkedList<State>();
			q.add(new State(0, 0, 0));
			while(!q.isEmpty()) {
				State curr = q.removeFirst();
				if(curr.idx == n && curr.mask == 0) {
					pw.println(dp[curr.idx][curr.loc][curr.mask] + 2*n);
					break;
				}
				for(int dir = -1; dir <= 1; dir += 2) {
					int nextLoc = curr.loc + dir;
					if(nextLoc < 0 || nextLoc >= 9) continue;
					Inside now = in.get(curr.mask);
					now.clean(nextLoc);
					int nextIdx = curr.idx;
					while(nextIdx < n && lhs[nextIdx] == nextLoc && now.a == -1) {
						now.a = rhs[nextIdx];
						nextIdx++;
						now.sort();
					}
					int nextMask = rev.get(now);
					if(dp[nextIdx][nextLoc][nextMask] > 1 + dp[curr.idx][curr.loc][curr.mask]) {
						dp[nextIdx][nextLoc][nextMask] = 1 + dp[curr.idx][curr.loc][curr.mask];
						q.add(new State(nextIdx, nextLoc, nextMask));
					}
				}
			}
		}
		pw.close();
	}

	static class Inside {
		public int a, b, c, d;

		public Inside(int a, int b, int c, int d) {
			super();
			this.a = a;
			this.b = b;
			this.c = c;
			this.d = d;
			sort();
		}

		public void clean(int idx) {
			if(idx==a) a=-1;
			if(idx==b) b=-1;
			if(idx==c) c=-1;
			if(idx==d) d=-1;
			sort();
		}

		public void sort() {
			if(d>c) {
				d^=c;
				c^=d;
				d^=c;
			}
			if(c>b) {
				c^=b;
				b^=c;
				c^=b;
			}
			if(b>a) {
				b^=a;
				a^=b;
				b^=a;
			}
			if(d>c) {
				d^=c;
				c^=d;
				d^=c;
			}
			if(c>b) {
				c^=b;
				b^=c;
				c^=b;
			}
			if(b>a) {
				b^=a;
				a^=b;
				b^=a;
			}
			if(d>c) {
				d^=c;
				c^=d;
				d^=c;
			}
			if(c>b) {
				c^=b;
				b^=c;
				c^=b;
			}
			if(b>a) {
				b^=a;
				a^=b;
				b^=a;
			}
		}

		@Override
		public int hashCode() {
			final int prime = 31;
			int result = 1;
			result = prime * result + a;
			result = prime * result + b;
			result = prime * result + c;
			result = prime * result + d;
			return result;
		}

		@Override
		public boolean equals(Object obj) {
			if (this == obj)
				return true;
			if (obj == null)
				return false;
			if (getClass() != obj.getClass())
				return false;
			Inside other = (Inside) obj;
			if (a != other.a)
				return false;
			if (b != other.b)
				return false;
			if (c != other.c)
				return false;
			if (d != other.d)
				return false;
			return true;
		}

	}

	static class State {
		public int idx, loc, mask;

		public State(int idx, int loc, int mask) {
			super();
			this.idx = idx;
			this.loc = loc;
			this.mask = mask;
		}

	}

	public static void exitImmediately() {
		pw.close();
		System.exit(0);
	}

	public static int readInt() throws IOException {
		return Integer.parseInt(nextToken());
	}

	public static double readDouble() throws IOException {
		return Double.parseDouble(nextToken());
	}

	public static long readLong() throws IOException {
		return Long.parseLong(nextToken());
	}

	public static String nextLine() throws IOException {
		st = null;
		if(!br.ready()) {
			exitImmediately();
		}
		return br.readLine();
	}

	public static String nextToken() throws IOException {
		while(st == null || !st.hasMoreTokens()) {
			if(!br.ready()) {
				exitImmediately();
			}
			st = new StringTokenizer(br.readLine());
		}
		return st.nextToken();
	}
}


import java.io.*;
import java.util.*;
import java.util.stream.Collectors;
import java.math.*;

public class C {
	public static void main(String[] args) throws IOException {

		/**/
		Scanner sc = new Scanner(new BufferedReader(new InputStreamReader(System.in)));
		/*/
		Scanner sc = new Scanner(new BufferedReader(new InputStreamReader(new FileInputStream("src/c.in"))));
		/**/

		int n = sc.nextInt();
		int[] a = new int[n];
		int[] b = new int[n];
		for (int i = 0; i < n; i++) {
			a[i] = sc.nextInt();
			b[i] = sc.nextInt();
		}
		int[][][][][][] dp = new int[n+1][10][10][10][10][10];
		PriorityQueue<Long> pq = new PriorityQueue<>();
		pq.add(1L<<16);
		while (!pq.isEmpty()) {
			long rem = pq.poll();
			int dist = (int)(rem>>32);
			int rest = (int)(rem<<32>>32);
			int emp = rest>>20;
			int floor = rest<<12>>28;
			int[] dests = {rest<<16>>28, rest<<20>>28, rest<<24>>28, rest<<28>>28};
			if (dist != dp[emp][floor][dests[0]][dests[1]][dests[2]][dests[3]])
				continue;
			if (dests[3]==0&&emp<n) {
				int[] nde = dests.clone();
				for (int i = 0; i < 3; i++) {
					if (nde[i]==a[emp])
						nde[i] = 0;
				}
				int nemp = emp;
				while (nde[3]==0&&nemp<n&&a[nemp]==a[emp]) {
					nde[3] = b[nemp++];
					Arrays.sort(nde);
				}
				int ndi = dist+Math.abs(a[emp]-floor);
				if (dp[nemp][a[emp]][nde[0]][nde[1]][nde[2]][nde[3]] == 0 || ndi < dp[nemp][a[emp]][nde[0]][nde[1]][nde[2]][nde[3]]) {
					dp[nemp][a[emp]][nde[0]][nde[1]][nde[2]][nde[3]] = ndi;
					pq.add((((long)ndi)<<32)+(nemp<<20)+(a[emp]<<16)+(nde[0]<<12)+(nde[1]<<8)+(nde[2]<<4)+(nde[3]));
				}
			}
			for (int i = 0; i < 4; i++) {
				int nfl = dests[i];
				int[] nde = dests.clone();
				for (int j = 0; j < 4; j++) {
					if (nde[i]==nfl)
						nde[i] = 0;
				}
				Arrays.sort(nde);
				int ndi = dist+Math.abs(nfl-floor);
				if (dp[emp][nfl][nde[0]][nde[1]][nde[2]][nde[3]] == 0 || ndi < dp[emp][nfl][nde[0]][nde[1]][nde[2]][nde[3]]) {
					dp[emp][nfl][nde[0]][nde[1]][nde[2]][nde[3]] = ndi;
					pq.add((((long)ndi)<<32)+(emp<<20)+(nfl<<16)+(nde[0]<<12)+(nde[1]<<8)+(nde[2]<<4)+(nde[3]));
				}
			}
		}
		int min = Integer.MAX_VALUE;
		for (int i = 1; i < 10; i++) {
			if (dp[n][i][0][0][0][0]>0)
				min = Math.min(min, dp[n][i][0][0][0][0]);
		}
		System.out.println(min+2*n);
	}
}


import java.io.*;
import java.util.*;
import java.util.stream.Collectors;
import java.math.*;

public class C {
	public static void main(String[] args) throws IOException {

		/**/
		Scanner sc = new Scanner(new BufferedReader(new InputStreamReader(System.in)));
		/*/
		Scanner sc = new Scanner(new BufferedReader(new InputStreamReader(new FileInputStream("src/c.in"))));
		/**/

		int n = sc.nextInt();
		int[] a = new int[n];
		int[] b = new int[n];
		for (int i = 0; i < n; i++) {
			a[i] = sc.nextInt();
			b[i] = sc.nextInt();
		}
		HashMap<Long, Integer> dp = new HashMap<Long, Integer>();
		PriorityQueue<Long> pq = new PriorityQueue<>();
		pq.add(1L<<16);
		dp.put(1L<<16, 0);
		while (!pq.isEmpty()) {
			long rem = pq.poll();
			int dist = (int)(rem>>>32);
			if (dist != dp.get(rem<<32>>>32))
				continue;
			int emp = (int)(rem<<32>>>52);
			int rest = (int)(rem<<44>>>44);
			int floor = rest>>>16;
			int[] dests = {rest<<16>>>28, rest<<20>>>28, rest<<24>>>28, rest<<28>>>28};
			if (dests[0]==0&&emp<n) {
				int[] nde = dests.clone();
				for (int i = 0; i < 3; i++) {
					if (nde[i]==a[emp])
						nde[i] = 0;
				}
				int nemp = emp;
				while (nde[0]==0&&nemp<n&&a[nemp]==a[emp]) {
					nde[0] = b[nemp++];
					Arrays.sort(nde);
				}
				int ndi = dist+Math.abs(a[emp]-floor);
				long key = (long)(nemp<<20)+(a[emp]<<16)+(nde[0]<<12)+(nde[1]<<8)+(nde[2]<<4)+(nde[3]);
				if (!dp.containsKey(key) || ndi < dp.get(key)) {
					dp.put(key, ndi);
					pq.add((((long)ndi)<<32)+key);
				}
			}
			for (int i = 0; i < 4; i++) {
				if (dests[i]==0)
					continue;
				int nfl = dests[i];
				int[] nde = dests.clone();
				for (int j = i; j < 4; j++) {
					if (nde[i]==nfl)
						nde[i] = 0;
					else
						break;
				}
				Arrays.sort(nde);
				int ndi = dist+Math.abs(nfl-floor);
				long key = (long)(emp<<20)+(nfl<<16)+(nde[0]<<12)+(nde[1]<<8)+(nde[2]<<4)+(nde[3]);
				if (!dp.containsKey(key) || ndi < dp.get(key)) {
					dp.put(key, ndi);
					pq.add((((long)ndi)<<32)+key);
				}
			}
		}
		int min = Integer.MAX_VALUE;
		for (int i = 1; i < 10; i++) {
			if (dp.containsKey((long)(n<<20)+(i<<16)))
				min = Math.min(min, dp.get((long)(n<<20)+(i<<16)));
		}
		System.out.println(min+2*n);
	}
}


import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.io.BufferedWriter;
import java.io.Writer;
import java.io.OutputStreamWriter;
import java.util.InputMismatchException;
import java.io.IOException;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 *
 * @author Egor Kulikov (egor@egork.net)
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        OutputWriter out = new OutputWriter(outputStream);
        CLift solver = new CLift();
        solver.solve(1, in, out);
        out.close();
    }

    static class CLift {
        int[] a;
        int[] b;
        int[][][][][][] result;

        public void solve(int testNumber, InputReader in, OutputWriter out) {
            int n = in.readInt();
            a = new int[n];
            b = new int[n];
            in.readIntArrays(a, b);
            result = new int[9][n + 1][10][][][];
            for (int i = 0; i < 9; i++) {
                for (int j = 0; j <= n; j++) {
                    for (int k = 0; k < 10; k++) {
                        result[i][j][k] = new int[k + 1][][];
                        for (int l = 0; l <= k; l++) {
                            result[i][j][k][l] = new int[l + 1][];
                            for (int m = 0; m <= l; m++) {
                                result[i][j][k][l][m] = ArrayUtils.createArray(m + 1, -1);
                            }
                        }
                    }
                }
            }
            out.printLine(go(0, 0, 0, 0, 0, 0) + 2 * n);
        }

        private int go(int floor, int id, int e1, int e2, int e3, int e4) {
            if (result[floor][id][e1][e2][e3][e4] != -1) {
                return result[floor][id][e1][e2][e3][e4];
            }
            if (id == a.length && e1 == 0) {
                return result[floor][id][e1][e2][e3][e4] = 0;
            }
            result[floor][id][e1][e2][e3][e4] = Integer.MAX_VALUE;
            if (e1 != 0) {
                result[floor][id][e1][e2][e3][e4] = Math.min(result[floor][id][e1][e2][e3][e4], go(e1 - 1, id, e2, e3,
                        e4, 0) + Math.abs(floor + 1 - e1));
                if (e2 != 0) {
                    result[floor][id][e1][e2][e3][e4] =
                            Math.min(result[floor][id][e1][e2][e3][e4], go(e2 - 1, id, e1, e3,
                                    e4, 0) + Math.abs(floor + 1 - e2));
                    if (e3 != 0) {
                        result[floor][id][e1][e2][e3][e4] = Math.min(result[floor][id][e1][e2][e3][e4], go(e3 - 1, id,
                                e1, e2, e4, 0) + Math.abs(floor + 1 - e3));
                        if (e4 != 0) {
                            result[floor][id][e1][e2][e3][e4] =
                                    Math.min(result[floor][id][e1][e2][e3][e4], go(e4 - 1, id,
                                            e1, e2, e3, 0) + Math.abs(floor + 1 - e4));
                        }
                    }
                }
            }
            if (id != a.length && e4 == 0) {
                if (b[id] >= e1) {
                    result[floor][id][e1][e2][e3][e4] =
                            Math.min(result[floor][id][e1][e2][e3][e4], go(a[id] - 1, id + 1,
                                    b[id], e1, e2, e3) + Math.abs(floor + 1 - a[id]));
                } else if (b[id] >= e2) {
                    result[floor][id][e1][e2][e3][e4] =
                            Math.min(result[floor][id][e1][e2][e3][e4], go(a[id] - 1, id + 1,
                                    e1, b[id], e2, e3) + Math.abs(floor + 1 - a[id]));
                } else if (b[id] >= e3) {
                    result[floor][id][e1][e2][e3][e4] =
                            Math.min(result[floor][id][e1][e2][e3][e4], go(a[id] - 1, id + 1,
                                    e1, e2, b[id], e3) + Math.abs(floor + 1 - a[id]));
                } else {
                    result[floor][id][e1][e2][e3][e4] =
                            Math.min(result[floor][id][e1][e2][e3][e4], go(a[id] - 1, id + 1,
                                    e1, e2, e3, b[id]) + Math.abs(floor + 1 - a[id]));
                }
            }
            return result[floor][id][e1][e2][e3][e4];
        }

    }

    static class OutputWriter {
        private final PrintWriter writer;

        public OutputWriter(OutputStream outputStream) {
            writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
        }

        public OutputWriter(Writer writer) {
            this.writer = new PrintWriter(writer);
        }

        public void close() {
            writer.close();
        }

        public void printLine(int i) {
            writer.println(i);
        }

    }

    static class ArrayUtils {
        public static int[] createArray(int count, int value) {
            int[] array = new int[count];
            Arrays.fill(array, value);
            return array;
        }

    }

    static class InputReader {
        private InputStream stream;
        private byte[] buf = new byte[1024];
        private int curChar;
        private int numChars;
        private InputReader.SpaceCharFilter filter;

        public InputReader(InputStream stream) {
            this.stream = stream;
        }

        public void readIntArrays(int[]... arrays) {
            for (int i = 0; i < arrays[0].length; i++) {
                for (int j = 0; j < arrays.length; j++) {
                    arrays[j][i] = readInt();
                }
            }
        }

        public int read() {
            if (numChars == -1) {
                throw new InputMismatchException();
            }
            if (curChar >= numChars) {
                curChar = 0;
                try {
                    numChars = stream.read(buf);
                } catch (IOException e) {
                    throw new InputMismatchException();
                }
                if (numChars <= 0) {
                    return -1;
                }
            }
            return buf[curChar++];
        }

        public int readInt() {
            int c = read();
            while (isSpaceChar(c)) {
                c = read();
            }
            int sgn = 1;
            if (c == '-') {
                sgn = -1;
                c = read();
            }
            int res = 0;
            do {
                if (c < '0' || c > '9') {
                    throw new InputMismatchException();
                }
                res *= 10;
                res += c - '0';
                c = read();
            } while (!isSpaceChar(c));
            return res * sgn;
        }

        public boolean isSpaceChar(int c) {
            if (filter != null) {
                return filter.isSpaceChar(c);
            }
            return isWhitespace(c);
        }

        public static boolean isWhitespace(int c) {
            return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
        }

        public interface SpaceCharFilter {
            public boolean isSpaceChar(int ch);

        }

    }
}


//package com.company;

import java.io.*;
import java.math.BigInteger;
import java.util.*;

public class Main {
    static long TIME_START, TIME_END;
    public static void main(String[] args) throws IOException {
        Scanner sc = new Scanner(System.in);
//        Scanner sc = new Scanner(new FileInputStream("Test.in"));
        PrintWriter pw = new PrintWriter(System.out);
//        PrintWriter pw = new PrintWriter(new FileOutputStream("Test.out"));
//        PrintWriter pw = new PrintWriter(new FileOutputStream("Test.in"));
//        pw.println(5000);
//        for (int i = 0; i < 5000; i++) {
//            pw.print(i + " ");
//        }
//        pw.println();
//        pw.println(100000);
//        for (int i = 0; i < 100000; i++) {
//            pw.println(1 + " " + 5000);
//        }
        Runtime runtime = Runtime.getRuntime();
        long usedMemoryBefore = runtime.totalMemory() - runtime.freeMemory();
        TIME_START = System.currentTimeMillis();
        Task t = new Task();
        t.solve(sc, pw);

        TIME_END = System.currentTimeMillis();
        long usedMemoryAfter = runtime.totalMemory() - runtime.freeMemory();
        pw.close();
//        System.out.println("Memory increased:" + (usedMemoryAfter-usedMemoryBefore) / 1000000 );
//        System.out.println("Time used: " + (TIME_END - TIME_START) + ".");
    }

    public static class Task {
        public class Config{
            int[] toStop;
            int time;
            int in;
            Config(){
                toStop = new int[9];
                time = 0;
                in = 0;
            }

            Config copyOf(){
                Config cp = new Config();
                cp.time = time;
                cp.in = in;
                cp.toStop = Arrays.copyOf(toStop, 9);
                return cp;
            }

            @Override
            public boolean equals(Object o) {
                if (this == o) return true;
                if (!(o instanceof Config)) return false;
                Config config = (Config) o;
                return Arrays.equals(toStop, config.toStop);
            }

            @Override
            public int hashCode() {
                return Arrays.hashCode(toStop);
            }
        }

        public void unlabel(Config cf, int from, int to){
            if (from > to) {
                for (int i = from; i >= to; i--) {
                    cf.in -= cf.toStop[i];
                    cf.toStop[i] = 0;
                }
                cf.time += from - to;
            } else {
                for (int i = from; i <= to; i++) {
                    cf.in -= cf.toStop[i];
                    cf.toStop[i] = 0;
                }
                cf.time += to - from;
            }

        }

        public int minNeed(Config cf, int at) {
            if (cf.in == 0) return 0;
            int minLevel = 10, maxLevel = 0;
            for (int i = 0; i < 9; i++) {
                if (cf.toStop[i] != 0) {
                    minLevel = Math.min(minLevel, i);
                    maxLevel = Math.max(maxLevel, i);
                }
            }
            int moveDown = minLevel < at ? at - minLevel : 0;
            int moveUp = maxLevel > at ? maxLevel - at: 0;
            return Math.min(moveDown, moveUp) + moveDown + moveUp;
        }

        public void configure(int at, int next, Config config, Map<Config, Config> all) {

            Config cofUp = config.copyOf();
            for (int i = at; i < 9; i++) {
                if (cofUp.toStop[i] != 0) {
                    cofUp.in -= cofUp.toStop[i];
                    cofUp.toStop[i] = 0;
                    Config toAdd = cofUp.copyOf();
                    unlabel(toAdd, i, next);
                    if (all.containsKey(toAdd)) {
                        all.get(toAdd).time = Math.min(all.get(toAdd).time, toAdd.time);
                    } else {
                        all.put(toAdd, toAdd);
                    }
                }
                cofUp.time++;
            }

            Config cofDown = config.copyOf();
            for (int i = at; i >= 0; i--) {
                if (cofDown.toStop[i] != 0) {
                    cofDown.in -= cofDown.toStop[i];
                    cofDown.toStop[i] = 0;
                    Config toAdd = cofDown.copyOf();
                    unlabel(toAdd, i, next);
                    if (all.containsKey(toAdd)) {
                        all.get(toAdd).time = Math.min(all.get(toAdd).time, toAdd.time);
                    } else {
                        all.put(toAdd, toAdd);
                    }
                }
                cofDown.time++;
            }
            return;
        }

        public void solve(Scanner sc, PrintWriter pw) throws IOException {
            Map<Config, Config> all = new HashMap<>();
            int n = sc.nextInt();
            Config inital = new Config();
            all.put(inital, inital);
            int[] from = new int[n + 1];
            int[] to = new int[n + 1];
            for (int i = 0; i < n; i++) {
                from[i] = sc.nextInt() - 1;
                to[i] = sc.nextInt() - 1;
            }
            from[n] = from[n - 1];
            to[n] = to[n - 1];
            inital.time += from[0];
            for (int i = 0; i < n - 1; i++) {
                Map<Config, Config> next = new HashMap<>();
                for(Config conf: all.keySet()) {
                    conf.toStop[to[i]]++;
                    conf.in++;
                    configure(from[i], from[i + 1], conf, next);
                }
                all = next;
            }
            int minTime = Integer.MAX_VALUE;
            for(Config conf: all.keySet()) {
                conf.toStop[to[n - 1]]++;
                conf.in++;
                int res = minNeed(conf, from[n - 1]) + conf.time;
                minTime = Math.min(minTime, res);
            }
            pw.println(minTime + 2 * n);
        }

    }

    static class Scanner {
        StringTokenizer st;
        BufferedReader br;

        public Scanner(InputStream s){  br = new BufferedReader(new InputStreamReader(s));}

        public Scanner(FileReader s) throws FileNotFoundException {br = new BufferedReader(s);}

        public String next() throws IOException
        {
            while (st == null || !st.hasMoreTokens())
                st = new StringTokenizer(br.readLine());
            return st.nextToken();
        }

        public int nextInt() throws IOException {return Integer.parseInt(next());}

        public long nextLong() throws IOException {return Long.parseLong(next());}

        public String nextLine() throws IOException {return br.readLine();}

        public double nextDouble() throws IOException { return Double.parseDouble(next()); }

        public boolean ready() throws IOException {return br.ready();}
    }
}


//package com.company;

import java.io.*;
import java.math.BigInteger;
import java.util.*;

public class Main {
    static long TIME_START, TIME_END;
    public static void main(String[] args) throws IOException {
        Scanner sc = new Scanner(System.in);
//        Scanner sc = new Scanner(new FileInputStream("Test.in"));
        PrintWriter pw = new PrintWriter(System.out);
//        PrintWriter pw = new PrintWriter(new FileOutputStream("Test.out"));
//        PrintWriter pw = new PrintWriter(new FileOutputStream("Test.in"));
//        pw.println(5000);
//        for (int i = 0; i < 5000; i++) {
//            pw.print(i + " ");
//        }
//        pw.println();
//        pw.println(100000);
//        for (int i = 0; i < 100000; i++) {
//            pw.println(1 + " " + 5000);
//        }
        Runtime runtime = Runtime.getRuntime();
        long usedMemoryBefore = runtime.totalMemory() - runtime.freeMemory();
        TIME_START = System.currentTimeMillis();
        Task t = new Task();
        t.solve(sc, pw);

        TIME_END = System.currentTimeMillis();
        long usedMemoryAfter = runtime.totalMemory() - runtime.freeMemory();
        pw.close();
//        System.out.println("Memory increased:" + (usedMemoryAfter-usedMemoryBefore) / 1000000 );
//        System.out.println("Time used: " + (TIME_END - TIME_START) + ".");
    }

    public static class Task {
        public class Config{
            int[] toStop;
            int time;
            int in;
            Config(){
                toStop = new int[9];
                time = 0;
                in = 0;
            }

            Config copyOf(){
                Config cp = new Config();
                cp.time = time;
                cp.in = in;
                cp.toStop = Arrays.copyOf(toStop, 9);
                return cp;
            }

            @Override
            public boolean equals(Object o) {
                if (this == o) return true;
                if (!(o instanceof Config)) return false;
                Config config = (Config) o;
                return Arrays.equals(toStop, config.toStop);
            }

            @Override
            public int hashCode() {
                return Arrays.hashCode(toStop);
            }
        }

        public void unlabel(Config cf, int from, int to){
            if (from > to) {
                for (int i = from; i >= to; i--) {
                    cf.in -= cf.toStop[i];
                    cf.toStop[i] = 0;
                }
                cf.time += from - to;
            } else {
                for (int i = from; i <= to; i++) {
                    cf.in -= cf.toStop[i];
                    cf.toStop[i] = 0;
                }
                cf.time += to - from;
            }

        }

        public int minNeed(Config cf, int at) {
            if (cf.in == 0) return 0;
            int minLevel = 10, maxLevel = 0;
            for (int i = 0; i < 9; i++) {
                if (cf.toStop[i] != 0) {
                    minLevel = Math.min(minLevel, i);
                    maxLevel = Math.max(maxLevel, i);
                }
            }
            int moveDown = minLevel < at ? at - minLevel : 0;
            int moveUp = maxLevel > at ? maxLevel - at: 0;
            return Math.min(moveDown, moveUp) + moveDown + moveUp;
        }

        public void configure(int at, int next, Config config, Map<Config, Config> all) {
            if (config.in < 4) {
                Config init = config.copyOf();
                unlabel(init, at, next);
                if (all.containsKey(init)) {
                    all.get(init).time = Math.min(all.get(init).time, init.time);
                } else {
                    all.put(init, init);
                }
            }
            Config cofUp = config.copyOf();
            for (int i = at; i < 9; i++) {
                if (cofUp.toStop[i] != 0) {
                    cofUp.in -= cofUp.toStop[i];
                    cofUp.toStop[i] = 0;
                    Config toAdd = cofUp.copyOf();
                    unlabel(toAdd, i, next);
                    if (all.containsKey(toAdd)) {
                        all.get(toAdd).time = Math.min(all.get(toAdd).time, toAdd.time);
                    } else {
                        all.put(toAdd, toAdd);
                    }
                }
                cofUp.time++;
            }

            Config cofDown = config.copyOf();
            for (int i = at; i >= 0; i--) {
                if (cofDown.toStop[i] != 0) {
                    cofDown.in -= cofDown.toStop[i];
                    cofDown.toStop[i] = 0;
                    Config toAdd = cofDown.copyOf();
                    unlabel(toAdd, i, next);
                    if (all.containsKey(toAdd)) {
                        all.get(toAdd).time = Math.min(all.get(toAdd).time, toAdd.time);
                    } else {
                        all.put(toAdd, toAdd);
                    }
                }
                cofDown.time++;
            }
            return;
        }

        public void solve(Scanner sc, PrintWriter pw) throws IOException {
            Map<Config, Config> all = new HashMap<>();
            int n = sc.nextInt();
            Config inital = new Config();
            all.put(inital, inital);
            int[] from = new int[n + 1];
            int[] to = new int[n + 1];
            for (int i = 0; i < n; i++) {
                from[i] = sc.nextInt() - 1;
                to[i] = sc.nextInt() - 1;
            }
            from[n] = from[n - 1];
            to[n] = to[n - 1];
            inital.time += from[0];
            for (int i = 0; i < n - 1; i++) {
                Map<Config, Config> next = new HashMap<>();
                for(Config conf: all.keySet()) {
                    conf.toStop[to[i]]++;
                    conf.in++;
                    configure(from[i], from[i + 1], conf, next);
                }
                all = next;
            }
            int minTime = Integer.MAX_VALUE;
            for(Config conf: all.keySet()) {
                conf.toStop[to[n - 1]]++;
                conf.in++;
                int res = minNeed(conf, from[n - 1]) + conf.time;
                minTime = Math.min(minTime, res);
            }
            pw.println(minTime + 2 * n);
        }

    }

    static class Scanner {
        StringTokenizer st;
        BufferedReader br;

        public Scanner(InputStream s){  br = new BufferedReader(new InputStreamReader(s));}

        public Scanner(FileReader s) throws FileNotFoundException {br = new BufferedReader(s);}

        public String next() throws IOException
        {
            while (st == null || !st.hasMoreTokens())
                st = new StringTokenizer(br.readLine());
            return st.nextToken();
        }

        public int nextInt() throws IOException {return Integer.parseInt(next());}

        public long nextLong() throws IOException {return Long.parseLong(next());}

        public String nextLine() throws IOException {return br.readLine();}

        public double nextDouble() throws IOException { return Double.parseDouble(next()); }

        public boolean ready() throws IOException {return br.ready();}
    }
}


//package com.company;

import java.io.*;
import java.math.BigInteger;
import java.util.*;

public class Main {
    static long TIME_START, TIME_END;
    public static void main(String[] args) throws IOException {
        Scanner sc = new Scanner(System.in);
//        Scanner sc = new Scanner(new FileInputStream("Test.in"));
        PrintWriter pw = new PrintWriter(System.out);
//        PrintWriter pw = new PrintWriter(new FileOutputStream("Test.out"));
//        PrintWriter pw = new PrintWriter(new FileOutputStream("Test.in"));
//        pw.println(5000);
//        for (int i = 0; i < 5000; i++) {
//            pw.print(i + " ");
//        }
//        pw.println();
//        pw.println(100000);
//        for (int i = 0; i < 100000; i++) {
//            pw.println(1 + " " + 5000);
//        }
        Runtime runtime = Runtime.getRuntime();
        long usedMemoryBefore = runtime.totalMemory() - runtime.freeMemory();
        TIME_START = System.currentTimeMillis();
        Task t = new Task();
        t.solve(sc, pw);

        TIME_END = System.currentTimeMillis();
        long usedMemoryAfter = runtime.totalMemory() - runtime.freeMemory();
        pw.close();
//        System.out.println("Memory increased:" + (usedMemoryAfter-usedMemoryBefore) / 1000000 );
//        System.out.println("Time used: " + (TIME_END - TIME_START) + ".");
    }

    public static class Task {
        public class Config{
            int[] toStop;
            int time;
            int in;
            Config(){
                toStop = new int[9];
                time = 0;
                in = 0;
            }

            Config copyOf(){
                Config cp = new Config();
                cp.time = time;
                cp.in = in;
                cp.toStop = Arrays.copyOf(toStop, 9);
                return cp;
            }

            @Override
            public boolean equals(Object o) {
                if (this == o) return true;
                if (!(o instanceof Config)) return false;
                Config config = (Config) o;
                return Arrays.equals(toStop, config.toStop);
            }

            @Override
            public int hashCode() {
                return Arrays.hashCode(toStop);
            }
        }

        public void configure(int at, int next, Config config, Map<Config, Config> all) {
            if (config.in < 4) {
                Config init = config.copyOf();
                all.put(init, init);
            }
            Config cofUp = config.copyOf();
            for (int i = at; i < 9; i++) {
                if (cofUp.toStop[i] != 0) {
                    cofUp.in -= cofUp.toStop[i];
                    cofUp.toStop[i] = 0;
                    if (all.containsKey(cofUp)) {
                        int newTime = cofUp.time + Math.abs(i - next);
                        all.get(cofUp).time = Math.min(all.get(cofUp).time, newTime);
                    } else {
                        Config toAdd = cofUp.copyOf();
                        toAdd.time += Math.abs(i - next);
                        all.put(toAdd, toAdd);
                    }
                }
                cofUp.time++;
            }
            Config cofDown = config.copyOf();
            for (int i = at; i >= 0; i--) {
                if (cofDown.toStop[i] != 0) {
                    cofDown.in -= cofDown.toStop[i];
                    cofDown.toStop[i] = 0;
                    if (all.containsKey(cofDown)) {
                        int newTime = cofDown.time + Math.abs(i - next);
                        all.get(cofDown).time = Math.min(all.get(cofDown).time, newTime);
                    } else {
                        Config toAdd = cofDown.copyOf();
                        toAdd.time += Math.abs(i - next);
                        all.put(toAdd, toAdd);
                    }
                }
                cofDown.time++;
            }
        }

        public void solve(Scanner sc, PrintWriter pw) throws IOException {
            Map<Config, Config> all = new HashMap<>();
            int n = sc.nextInt();
            Config inital = new Config();
            all.put(inital, inital);
            int[] from = new int[n + 1];
            int[] to = new int[n + 1];
            for (int i = 0; i < n; i++) {
                from[i] = sc.nextInt() - 1;
                to[i] = sc.nextInt() - 1;
            }
            from[n] = from[n - 1];
            to[n] = to[n - 1];
            inital.time += from[0];
            for (int i = 0; i < n; i++) {
                Map<Config, Config> next = new HashMap<>();
                for(Config conf: all.keySet()) {
                    conf.toStop[to[i]]++;
                    conf.in++;
                    configure(from[i], from[i + 1], conf, next);
                }
                all = next;
            }
            int minTime = Integer.MAX_VALUE;
            for (Config conf: all.keySet()) {
                if (conf.in == 0) {
                    minTime = Math.min(minTime, conf.time);
                }
            }
            pw.println(minTime + 2 * n);
        }

    }

    static class Scanner {
        StringTokenizer st;
        BufferedReader br;

        public Scanner(InputStream s){  br = new BufferedReader(new InputStreamReader(s));}

        public Scanner(FileReader s) throws FileNotFoundException {br = new BufferedReader(s);}

        public String next() throws IOException
        {
            while (st == null || !st.hasMoreTokens())
                st = new StringTokenizer(br.readLine());
            return st.nextToken();
        }

        public int nextInt() throws IOException {return Integer.parseInt(next());}

        public long nextLong() throws IOException {return Long.parseLong(next());}

        public String nextLine() throws IOException {return br.readLine();}

        public double nextDouble() throws IOException { return Double.parseDouble(next()); }

        public boolean ready() throws IOException {return br.ready();}
    }
}


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 + 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 < 19; 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 + 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 + 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++;
                    if (c == 9) rm++;
                    if (d == 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++;
                    if (d == 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 < 19; j++)   {
            if (ans[hash(n, j, 9, 9, 9)] < ansmin) ansmin = ans[hash(n, j, 9, 9, 9)];
        }
        System.out.println(ansmin);
    }
}