Mall Mania

Waterloo has two giant shop- ping malls, each enclosing several city blocks. Kim and Pat like to shop and to walk within the malls but they don’t like walking between the malls because such walk- ing does not contribute di- rectly to the task at hand: shopping. They would there- fore like to know the min- imum crossing distance be- tween the malls. Each city block is a unit square delimited by streets and avenues. Streets run east-west and avenues run north-south. Both are iden- tified by consecutive integers between 0 and 2000. (Lower numbered avenues are west of higher numbered avenues and lower numbered streets are south of higher numbered streets.) Streets and avenues are narrow so their thickness may be assumed to be 0. Each mall is a contiguous set of complete city blocks. By contiguous we mean that any two blocks are connected by some sequence of blocks such that consecutive pairs of blocks in the sequence share a side. The malls do not intersect and do not surround any empty blocks; that is, the blocks not in any mall are themselves contiguous. Input Standard input contains several test cases. Each test case contains the description of the two malls. Each mall’s description consists of an integer p ≥ 4, the perimeter of the mall, followed by one or more lines containing p pairs (a, s) giving the coordinates of the avenue-street intersections contained in the perimeter, in clockwise order. A line containing 0 follows the last test case. Output For each case, output a single integer d — the minimum walking distance between the malls, assuming that Kim and Pat always walk along streets and avenues. Sample Input 4 00011110 6 434232

2/2 2223 33 0 Sample Output 2