CF1797A.Li Hua and Maze

传统题 时间 2000 ms 内存 256 MiB 3 尝试 1 已通过 1 标签

Li Hua and Maze

There is a rectangular maze of size n×mn\times m. Denote (r,c)(r,c) as the cell on the rr-th row from the top and the cc-th column from the left. Two cells are adjacent if they share an edge. A path is a sequence of adjacent empty cells.

Each cell is initially empty. Li Hua can choose some cells (except (x1,y1)(x_1, y_1) and (x2,y2)(x_2, y_2)) and place an obstacle in each of them. He wants to know the minimum number of obstacles needed to be placed so that there isn't a path from (x1,y1)(x_1, y_1) to (x2,y2)(x_2, y_2).

Suppose you were Li Hua, please solve this problem.

Input

The first line contains the single integer tt (1t5001 \le t \le 500) — the number of test cases.

The first line of each test case contains two integers n,mn,m (4n,m1094\le n,m\le 10^9) — the size of the maze.

The second line of each test case contains four integers x1,y1,x2,y2x_1,y_1,x_2,y_2 (1x1,x2n,1y1,y2m1\le x_1,x_2\le n, 1\le y_1,y_2\le m) — the coordinates of the start and the end.

It is guaranteed that x1x2+y1y22|x_1-x_2|+|y_1-y_2|\ge 2.

Output

For each test case print the minimum number of obstacles you need to put on the field so that there is no path from (x1,y1)(x_1, y_1) to (x2,y2)(x_2, y_2).

Note

In test case 1, you can put obstacles on (1,3),(2,3),(3,2),(4,2)(1,3), (2,3), (3,2), (4,2). Then the path from (2,2)(2,2) to (3,3)(3,3) will not exist.

Samples

3
4 4
2 2 3 3
6 7
1 1 2 3
9 9
5 1 3 6
4
2
3

在线编程 IDE

建议全屏模式获得最佳体验