#T60. 连接格点

连接格点

Description

There is a point grid with M rows and N columns, where adjacent points can be connected. A vertical connection costs one unit, and a horizontal connection costs two units. Some points are already connected. Determine the minimum additional cost required to ensure all points are fully connected.

Input Format

The first line contains two positive integers, m and n.
The following lines each contain four positive integers: x1,y1,x2,y2x_1, y_1, x_2, y_2, indicating that the point at row x1x_1, column y1y_1 is already connected to the point at row x2x_2, column y2y_2. The input guarantees that x1x2+y1y2=1|x_1 - x_2| + |y_1 - y_2| = 1.

Output Format

Output the minimum additional cost required to connect all the points.

2 2
1 1 2 1

3

Hint

【Data Scale】 30% of the data: n*m ≤ 1000
100% of the data: m, n ≤ 1000