Under construction

Still being built. Things may change

Garden

Time limit2sMemory limit1024 MB

Summary
Find the smallest axis-aligned rectangle that contains at least one artwork of each kind, where type A repeats with period D in both directions and type B in one.
Level

Hard8 of 10

Topics
Two pointers, Segment tree, Sorting, Math
Solved
No attempts yet

Statement

JOI Kingdom is a mysterious kingdom which has a boundless expanse of territory. JOI-kun, the king of JOI Kingdom, is planning to cut a part of the territory and make his garden.

The territory of JOI Kingdom is considered as a sufficiently large 22-dimensional grid. The grid is paved with square cells from the top to the bottom and from the left to the right. There is a cell, which is the origin of the coordinates. Let (x,y)(x, y) denote the cell one arrives at when one moves from the origin to the right direction for the distance of xx cells and to the upward direction for the distance of yy cells. Here, the left direction for the distance of aa cells means the right direction for the distance of −a-a cells. Similarly, the downward direction for the distance of aa cells means the upward direction for the distance of −a-a cells.

Some artworks are placed on the territory. The artworks are classified into two types, Type A and Type B, according to the way to be placed in the territory.

  • There are NN kinds of artworks of type A. An artwork of ii-th kind (1≤i≤N1 ≤ i ≤ N) is placed on every cell of the form (Pi+kD,Qi+lD)(P_i + kD, Q_i + lD), where kk, ll are integers.
  • There are MM kinds of artworks of type B. An artwork of jj-th kind (1≤j≤M1 ≤ j ≤ M) is placed on every cell of the form (Rj+kD,y)(R_j + kD, y), where kk, yy are integers, or of the form (x,Sj+lD)(x, S_j + lD), where ll, xx are integers.

A cell may contain several artworks of different kinds.

JOI-kun is planning to choose a rectangular region on the grid to make a garden. In other words, he will choose 44 integers aa, bb, cc, dd. Then the cells of the form (x,y)(x, y), where xx, yy are integers satisfying a≤x≤ba ≤ x ≤ b, c≤y≤dc ≤ y ≤ d, will constitute JOI-kun's garden. Since JOI-kun likes to see artworks of many kinds, for any of the N+MN + M kinds of artworks, JOI-kun's garden should contain at least one artwork of that kind. On the other hand, the citizens of JOI Kingdom will be angry if JOI-kun plans to make a too large garden. Therefore, JOI-kun wants to minimize the number of cells in the garden so that the above condition is satisfied.

Write a program which, given information of artworks, calculates the minimum number of cells in JOI-kun's garden.

Input

N M D
P_1 Q_1
...
P_N Q_N
R_1 S_1
...
R_M S_M

Output

Write one line to the standard output. The output should contain the minimum number of cells in JOI-kun's garden.

Constraints

  • N≥1N ≥ 1.
  • M≥1M ≥ 1.
  • N+M≤500000N + M ≤ 500000.
  • 1≤D≤50001 ≤ D ≤ 5000.
  • 0≤Pi<D0 ≤ P_i < D (1≤i≤N1 ≤ i ≤ N).
  • 0≤Qi<D0 ≤ Q_i < D (1≤i≤N1 ≤ i ≤ N).
  • 0≤Rj<D0 ≤ R_j < D (1≤j≤M1 ≤ j ≤ M).
  • 0≤Sj<D0 ≤ S_j < D (1≤j≤M1 ≤ j ≤ M).
  • Given values are all integers.

Examples3

  1. Example 1

    Input
    2 1 5
    1 4
    2 2
    0 0
    
    Expected output
    8
    
  2. Example 2

    Input
    3 4 100
    20 26
    81 56
    20 3
    58 71
    74 82
    95 61
    95 61
    
    Expected output
    2840
    
  3. Example 3

    Input
    5 7 5000
    1046 365
    4122 1166
    4009 2896
    1815 4065
    4372 1651
    2382 123
    1475 836
    3313 4005
    2579 568
    4300 4867
    1050 3214
    3589 4653
    
    Expected output
    10543092