atcoder#ARC146D. [ARC146D] >=<

[ARC146D] >=<

Score : 800800 points

Problem Statement

A fantastic IS is an integer sequence of length NN whose every element is between 11 and MM (inclusive) that satisfies the following condition.

  • For every integer ii such that 1iK1 \le i \le K, one of the following holds.- APi<XiA_{P_i} < X_i and AQi<YiA_{Q_i} < Y_i;
    • APi=XiA_{P_i} = X_i and AQi=YiA_{Q_i} = Y_i;
    • APi>XiA_{P_i} > X_i and AQi>YiA_{Q_i} > Y_i.
  • APi<XiA_{P_i} < X_i and AQi<YiA_{Q_i} < Y_i;
  • APi=XiA_{P_i} = X_i and AQi=YiA_{Q_i} = Y_i;
  • APi>XiA_{P_i} > X_i and AQi>YiA_{Q_i} > Y_i.

Determine whether a fantastic IS exists. If it does, find the minimum possible sum of the elements in a fantastic IS.

Constraints

  • 1N,M,K2×1051 \le N,M,K \le 2 \times 10^5
  • 1Pi,QiN1 \le P_i,Q_i \le N
  • 1Xi,YiM1 \le X_i,Y_i \le M
  • PiQiP_i \neq Q_i
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

NN MM KK

P1P_1 X1X_1 Q1Q_1 Y1Y_1

P2P_2 X2X_2 Q2Q_2 Y2Y_2

\vdots

PKP_K XKX_K QKQ_K YKY_K

Output

If a fantastic IS exists, print the minimum possible sum of the elements in a fantastic IS; otherwise, print 1-1.

3 4 3
3 1 1 2
1 1 2 2
3 4 1 4
6

A=(2,3,1)A=(2,3,1) fully satisfies the condition and thus is a fantastic IS, whose sum of the elements is 66.

There is no fantastic IS whose sum of the elements is less than 66, so the answer is 66.

2 2 2
1 1 2 2
2 1 1 2
-1

There is no fantastic IS, so 1-1 should be printed.

5 10 10
4 1 2 7
5 1 3 2
2 9 4 4
5 4 2 9
2 9 1 9
4 8 3 10
5 7 1 5
3 5 1 2
3 8 2 10
2 9 4 8
12