#1842. [NOI 2019] Route to Home

[NOI 2019] Route to Home

Description

Duplicated of #6520. 「ICPC PacNW 2017 Div.1」David 的旅程

There are nn railway stations in the Kingdom of Cat, numbered from 11 to nn. Little cat is going home (at station nn) from station 11. There are mm trains in the kingdom, numbered from 11 to mm. For the ii-th train, it will depart station xix_i at time pip_i, and arrive at station station yiy_i directly at time qiq_i. At time 00, little cat is at station 11.

Little cat can go home by multiple transfers. One transfer is that for a pair of trains uu and vv, if yu=xvy_u = x_v and qupvq_u \leq p_v, then after taking train uu, little cat can wait at station yuy_u for pvqup_v - q_u time units, and take train vv at time pvp_v.

Little cat wants to minimize its anxiety, which is calculated as followed:

  • If little cat waits at some stations for t(t0)t\,(t \geq 0) time units, its anxiety will be increased by At2+Bt+CAt^2 + Bt + C, where A,B,CA, B, C are given constants.
    • Note that before little cat taking the first train at time tt, it has been waiting for tt time units at station 11.
  • If little cat arrives at station nn at time zz, its anxiety will be increased by zz.

Formally, if little cat takes kk trains totally, whose numbers are s1,s2,,sks_1, s_2, \cdots, s_k, little cat can get home if and only if:

  • xs1=1,ysk=nx_{s_1} = 1, y_{s_k} = n
  • ysj=xsj+1y_{s_j} = x_{s_{j + 1}} and qsjpsj+1q_{s_j} \leq p_{s_{j + 1}} hold for 1j<k1 \leq j < k

and its anxiety will be

$$q_{s_k} + (A \cdot p_{s_1}^2 + B \cdot p_{s_1} + C) + \sum_{j = 1}^{k - 1}\left(A(p_{s_{j + 1}} - q_{s_j})^2 + B(p_{s_{j + 1}} - q_{s_j}) + C\right) $$

Your task is to figure out the minimal anxiety.

Input

The first line contains 55 integers n,m,A,B,Cn, m, A, B, C.

Each of the next mm lines contains 44 integers pi,qi,xi,yip_i, q_i, x_i, y_i.

It is guaranteed that there is at least one way for little cat to go home.

Output

Print a single integer — the minimal anxiety.

Sample

3 4 1 5 10
1 2 3 4
1 2 5 7
1 2 6 8
2 3 9 10
94

There are 33 ways to go home.

  1. Take train 1,41, 4 in turn, and the anxiety is $10 + 1 \times 3^2 + 5 \times 3 + 10 + 1 \times (9-4)^2$ +5×(94)+ 5 \times (9-4) +10+ 10 =104= 104
  2. Take train 2,42, 4 in turn, and the anxiety is $10 + 1 \times 5^2 + 5 \times 5 + 10 + 1 \times (9-7)^2$ +5×(97)+ 5 \times (9-7) +10+ 10 =94= 94
  3. Take train 3,43, 4 in turn, and the anxiety is $10 + 1 \times 6^2 + 5 \times 6 + 10 + 1 \times (9-8)^2$ +5×(98)+ 5 \times (9-8) +10+ 10 =102= 102

So the minimal anxiety is 9494.

4 3 1 2 3
1 2 2 3
2 3 5 7
3 4 7 9
34

Limits And Hints

For all of the tests, 2n1052 \leq n \leq 10^5, 1m2×1051 \leq m \leq 2 \times 10^5, 0A100 \leq A \leq 10, 0B,C1060 \leq B, C \leq 10^6, 1xi,yin1 \leq x_i, y_i \leq n, xiyix_i \neq y_i, 0pi<qi1030 \leq p_i < q_i \leq 10^3.