Codeforces Round 238 (Div. 1) |
---|

Finished |

Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests.
If you've seen these problems, a virtual contest is not for you - solve these problems in the archive.
If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive.
Never use someone else's code, read the tutorials or communicate with other person during a virtual contest.

implementation

math

two pointers

*2800

No tag edit access

The problem statement has recently been changed. View the changes.

×
E. Hamming Triples

time limit per test

1 secondmemory limit per test

256 megabytesinput

standard inputoutput

standard outputLittle Chris is having a nightmare. Even in dreams all he thinks about is math.

Chris dreams about *m* binary strings of length *n*, indexed with numbers from 1 to *m*. The most horrifying part is that the bits of each string are ordered in either ascending or descending order. For example, Chris could be dreaming about the following 4 strings of length 5:

The Hamming distance *H*(*a*, *b*) between two strings *a* and *b* of length *n* is the number of positions at which the corresponding symbols are different.

Сhris thinks that each three strings with different indices constitute a single triple. Chris's delusion is that he will wake up only if he counts the number of such string triples *a*, *b*, *c* that the sum *H*(*a*, *b*) + *H*(*b*, *c*) + *H*(*c*, *a*) is maximal among all the string triples constructed from the dreamed strings.

Help Chris wake up from this nightmare!

Input

The first line of input contains two space-separated integers *n* and *m* (1 ≤ *n* ≤ 10^{9}; 3 ≤ *m* ≤ 10^{5}), the length and the number of strings. The next *m* lines contain the description of the strings. The *i*-th line contains two space-separated integers *s*_{i} and *f*_{i} (0 ≤ *s*_{i} ≤ 1; 1 ≤ *f*_{i} ≤ *n*), the description of the string with index *i*; that means that the first *f*_{i} bits of the *i*-th string are equal to *s*_{i}, and the remaining *n* - *f*_{i} bits are equal to 1 - *s*_{i}. There can be multiple equal strings in Chris's dream.

Output

Output a single integer, the number of such string triples among the given that the sum of the Hamming distances between the strings of the triple is maximal.

Examples

Input

5 4

0 3

0 5

1 4

1 5

Output

3

Input

10 4

1 5

0 5

0 5

1 5

Output

4

Codeforces (c) Copyright 2010-2024 Mike Mirzayanov

The only programming contests Web 2.0 platform

Server time: Apr/13/2024 09:32:24 (i1).

Desktop version, switch to mobile version.

Supported by

User lists

Name |
---|