You work in a big office. It is a 9 floor building with an elevator that can accommodate up to 4 people. It is your responsibility to manage this elevator.
Today you are late, so there are queues on some floors already. For each person you know the floor where he currently is and the floor he wants to reach. Also, you know the order in which people came to the elevator.
According to the company's rules, if an employee comes to the elevator earlier than another one, he has to enter the elevator earlier too (even if these employees stay on different floors). Note that the employees are allowed to leave the elevator in arbitrary order.
The elevator has two commands:
Initially the elevator is empty and is located on the floor 1.
You are interested what is the minimum possible time you need to spend to deliver all the employees to their destination. It is not necessary to return the elevator to the floor 1.
The first line contains an integer n (1 ≤ n ≤ 2000) — the number of employees.
The i-th of the next n lines contains two integers ai and bi (1 ≤ ai, bi ≤ 9, ai ≠ bi) — the floor on which an employee initially is, and the floor he wants to reach.
The employees are given in the order they came to the elevator.
Print a single integer — the minimal possible time in seconds.
2
3 5
5 3
10
2
5 3
3 5
12
t = 2
t = 3
t = 5
t = 6
t = 7
t = 9
t = 10
Name |
---|