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

BOMB

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 256    Accepted Submission(s): 19


Problem Description
Our space military base is under attack.The virus break into our computer,and destroy the CCS(Carrier Control System).Carrier is a kind of spaceship,which has the most powerful weapon.We have tried several times to find out the way to take control of them again.Unfortunately,at last we realized that our carriers are not belong to us anymore.We nosed out that our enemy who comes from Chars,a planet with many wise cultures living in,had embezzled the command of the carriers from us.Soon after,We recived a command from our SCO(space command office).It says we have to destroy all the uncontrollable carriers.
According to the information report we got from our spy organization,All the uncontrollable carriers are fly in a circle rail.To make a easier problem,we divide the circle into equal N(N<=100000000) parts and number it from 0 to N-1. We can consider that the length every part of rail is 1.In light of our report,there are M(M<=100000) carriers are uncontrollable.The number i uncontrollable carrier is at the middle of the number p[i](0<=p[i]<=N-1) rail.To destroy the carriers,we have M remote control bombs.The number I bombs is at the middle of the number q[i](0<=q[i]<=N-1) rail.We can let a bomb move to a carrier.When it move from number i rail to number i+1(or number i-1) rail it cost 1 energy(bombs in number N-1 rail can move to number 0 rail ,it also cost 1 energy;Remember bombs cannot leave the rail ) .The more energy we used when we move,The less damage we can make to the carriers.Clearly, We must spend the minimal energy to move all the bombs on to the carrier.One bombs can just destroy one carrier,even two carrier are at the same place.
We can imagine that carrier do not move.
 

Input
There are several test case.
For each test case: The first line contain 2 integers.N,M. Then followed 2*m lines,1..m line contain p[i],and m+1..2*m lines contain q[i];
 

Output
For each test case,print the minimal energy we have to spent to move all the bombs to destroy all the carriers.
 

Sample Input
10 4 0 1 2 3 8 9 4 5
 

Sample Output
8
 

Author
DingShuai
 

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-11-22 22:52:42, Gzip enabled