#P6469. [COCI 2008/2009 #6] NERED

[COCI 2008/2009 #6] NERED

Description

There is an n×nn \times n grid. Each row and each column has nn cells. Each cell contains a number, and initially all numbers are 00. There are mm modifications, and each modification adds 11 to the number in a specific cell.

Define one operation as decreasing the number in a non-zero cell by 11, and increasing the number in another cell (which may be 00) by 11.

Find the minimum number of operations needed so that every cell contains either 00 or 11, and all cells with value 11 form a rectangle. Output the number of operations.

Input Format

The first line contains two integers, representing the grid size nn and the number of modifications mm.

The next mm lines each contain two integers x,yx, y, meaning the number in the cell at row xx, column yy is increased by 11.

Output Format

Output one line with one integer, the answer.

3 2
1 1
1 1

1
4 3
2 2
4 4
1 1

2
5 8
2 2
3 2
4 2
2 4
3 4
4 4
2 3
2 3 

3

Hint

Sample Explanation

Explanation for Sample 1

Subtract 11 from row 11, column 11, and add 11 to row 22, column 11.

Explanation for Sample 3

  • Subtract 11 from row 22, column 33, and add 11 to row 33, column 33.
  • Subtract 11 from row 44, column 22, and add 11 to row 22, column 55.
  • Subtract 11 from row 44, column 44, and add 11 to row 33, column 55.

Constraints

For all test points, it is guaranteed that:

  • 1n1001 \leq n \leq 100, 1mn21 \leq m \leq n^2.
  • 1x,yn1 \leq x, y \leq n.
  • A solution always exists for the input.

Notes

This problem is translated from COCI2008-2009 CONTEST #6 T3 NERED. Translation by @一扶苏一.

Translated by ChatGPT 5