Time

13:09:07
23 May 2012
Version for print

Triangular Grid

   There is an infinite grid in the Cartesian plane composed of isosceles triangles, with the following design:

prb785

   A single triangle in this grid is a triangle with vertices on intersections of grid lines that has not other triangles inside it.

   Given two points P and Q in the Cartesian plane you must determine how many single triangles are intersected by the segment $  \overline{{PQ}}$. A segment intersects a polygon if and only if there exists one point of the segment that lies inside the polygon (excluding its boundary).

   Note that the segment $  \overline{{PQ}}$ in the example intersects exactly six single triangles.


Specifications

   Input

   The problem input consists of several cases, each one defined in a line that contains six integer values B, H, x1, y1, x2 and y2 (1 ≤ B 200, 2 H 200, -1000 x1, y1, x2, y2 1000), where:

  • B is the length of the base of all isosceles single triangles of the grid.
  • H is the height of all isosceles single triangles of the grid.
  • (x1, y1) is the point P, that defines the first extreme of the segment.
  • (x2, y2) is the point Q, that defines the second extreme of the segment.

   You can suppose that neither P nor Q lie in the boundary of any single triangle, and that P ≠ Q.

   The end of the input is specified by a line with the string "0 0 0 0 0 0".

   Output

   For each case in the input, print one line with the number of single triangles on the grid that are intersected by the segment$  \overline{{PQ}}$.


Problem information

Time Limit: 4 seconds
Memory Limit: 64 MB
Balls for the passed test: 50
Complexity: 75% 1/4

Example

Example input

100 120 -20 -100 160 160
10 8 5 5 5 4
10 8 5 5 10 5
10 8 5 5 10 10
0 0 0 0 0 0

Example output

6
1
2
3


← Burger Time? Problems GrayInc →