A Path to Nowhere
Time limit2sMemory limit1024 MB
Given an n by m grid and a start cell, count the cells visited by a spiral walk with step lengths 1, 1, 2, 2, 3, 3, and so on, until it leaves the grid.
- Level
Medium5 of 10
- Topics
- Simulation, Math
- Solved
- No attempts yet
Statement
Kolobok has a strange dream. In the dream, Kolobok stands on a grid field of size at cell .
At the start, Kolobok faces the positive direction of the X axis. Then he walks across the field following this pattern:
- Move one cell forward. Turn 90 degrees to the right.
- Move one cell forward. Turn 90 degrees to the right.
- Move two cells forward. Turn 90 degrees to the right.
- Move two cells forward. Turn 90 degrees to the right.
- Move three cells forward. Turn 90 degrees to the right.
- Move three cells forward. Turn 90 degrees to the right.
- Move four cells forward. Turn 90 degrees to the right.
- And so on...
The movement continues until Kolobok leaves the field. Then he wakes up.
In the morning, Kolobok wants to analyze his dream. He is sure that he was in each cell at most once, but he cannot remember how many cells he visited. Write a program that counts the cells he visited.

Input
The first line contains two positive integers and (), the sizes of the board along the X axis and the Y axis. The second line contains two positive integers and (; ), the start position of Kolobok.
Output
Print one number, the count of cells Kolobok visited during the dream.
Hint
The picture shows the first example.