ENG  RUSTimus Online Judge
Online Judge
Online contests
About Online Judge
Frequently asked questions
Site news
Problem set
Submit solution
Judge status
Update your info
Authors ranklist
Current contest
Scheduled contests
Past contests

Ural Championship 2005 Round I

About     Problems     Submit solution     Judge status     Standings
Contest is over

H. Lara Croft

Time limit: 1.0 second
Memory limit: 64 MB
A cemetery has a form of rectangle. There are N rows of graves, M ones in each row. The cemetery is enclosed with a high fence.
Lara Croft has penetrated into the cemetery through the sap at the Northwestern corner. It takes one night for Lara to dig a subway under one of the graves. If there is an intact grave straight ahead then Lara will lengthen the passage during the next night and will ravage the grave. If there is a cemetery fence or a ravaged grave on the way, then Lara will turn 90 degrees clockwise and will continue with her questionable affairs.
Treasures are located in two graves only. And we exactly know in which ones. But Lara doesn't. Lara has bought a package of champagne today. It means, that today she has found one of those graves. We wonder how long will it take her to find the other one?


The first line contains integers N and M that are the sizes of the cemetery (2 ≤ N, M ≤ 100). The North-Western grave has coordinates (1, 1) and the South-Eastern one — (NM). Lara starts with the grave (1, 1) moving to the East, i.e. towards the grave (1, 2).
The second and the third lines contain integers (r1, c1) and (r2, c2) that are the treasure graves coordinates (1 ≤ riN; 1 ≤ ciM). The order of graves is arbitrary, i.e. Lara may reach the first or the second grave earlier.


Output an amount of days that Lara will spend reaching for another grave with treasures.


5 4
2 2
5 3


In the example Lara will find the treasures on the 9'th and the 15'th days.
Problem illustration
Problem Author: Stanislav Vasilyev
Problem Source: IX Urals Programming Contest. Yekaterinburg, April 19-24, 2005
To submit the solution for this problem go to the Problem set: 1377. Lara Croft