{"raw_statement":[{"iden":"problem statement","content":"A _fantastic IS_ is an integer sequence of length $N$ whose every element is between $1$ and $M$ (inclusive) that satisfies the following condition.\n\n*   For every integer $i$ such that $1 \\le i \\le K$, one of the following holds.\n    *   $A_{P_i} < X_i$ and $A_{Q_i} < Y_i$;\n    *   $A_{P_i} = X_i$ and $A_{Q_i} = Y_i$;\n    *   $A_{P_i} > X_i$ and $A_{Q_i} > Y_i$.\n\nDetermine whether a fantastic IS exists. If it does, find the minimum possible sum of the elements in a fantastic IS."},{"iden":"constraints","content":"*   $1 \\le N,M,K \\le 2 \\times 10^5$\n*   $1 \\le P_i,Q_i \\le N$\n*   $1 \\le X_i,Y_i \\le M$\n*   $P_i \\neq Q_i$\n*   All values in input are integers."},{"iden":"input","content":"Input is given from Standard Input in the following format:\n\n$N$ $M$ $K$\n$P_1$ $X_1$ $Q_1$ $Y_1$\n$P_2$ $X_2$ $Q_2$ $Y_2$\n$\\vdots$\n$P_K$ $X_K$ $Q_K$ $Y_K$"},{"iden":"sample input 1","content":"3 4 3\n3 1 1 2\n1 1 2 2\n3 4 1 4"},{"iden":"sample output 1","content":"6\n\n$A=(2,3,1)$ fully satisfies the condition and thus is a fantastic IS, whose sum of the elements is $6$.\nThere is no fantastic IS whose sum of the elements is less than $6$, so the answer is $6$."},{"iden":"sample input 2","content":"2 2 2\n1 1 2 2\n2 1 1 2"},{"iden":"sample output 2","content":"\\-1\n\nThere is no fantastic IS, so $-1$ should be printed."},{"iden":"sample input 3","content":"5 10 10\n4 1 2 7\n5 1 3 2\n2 9 4 4\n5 4 2 9\n2 9 1 9\n4 8 3 10\n5 7 1 5\n3 5 1 2\n3 8 2 10\n2 9 4 8"},{"iden":"sample output 3","content":"12"}],"translated_statement":null,"sample_group":[],"show_order":["default"],"formal_statement":null,"simple_statement":null,"has_page_source":true}