QOJ.ac

QOJ

Time Limit: 1 s Memory Limit: 128 MB Total points: 10

#11840. Buses [B]

统计

Little John is planning to make a trip with his friend to the museum. They arranged a meeting at the bus depot. John is a very punctual person, so he arrived at the bus depot too early. It is cold outside, so John decided to wait for his colleague in a bus. Unfortunately, all buses stop at the depot only for a while, so John decided to get on one of the buses, travel a couple of bus stops and then switch back to a bus going back to the depot. Because of bad weather, John would like to spend outside as little time as possible. In order to obtain this goal, he would like to minimize the total time he will wait for the bus at the depot, the time he will wait for his friend after getting back to the depot and the time he will spend waiting for the change of buses. As we already know, John is very punctual and so he would not like to be late for meeting his friend. John found a detailed timetable of all buses, which will let him plan the ride. He is not a great mathematical however, so he asked you to solve the problem for him.

Write a program which:

  • reads from the standard input the moments of John's and his friend's arrivals and the bus timetable,
  • calculates the shortest possible time, which John has to spend outside waiting for his friend,
  • writes the result to the standard output.

Input Format

In the first line of the standard input there are five integers: $ t_{1} $, $ t_{2} $, $ m $, $ n_{1} $, $ n_{2}$ ($0 ≤ t_{1} ≤ t_{2} ≤ 1\,000\,000\,000$, $2 ≤ m ≤ 1\,000$, $ n_{1}, n_{2} ≥ 1$, $ m \times (n_{1} + n_{2}) ≤ 1\,000\,000$), separated by single spaces. These numbers represent adequately: the moment of John's arrival to the bus depot, the moment of John's friend's arrival to the bus depot, the number of bus stops on the route (including the bus depot), the number of buses departing from the bus depot and the number of buses arriving at the depot. The following $ m $ lines contain bus timetables for the consecutive bus stops. The first bus stop is the depot. Buses departing from the depot stop at the stops $1, 2, \ldots, m $. Buses arriving at the depot stop at the stops $ m, \ldots, 2, 1$. Each bus after arriving to the first or $ m $-th stop finishes its course for the considered day. The timetable for the $ i $-th bus stop consists of $n_1+n_2$ integers $0 ≤ x_{ij} ≤ 1\,000\,000\,000$, where $ x_{ij} $ represents the time of arrival / departure of the $ j $-th bus. These numbers are separated within each line by single spaces. For $ j ≤ n_{1} $ the number is related to the bus departing from the bus depot, and for $ j > n_{1} $ - the bus arriving at the depot. Arrival and departure of each bus at every stop take place in the same time unit. Each bus needs at least one time unit to travel between any two consecutive bus stops. John can get on a bus assuming that he is at the appropriate bus stop at the time of the bus's departure. In particular, switching from bus A to bus B, provided that the moments of their arrival at the bus stop are adequately $ t_{a} $ and $ t_{b} $, is possible if $ t_{a} ≤ t_{b} $.

Output Format

The first and only line of the standard output should contain one integer - the minimal time that John has to spend outside waiting for his friend. If there are no busses allowing John to perform planned maneuver, you should assume that John will spend all the time waiting at the depot.

Example

Input

0 10 3 1 2
0 9 10
3 4 8
4 3 7

Output

2

In the solution minimizing John's time of waiting outside, he is required to: get on the first bus at the depot in the zero time unit, get off at the second bus stop, get on the second bus in the fourth time unit and be back at the depot in the ninth time unit.

About Issues

We understand that our problem archive is not perfect. If you find any issues with the problem, including the statement, scoring configuration, time/memory limits, test cases, etc.

You may use this form to submit an issue regarding the problem. A problem moderator will review your issue and proceed it properly.

STOP! Before you submit an issue, please READ the following guidelines:

  1. This is not a place to publish a discussion, editorial, or requests to debug your code. Your issue will only be visible by you and problem moderators. Other users will not be able to view or reply your issues.
  2. Do not submit duplicated issues. If you have already submitted one, please wait for an moderator to review it. Submitting multiple issues will not speed up the review process and might cause your account to be banned.
  3. Issues must be filed in English or Chinese only.
  4. Be sure your issue is related to this problem. If you need to submit an issue regarding another problem, contest, category, etc., you should submit it to the corresponding page.

Active Issues 0

No issues in this category.

Closed/Resolved Issues 0

No issues in this category.