Training Site
Sideways banner

Mathematicians and Physicists

Input: Standard Input (stdin)
Output: Standard Output (stdout)
Memory limit: 256 megabytes
Time limit: 2.0 seconds

You've picked out a set of your M + P favourite scientists. M of them were mathematicians, and the other P were physicists. The i-th scientist was alive from the year a_i until the year b_i (inclusive range). Now you're curious, Which pair of mathematician and physicist coexisted for the largest number of years?

Input

The first line of input contains two integers M and P.

M lines follow. Line number i will contain two integers. The two integers will be a_i and b_i, representing the lifespan of a particular mathematician.

For the remaining P lines, line i will contain two integers. The two integers will be a_i and b_i, representing the lifespan of a particular physicist.

Output

Output a single integer: The maximum number of years any one pair of mathematician and physicist were alive at the same time.

Constraints

  • 1 ≤ M, P ≤ 100,000

  • 2 ≤ M + P ≤ 160{,}000

  • 1 ≤ a_i ≤ b_i ≤ 1{,}000{,}000{,}000.

Subtasks

  • Subtask 1 (+11%): P = 1
  • Subtask 2 (+21%): M + P ≤ 1{,}000
  • Subtask 3 (+23%): a_i = 1 for all i.
  • Subtask 4 (+32%): b_i ≤ 100{,}000 for all i.
  • Subtask 5 (+13%): There are no additional constraints.

Sample Explanation

The 3rd mathematician alive from 1882 - 1935 and the 1st physicist alive from 1879 - 1955 co-existed for 54 years (1882 - 1935)

  • Sample Input 1

    3 4
    1642 1727
    1912 1954
    1882 1935
    1879 1955
    1942 2018
    1867 1934
    1918 1988
    

    Sample Output 1

    54