eolymp
bolt
Try our new interface for solving problems
Problems

Fast Postman

Fast Postman

A postman has to deliver several letters along a street. He has the address (in the form of the number of meters from the left end of the street to the destination of the letter) and the maximum time he can take to deliver each letter. The postman moves at $1$ meter per second and can deliver a letter instantly once he reaches the right location. You need to find out if it's possible to make all the deliveries within the given constraints, and if so, the minimum time the postman can take to do it. \InputFile Contains multiple test cases, each of them consists of three lines. In the first line of each test there are two numbers: the number of addresses $AddrNum~(1 \le AddrNum \le 50)$ and the initial position of the postman $initialPos~(1 \le initialPos \le 10^6)$ in the same units and format as the addresses. The second and the third line contains $AddrNum$ numbers. The $i$-th element of the second and the third line represents the address and maximum time of delivery of the $i$-th letter. Each number in the second line of the test is between $1$ and $10^6$ inclusive. Each number in the third line of the test is between $1$ and $10^9$ inclusive. \OutputFile For each test case print in a separate line the minimum amount of time the postman needs to deliver all letters within the given constraints or $-1$ if it's impossible to do so.
Time limit 1 second
Memory limit 128 MiB
Input example #1
4 4
1 3 5 7
9 2 5 100
4 2
1 7 10 4
15 6 28 39
Output example #1
13
20