#P1129A1. Toy Train (Simplified)
Toy Train (Simplified)
No submission language available for this problem.
Description
This is a simplified version of the task Toy Train. These two versions differ only in the constraints. Hacks for this version are disabled.
Alice received a set of Toy Train™ from Bob. It consists of one train and a connected railway network of stations, enumerated from through . The train occupies one station at a time and travels around the network of stations in a circular manner. More precisely, the immediate station that the train will visit after station is station if or station if . It takes the train second to travel to its next station as described.
Bob gave Alice a fun task before he left: to deliver candies that are initially at some stations to their independent destinations using the train. The candies are enumerated from through . Candy (), now at station , should be delivered to station ().

The train has infinite capacity, and it is possible to load off any number of candies at a station. However, only at most one candy can be loaded from a station onto the train before it leaves the station. You can choose any candy at this station. The time it takes to move the candies is negligible.
Now, Alice wonders how much time is needed for the train to deliver all candies. Your task is to find, for each station, the minimum time the train would need to deliver all the candies were it to start from there.
The first line contains two space-separated integers and (; ) — the number of stations and the number of candies, respectively.
The -th of the following lines contains two space-separated integers and (; ) — the station that initially contains candy and the destination station of the candy, respectively.
In the first and only line, print space-separated integers, the -th of which is the minimum time, in seconds, the train would need to deliver all the candies were it to start from station .
Input
The first line contains two space-separated integers and (; ) — the number of stations and the number of candies, respectively.
The -th of the following lines contains two space-separated integers and (; ) — the station that initially contains candy and the destination station of the candy, respectively.
Output
In the first and only line, print space-separated integers, the -th of which is the minimum time, in seconds, the train would need to deliver all the candies were it to start from station .
Samples
Note
Consider the second sample.
If the train started at station , the optimal strategy is as follows.
- Load the first candy onto the train.
- Proceed to station . This step takes second.
- Deliver the first candy.
- Proceed to station . This step takes second.
- Load the second candy onto the train.
- Proceed to station . This step takes second.
- Deliver the second candy.
- Proceed to station . This step takes second.
- Load the third candy onto the train.
- Proceed to station . This step takes second.
- Deliver the third candy.
Hence, the train needs seconds to complete the tasks.
If the train were to start at station , however, it would need to move to station before it could load the first candy, which would take one additional second. Thus, the answer in this scenario is seconds.