#K39617. Minimum Running Distance

    ID: 26461 Type: Default 1000ms 256MiB

Minimum Running Distance

Minimum Running Distance

You are given a running track with n checkpoints numbered from 1 to n. Additionally, you are given m subpaths, each represented by a pair of integers (a, b), which indicates that you must cover all checkpoints from a to b (inclusive) in that subpath.

The running distance for a subpath is calculated as $$d = b - a + 1$$. The goal is to determine the minimum total running distance required to cover all given subpaths. Note that the subpaths do not overlap and the total distance is simply the sum of the distances of each individual subpath.

For example, if n = 10 and m = 3 with subpaths: (1, 5), (6, 8), and (9, 10), then the total running distance is:

$$ (5 - 1 + 1) + (8 - 6 + 1) + (10 - 9 + 1) = 5 + 3 + 2 = 10 $$

inputFormat

The input is read from standard input (stdin) and includes:

  • The first line contains two integers n and m separated by a space, where n is the total number of checkpoints and m is the number of subpaths.
  • The following m lines each contain two integers a and b representing the starting and ending checkpoints of a subpath.

outputFormat

Output a single integer to standard output (stdout) representing the minimum total running distance to cover all subpaths.

## sample
10 3
1 5
6 8
9 10
10

</p>