Problem list
Log in
#icpc00219 · ICPC NWERC 2023

Klompendans

Silver I 5.0 s 2048 MB

In traditional Dutch clog dancing, you as the dancer need to follow a very specific sequence of movements. The dance takes place on a square grid of square tiles, and at the start of the dance you stand on the top left corner tile of the grid. You then alternate between two types of dance move, moving from tile to tile in the grid for as long as you want. Your first move may be of either kind, but after that you need to strictly alternate between the two kinds of moves.

Both moves are similar to knight moves in chess: in the first type of move, you go from your current square to a square that is $a$ tiles away along one axis of the grid and $b$ tiles away along the other axis. Similarly, in the second type of move, you need to move $c$ and $d$ tiles along the respective axes. As you can freely swap the two axes and choose the movement direction along each axis, there can be up to $8$ ways of performing a given type of move. Figure shows an example dance routine with $(a,b) = (1,2)$ and $(c,d) = (2,3)$.

Starting on the top left corner tile, how many different tiles could you reach while doing a clog dance? It is not allowed to step outside of the grid and you do not count tiles that you are simply stepping over while doing a move. Note that you need to count all tiles that can be reached during some performance of the dance, but not necessarily during the same one.

Input

The input consists of:

  • One line with an integer $n$ ($3\leq n\leq 500$), the side length of the square.
  • One line with two integers $a$ and $b$ ($1\leq a, b < n$), describing the first dance move.
  • One line with two integers $c$ and $d$ ($1\leq c, d < n$), describing the second dance move.

Output

Output the number of tiles you can reach using these dance moves.

Example 1

Input
3
2 1
2 2
Output
6

Example 2

Input
8
1 2
1 2
Output
64

Example 3

Input
4
1 2
2 3
Output
13

Example 4

Input
5
1 2
2 3
Output
25

Example 5

Input
10
3 3
4 4
Output
50
Source
ICPC NWERC 2023
Appears in
Not part of a ladder, contest or set yet.
Accepted 0 Submissions 0 Acceptance 0% Solvers 0

Discussion

Threads about this problem. Spoilers are collapsed; wrap hints in ||double bars||.

No discussion yet. Start a thread if something here needs explaining.

Recent submissions Open with filters
Code