F.A.Q
Hand In Hand
Online Acmers
Problem Archive
Realtime Judge Status
Authors Ranklist
 
     C/C++/Java Exams     
ACM Steps
Go to Job
Contest LiveCast
ICPC@China
Best Coder beta
VIP | STD Contests
    DIY | Web-DIY beta
Author ID 
Password 
 Register new ID

National Day Parade

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2913    Accepted Submission(s): 1286


Problem Description
There are n¡Án students preparing for the National Day parade on the playground. The playground can be considered as a n¡Ám grid. The coordinate of the west north corner is (1,1) , and the coordinate of the east south corner is (n,m).

When training, every students must stand on a line intersection and all students must form a n¡Án square. The figure above shows a 3¡Á8 playground with 9 students training on it. The thick black dots stand for the students. You can see that 9 students form a 3¡Á3 square.

After training, the students will get a time to relax and move away as they like. To make it easy for their masters to control the training, the students are only allowed to move in the east-west direction. When the next training begins, the master would gather them to form a n¡Án square again, and the position of the square doesn¡¯t matter. Of course, no student is allowed to stand outside the playground.

You are given the coordinates of each student when they are having a rest. Your task is to figure out the minimum sum of distance that all students should move to form a n¡Án square.
 

Input
There are at most 100 test cases.
For each test case:
The first line of one test case contain two integers n,m. (n<=56,m<=200)
Then there are n¡Án lines. Each line contains two integers, 1<=Xi<=n,1<= Yi<=m indicating that the coordinate of the ith student is (Xi , Yi ). It is possible for more than one student to stand at the same grid point.

The input is ended with 0 0.
 

Output
You should output one line for each test case. The line contains one integer indicating the minimum sum of distance that all students should move to form a n¡Án square.
 

Sample Input
2 168 2 101 1 127 1 105 2 90 0 0
 

Sample Output
41
 

Source
 

Statistic | Submit | Discuss | Note
Hangzhou Dianzi University Online Judge 3.0
Copyright © 2005-2024 HDU ACM Team. All Rights Reserved.
Designer & Developer : Wang Rongtao LinLe GaoJie GanLu
Total 0.000000(s) query 1, Server time : 2024-06-18 20:10:25, Gzip enabled