MNERED - NERED

In the nearby kindergarten they recently made up an attractive game of strength and agility that kids love. The surface for the game is a large flat area divided into N×N squares. The children lay large spongy cues onto the surface. The sides of the cubes are the same length as the sides of the squares. When a cube is put on the surface, its sides are aligned with some square. A cube may be put on another cube too. Kids enjoy building forts and hiding them, but they always leave behind a huge mess. Because of this, prior to closing the kindergarten, the teachers rearrange all the cubes so that they occupy a rectangle on the surface, with exactly one cube on every square in the rectangle. In one moving, a cube is taken off the top of a square to the top of any other square.

Write a program that, given the state of the surface, calculates the smallest number of moves needed to arrange all cubes into a rectangle.

Input

The first line contains the integers N and M (1 ≤ N ≤ 100, 1 ≤ M ≤ N^2), the dimensions of the surface and the number of cubes currently on the surface.

Each of the following M lines contains two integers R and C (1 ≤ R, C ≤ N), the coordinates of the square that contains the cube.

Output

Output the smallest number of moves. A solution will always exist.

Sample

Input:
4 3
2 2
4 4
1 1

Output:
2
Input:
5 8
2 2
3 2
4 2
2 4
3 4
4 4
2 3
2 3

Output:
3

In the second example, a cube is moved from (2, 3) to (3, 3), from (4, 2) to (2, 5) and from (4, 4) to (3, 5).


Được gửi lên bởi:psetter
Ngày:2009-03-08
Thời gian chạy:1s
Giới hạn mã nguồn:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Ngôn ngữ cho phép:Tất cả ngoại trừ: ERL GOSU JS-RHINO NODEJS PERL6 PYPY RUST SED VB.NET
Nguồn bài:COCI 6th 09

hide comments
2021-05-27 18:02:00
Tham khảo: https://vnspoj.github.io/problems/MNERED
2017-10-05 03:36:34
Trau cung ac
2017-09-20 16:08:04
Đề này có thể có những trường hợp không chuyển được không? Chẳng hạn có 3 hộp mà N lại bằng 2.
Lúc này hình chữ nhật chỉ có thể xếp là 1x3 hay 3x1 không thể xếp được.
2017-09-12 13:18:25
Trâu = AC :3
2014-09-23 14:24:38 Thần Ðồng Mẫu Giáo
bài gì mà không có giới hạn :3
2014-08-18 13:58:13 Bùi Hữu Giáp
Toàn sai Test thứ 10 mới chết chứ!
2013-11-12 17:01:43 In God We Trust
sai test cuối vãi
2013-06-14 14:21:10 Bitagi97
bài này làm khó chịu quá đi

Last edit: 2013-07-10 04:10:51
2013-06-12 15:45:03 a;slkfjasl;fkj
làm thế nào ta?
2013-05-27 18:03:20 ‡■■Lãng du■■‡
::::==== Anh Voyage
Sao hinh vuông 2x2 lại có ô 1 3
© Spoj.com. All Rights Reserved. Spoj uses Sphere Engine™ © by Sphere Research Labs.