问题3416--Connect

3416: Connect

[命题人 : ]
时间限制 : 5.000 sec  内存限制 : 64 MB

题目描述

When constructing electric circuits one has to connect pairs of points using wire, preferable as short as possible. In this problem we have an empty circuit board of size N M where we want to connect the two points A1 and A2 with each other using one wire, and the two points B1 and B2 with each other using another wire. The wires must go along the horizontal and vertical edges of the grid (see gure), and the two wires may not share a common vertex. Determine the minimum length of wire needed to do so. The wire may not go outside the circuit board.

输入

The frst line contains two integers, N (2 <= N <= 100) and M (2 <= M <= 100), the grid size of the circuit board. Then follows four lines containing the coordinates for the points A1, A2, B1 and B2, respectively. Each coordinate pair will be described using two integers and will correspond to an intersection point in the grid. The rst coordinate will be between 0 and N inclusive and the second coordinate between 0 and M inclusive. All coordinate pairs will be unique.

输出

A single line containing the minimum length of wire needed to connect the points, or "IMPOSSIBLE" if it's not possible to do so.

样例输入 Copy

6 6
2 1
5 4
4 0
4 5
6 3
2 3
4 0
0 2
6 1

样例输出 Copy

15
IMPOSSIBLE

来源/分类