IOIOI Cards

Time limit1sMemory limit512 MB

Summary
Given a row of I/O cards and interval flip operations with per-length costs, decide whether all cards can be turned face up and find the minimum total flip time.
Level

Hard8 of 10

Topics
Shortest path, Graph, Math, Prefix sum
Solved
No attempts yet

Problem

Chairman K enjoys fortune telling and practices many forms of it. Today he decided to use cards with 'I' on the front and 'O' on the back to tell the fortunes of the Japanese team at this year's IOI.

The fortune telling works as follows.

  1. First, choose positive integers A,B,C,D,EA, B, C, D, E.
  2. Lay out A+B+C+D+EA + B + C + D + E cards in a single row. The leftmost AA cards are face up, the next BB cards are face down, the next CC cards are face up, the next DD cards are face down, and the next EE cards are face up. Laid out this way, the row shows AA copies of 'I', then BB copies of 'O', then CC copies of 'I', then DD copies of 'O', then EE copies of 'I', from left to right.
  3. Choose one or more of the NN predetermined operations and perform them in any order. The same operation may be performed more than once. Operation ii (1≤i≤N1 \le i \le N) is "flip every card from the LiL_i-th to the RiR_i-th position from the left." Flipping one card takes 1 second, so performing operation ii takes Ri−Li+1R_i - L_i + 1 seconds.
  4. If every card is face up after all operations, the fortune telling succeeds.

To avoid flipping cards more than necessary, Chairman K decided to first determine whether the fortune telling can succeed before actually using the cards. Moreover, if it can succeed, he decided to find the minimum time needed to make it succeed.

Given the information about how the cards are laid out and the predetermined operations, write a program that determines whether the fortune telling can succeed and, if so, finds the minimum time needed to make it succeed.

Input

Read the following data from standard input.

  • The first line contains integers A,B,C,D,EA, B, C, D, E separated by spaces. This means that at the start of the fortune telling, cards are laid out so that the leftmost AA are face up, the next BB are face down, the next CC are face up, the next DD are face down, and the next EE are face up.
  • The second line contains the integer NN. This means there are NN predetermined operations.
  • Of the following NN lines, the ii-th line (1≤i≤N1 \le i \le N) contains integers Li,RiL_i, R_i separated by spaces. This means that operation ii is "flip every card from the LiL_i-th to the RiR_i-th position from the left."

Output

If the fortune telling can succeed, print a single line to standard output containing the integer that is the minimum time needed to make it succeed. Otherwise, print −1-1.

Constraints

  • 1≤A≤100 0001 \le A \le 100\,000.
  • 1≤B≤100 0001 \le B \le 100\,000.
  • 1≤C≤100 0001 \le C \le 100\,000.
  • 1≤D≤100 0001 \le D \le 100\,000.
  • 1≤E≤100 0001 \le E \le 100\,000.
  • 1≤N≤100 0001 \le N \le 100\,000.
  • 1≤Li≤Ri≤A+B+C+D+E1 \le L_i \le R_i \le A + B + C + D + E (1≤i≤N1 \le i \le N).

Examples2

  1. Example 1

    Input
    1 2 3 4 5
    3
    2 3
    2 6
    4 10
    
    Expected output
    12
    
  2. Example 2

    Input
    1 1 1 1 1
    1
    1 1
    
    Expected output
    -1