Under construction

Still being built. Things may change

Multiplayer Snake

Time limit2sMemory limit1024 MB

Summary
On a wrap-around grid, two snakes move alternately; find the first move on which one snake can step onto the other snake's cell.
Level

Medium6 of 10

Topics
Math, Implementation
Solved
No attempts yet

Statement

Konia is a company that makes budget mobile devices, and it has been losing market share recently. To save the company, the board of directors decided to change Snake, the most famous game preinstalled on every Konia device.

The rules are simple. A snake of length 1 (a single 1 × 1 cell) sits on a grid of NN rows and MM columns. On each move, the snake moves one cell up, down, left, or right. If it crosses a border of the screen, it appears on the opposite side. For example, stepping left from cell (4,1)(4, 1) on a board with 10 rows and 8 columns puts the snake in cell (4,8)(4, 8). Food sometimes appears on the board and can make the snake longer, but that does not matter for this problem.

The board gets a two-player mode. At the start, the snakes of the two players appear in different cells, each of length 1, so each snake consists only of its starting cell. Each turn, the first player's snake moves first, then the second player's snake moves. Movement follows the same rules as in single-player mode. Unlike single-player mode, the snakes can run into each other, and then the game ends. Market research shows that users prefer games that last as long as possible, so the developers must study how the game length depends on the starting coordinates (a1,b1)(a_1, b_1) and (a2,b2)(a_2, b_2). The two-player mode only supports movement for now, so no food appears, and the game reduces to controlling two single-cell snakes.

As the lead engineer, you are tasked with overseeing your colleagues' work. Write a program that takes the board size NN and MM and two different cells (a1,b1)(a_1, b_1) and (a2,b2)(a_2, b_2), and computes the number of the first move on which the snakes can collide, that is, one snake moves into the cell where the other snake is.

Input

The first line contains two integers NN and MM (1≤N,M≤1061 \le N, M \le 10^6), the number of rows and columns of the board. Rows are numbered from 1 to NN from bottom to top, and columns are numbered from 1 to MM from left to right.

The second line contains a1a_1 and b1b_1 (1≤a1≤N1 \le a_1 \le N, 1≤b1≤M1 \le b_1 \le M), the starting row and column of the first player's snake.

The third line contains a2a_2 and b2b_2 in the same format for the second player's snake.

It is guaranteed that the two snakes start in different cells.

Output

Print the number of the first move on which the snakes can collide, that is, one snake moves into the cell where the other snake is.

Hint

In the first test, there is exactly one cell where the snakes can meet after one move: cell (5,6)(5, 6).

Examples3

  1. Example 1

    Input
    5 8
    5 5
    5 7
    
    Expected output
    1
    
  2. Example 2

    Input
    10 10
    1 1
    10 10
    
    Expected output
    1
    
  3. Example 3

    Input
    10 4
    3 1
    10 3
    
    Expected output
    3