#C8977. Minimum Road Connection Length

    ID: 53018 Type: Default 1000ms 256MiB

Minimum Road Connection Length

Minimum Road Connection Length

In Graphland, there are n cities numbered from 1 to n. The cities are connected by roads of equal length (assumed to be 1 unit each). Your task is to calculate the minimum total length of roads required to connect each city to its immediate next city, so that city 1 is connected to city 2, city 2 is connected to city 3, and so on, until city n. This is mathematically given by the formula \(n - 1\), where \(n\) is the number of cities.

The input also contains the list of roads provided for record purposes. However, the optimal strategy is always to connect cities in sequential order (i.e. 1 to 2, 2 to 3, ..., n-1 to n), resulting in a total road length of \(n - 1\).

Note: The provided road connections do not affect the result.

inputFormat

The first line of input contains two space-separated integers n and m, where:

  • n (2 ≤ n ≤ 200,000) is the number of cities.
  • m is the number of road descriptions.

Each of the following m lines contains two space-separated integers representing a road that connects two cities.

outputFormat

Output a single integer representing the minimum total length of roads needed to connect all the cities in sequential order.

## sample
5 4
1 2
2 3
3 4
4 5
4