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

1377. 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 and deep fence.
Lara Petrovna 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 two numbers N and M (2 ≤ N,M ≤ 100) — the sizes of the cemetery. The second and the third lines contain the treasure graves coordinates. Assume that the North-Western grave has coordinates (1,1) and the South-Eastern — (N,M). Lara starts with the grave (1,1) moving to the East, i.e. towards the grave (1,2).


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


5 4
2 2
5 3
Problem Author: Stanislav Vasilyev
Problem Source: IX Urals Programming Contest. Yekaterinburg, April 19-24, 2005