#K78142. Minimum Brush Strokes for Painting a Rectangle

    ID: 35021 Type: Default 1000ms 256MiB

Minimum Brush Strokes for Painting a Rectangle

Minimum Brush Strokes for Painting a Rectangle

In this problem, you are given a grid with dimensions (n \times m) and a subrectangle specified by its top-left corner ((x_1, y_1)) and bottom-right corner ((x_2, y_2)). The task is to determine the minimum number of continuous brush strokes required to paint the entire subrectangle. A single stroke can paint either a full contiguous row or a full contiguous column within the subrectangle. It turns out that the minimum number of strokes needed is exactly (\min(\text{width},,\text{height})), where the width and height of the subrectangle are defined as follows:

[ \text{width} = y_2 - y_1 + 1 \quad \text{and} \quad \text{height} = x_2 - x_1 + 1. ]

Your goal is to compute and output this minimum number of strokes.

inputFormat

The input consists of a single line containing six space-separated integers: (n), (m), (x_1), (y_1), (x_2), and (y_2). It is guaranteed that (1 \le x_1 \le x_2 \le n) and (1 \le y_1 \le y_2 \le m).

  • (n) and (m) represent the number of rows and columns of the grid, respectively.
  • ((x_1, y_1)) is the coordinate of the top-left corner of the subrectangle.
  • ((x_2, y_2)) is the coordinate of the bottom-right corner of the subrectangle.

outputFormat

Output a single integer: the minimum number of continuous strokes required to paint the subrectangle. The result is computed by the formula:

[ \min,(y_2 - y_1 + 1,, x_2 - x_1 + 1). ]## sample

5 5 2 2 4 4
3