Monocarp is the most famous thief in Berland. This time, he decided to steal two diamonds. Unfortunately for Monocarp, there are $$$n$$$ cameras monitoring the diamonds. Each camera has two parameters, $$$t_i$$$ and $$$s_i$$$. The first parameter determines whether the camera is monitoring the first diamond only ($$$t_i=1$$$), the second diamond only ($$$t_i=2$$$), or both diamonds ($$$t_i=3$$$). The second parameter determines the number of seconds the camera will be disabled after it is hacked.
Every second, Monocarp can perform one of the following three actions:
Note that Monocarp can hack a camera multiple times, even if it is currently disabled.
Your task is to determine the minimum time it will take Monocarp to steal both diamonds, beginning with the first diamond, or report that it is impossible.
The first line contains a single integer $$$n$$$ ($$$0 \le n \le 1500$$$) — the number of cameras.
Then $$$n$$$ lines follow, the $$$i$$$-th of them contains two integers $$$t_i$$$ and $$$s_i$$$ ($$$1 \le t_i \le 3$$$; $$$1 \le s_i \le 2n$$$) — the parameters of the $$$i$$$-th camera.
Print a single integer — the minimum time it will take for Monocarp to steal the first diamond first and then the second diamond. If it is impossible, print -1.
4 2 6 1 2 1 2 2 1
6
4 2 8 3 2 3 2 3 5
9
2 3 2 2 3
4
1 3 1
4
8 2 1 2 2 3 5 3 6 1 2 1 3 1 4 1 5
11
Name |
---|