Marriage questions

Count the candidate intervals [L, R] in which all M daughters can each marry a distinct candidate they accept.

Hard8GraphTwo pointersNo attempts yetTime limit2sMemory limit256 MB

Problem

Long ago, in a country far away, a wise king had MM beautiful daughters. The time for them to marry finally came. The king sent word to NN neighboring kingdoms, and every kingdom sent one prince to marry a princess.

The king respected his daughters' wishes. He lined the candidates up, numbered them 11 to NN from the left, and asked each daughter which of these candidates she was willing to marry.

The king knew mathematics well, so deciding whether every daughter could be given a husband while every wish is respected was easy for him. Then he thought of a more interesting question: how many pairs (L,R)(L, R) (1LRN1 \le L \le R \le N) are there such that every daughter can be given a husband using only the candidates numbered LL to RR?

A candidate marries at most one daughter, and a daughter marries only a candidate she said she was willing to marry. Write a program that answers the king's question.

Input

The first line contains the integers NN, MM, and KK: the number of candidates, the number of daughters, and the number of lines describing the daughters' preferences. (1N300001 \le N \le 30\,000, 1M20001 \le M \le 2\,000, 1Kmin(N×M,100000)1 \le K \le \min(N \times M, 100\,000))

Each of the next KK lines contains the integers AiA_i and BiB_i, meaning that daughter BiB_i is willing to marry candidate AiA_i. (1AiN1 \le A_i \le N, 1BiM1 \le B_i \le M) All pairs are different.

Output

Print the number of pairs (L,R)(L, R) that satisfy the condition.

Hint

In the first example the pairs (1,3)(1, 3), (1,4)(1, 4), (1,5)(1, 5), and (2,5)(2, 5) satisfy the condition.