#P10057. Intersecting Axis-Parallel Segments

    ID: 12037 Type: Default 1000ms 256MiB

Intersecting Axis-Parallel Segments

Intersecting Axis-Parallel Segments

In a two-dimensional plane, you are given two line segments: segment \(AB\) which is parallel to the \(x\)-axis, and segment \(CD\) which is parallel to the \(y\)-axis.

You are allowed to perform an operation where you choose one of the two segments and translate (shift) it by an arbitrary distance in one of the four cardinal directions (up, down, left, or right). Each such translation counts as one operation.

The task is to determine the minimum number of operations required so that the two segments intersect. Two segments are said to intersect if they share at least one common point. Given that the horizontal segment has a fixed \(y\)-coordinate and spans between two \(x\)-coordinates, and the vertical segment has a fixed \(x\)-coordinate and spans between two \(y\)-coordinates, they intersect if and only if the \(x\)-coordinate of the vertical segment lies within the horizontal segment and the \(y\)-coordinate of the horizontal segment lies within the vertical segment.

Note: If the segments already intersect, then no operation is required.

Mathematically, let the horizontal segment be defined by endpoints \((x_1, y)\) and \((x_2, y)\) (assume \(x_1 \le x_2\)) and the vertical segment by endpoints \((x, y_1)\) and \((x, y_2)\) (assume \(y_1 \le y_2\)). They intersect if and only if:

\[ x_1 \le x \le x_2 \quad \text{and} \quad y_1 \le y \le y_2. \]

If one of these conditions is not satisfied, you can perform a move on the appropriate segment. Since each move translates a segment in only one direction (either horizontally or vertically), the minimum number of moves required is the sum of the moves needed to correct each coordinate misalignment.

inputFormat

The input consists of two lines:

  1. The first line contains three integers: x1, x2, and y, representing the coordinates of the horizontal segment \(AB\). The segment lies at \(y\) and spans from \(x1\) to \(x2\).
  2. The second line contains three integers: x, y1, and y2, representing the coordinates of the vertical segment \(CD\). The segment lies at \(x\) and spans from \(y1\) to \(y2\).

It is guaranteed that the horizontal segment is parallel to the \(x\)-axis and the vertical segment is parallel to the \(y\)-axis.

outputFormat

Output a single integer, the minimum number of operations required for the two segments to intersect.

sample

1 3 2
2 1 4
0