Marriage questions
Once upon a time in a country far, far away, the wise king had m beautiful daughters. At last, the time for them to get married has come. King sent a message in n neighboring kingdoms, so each of them sent a prince to marry one of the princesses.
As a loving father the king considered his daughters’ opinions. First of all he arranged young men in a line, enumerated them with numbers from 1 to n, and asked each of his daughters, with whom from those candidates she was agree to get married.
King had an excellent mathematical background, so it was easy for him to check whether it is possible to find a husband for each of his daughters in such way, that requests of each daughter were satisfied. But suddenly the king thought about more interesting question: how many pairs (l, r) (1 ≤ l ≤ r ≤ n) are there, such that it is possible to assign a husband for each daughter, using only candidates with numbers from l to r inclusive?
Help king to find an answer to his question!
Input
First line contains three integers n, m and k (1 ≤ n ≤ 30 000, 1 ≤ m ≤ 2 000, 1 ≤ k ≤ min(n ∙ m, 100 000)) - number of candidates, number of girls and number of lines, describing girls’ preferences respectively.
In next k lines there are two integers A[i]
, B[i]
(1 ≤ A[i]
≤ n, 1 ≤ B[i]
≤ m), it means that girl B[i]
likes candidate A[i]
. All pairs are different.
Output
Output the number of ways king can choose a pair (l, r) to satisfy the problem statement.