Problem Statement

There are N students in 302, all seated in a row. They are numbered in order of their seats, so the leftmost person is 1 and the rightmost is N.

There are currently M friendships among the students of 302. Each friendship involves two people and is mutual. Two people are considered to know each other if they can describe each other as "a friend", "a friend of a friend", or using any such chains of friendships.

Recently, fights have been breaking out in 302. The teacher thinks that it is because people who know each other talk a lot in class, and thus annoy the people in between them who do not know them. Thus, the teacher has ordered the class to make friends with more people such that for any two people L and R who know each other, everyone in between them must also know them. This will surely return harmony to the class, thought the teacher.

However, different people in 302 don't really get along very well. Thus, they do not like to make friends. Help them find the minimum number of friendships they must form to create the "harmonious class" that the teacher wants.


The first line of input contains two integers, N and M.

The next M lines of input contain two integers each, Ai and Bi, denoting a friendship between the people numbered as such.


The first and only line of output should contain a single integer, denoting the minimum number of new friendships that must be formed in 302.


For all subtasks, 3 ≤ N ≤ 200000 and 1 ≤ M ≤ 200000. It is guaranteed that each pair (Ai, Bi) only appears once and AiBi for all i.

Subtask 1 (20%): M = 1

Subtask 2 (80%): There are no more constraints.

Subtask 3 (0%): Sample test cases.

Sample Input 1

6 5
1 3
1 4
1 5
1 6
3 6

Sample Output 1


Sample Input 2

5 3
2 1
3 5
4 5

Sample Output 2



For Sample 1, the class is not harmonious as 2 is in between 1 and 3 (who know each other), but does not know either 1 or 3. However, if 2 makes friends with 4, the class becomes harmonious.

For Sample 2, the class is already harmonious, thus no more friends need to be made.

Submitting .cpp to 'harmony'

You're not logged in! Click here to login

Time Limit: 1 Seconds
Memory Limit: 1024MB
No. of ACs: 2
Your best score: 0
Source: Dunjudge Archive

Subtask Score
1 20
2 80
3 0