Memory limit: 32 MB
here is a building with flat square roof of size and sides parallel to north-south and east-west directions. The roof is covered with square tiles of size (with a side of length ), but one of the tiles has been removed and there is a hole in the roof (big enough to fall in). The tiles form a rectangular mesh on the roof, so their positions may be specified with coordinates. The tile at the southwestern corner has coordinates . The first coordinate increases while going eastwards, and the second while going northwards.
Sleepwalker wanders across the roof, in each step moving from the tile he is standing on to the adjacent one on the east (E), west (W), south (S), or north(N). The sleepwalker roof ramble starts from the southwestern corner tile. The description of the path is a word built of the letters N, S, E, W denoting respectively a step to the north, south, east and west. For the word describing the path of sleepwalker is
For the word describing the path of sleepwalker is
(See the picture that shows how the sleepwalker would go across a roof of dimension or .) Generally, if , the description of a sleepwalker's path on the roof of dimension is a word:
where functions , and denote the following permutations of letters specifying directions:
- : EN WS NE SW
- : ES WN NW SE
- : EW WE NS SN
E.g. SENWNE, SENESW, SENNWS.
We start observing sleepwalker at the time he stands on the tile of coordinates . After how many steps will sleepwalker fall into the hole made after removing the tile of coordinates ?
There are sleepwalker's paths on roofs of dimension and on the picture below. In the second case, the point at which the observation starts and the hole have been marked. The sleepwalker has exactly 20 steps to the hole (from the moment the observation starts).
Write a program which:
- reads from the standard input integer denoting the size of the roof (), the position of the sleepwalker at the moment the observation starts and the position of the hole,
- computes the number of steps that the sleepwalker will make before he falls into the hole,
- writes the result to the standard output.
In the first line of the standard input one integer , , denoting the size of the roof () is written. In each of the following two lines of the input two natural numbers , separated with a space are written, , . The numbers in the second line are the coordinates of the tile the sleepwalker is standing on. The numbers in the third line are the coordinates of the hole. You may assume, that with these data the sleepwalker will eventually fall into the hole after some number of steps.
The only line of standard output should contain the number of steps on the sleepwalker's path to the hole.
For the input data:
2 3 2 7 2
the correct result is:
Task author: Wojciech Rytter.<Submit a solution> [0/100]