cswork / OAOpen practice workspace

OpenAIOA #2

Bacteria Coexistence (bioHazard)

Estimated MediumJudge-ready

Topics (AI-labelled)Two PointersHashing

The samples are arranged by number from 1 to n. The two samples of any conflicting pair cannot both appear in a range, and direction does not matter for a conflict. Count the valid non-empty contiguous ranges; the samples cannot be reordered. Duplicate conflicts are not subtracted twice.

Input

The first line contains n m, followed by m lines each with a b, denoting a conflicting pair. This site uses 1≤n≤200000, 0≤m≤200000, 1≤a,b≤n, and a≠b; duplicates and reversed duplicates are allowed.

Output

The number of valid ranges, as a 64-bit integer.

Example 1

Input:

3 3
2 3
1 3
3 1

Output:

4

Example 2

Input:

4 0

Output:

10

Example 3

Input:

4 3
1 4
4 1
2 3

Output:

6

Example notes

In Example 1, the valid ranges are the three single points and [1,2], for 4 in total. In Example 2, all 10 ranges are valid. In Example 3, the duplicated reversed relation counts as one, and there are 6 valid ranges in total.

Editorial

Loading solution…