Under construction

Still being built. Things may change

Club Room Project (Large)

Time limit1sMemory limit512 MB

Summary
For each action, remove all walls between rooms x and y; print how many connected room blocks remain.
Level

Medium4 of 10

Topics
Union-find, Array
Solved
No attempts yet

Problem

Byeongchan wants a club room, and the office gave him a chance to take one of NN rooms. The building is a single straight corridor with NN rooms in a row. The leftmost room is room 1, the numbers increase by one to the right, and the rightmost room is room NN. Between every pair of neighboring rooms stands one wall that separates them.

Other people want a club room too, but there were enough rooms, so Byeongchan was not worried. Then the Big Jongbin villain showed up and started knocking down the walls of the building. The villain follows these rules.

  • The villain picks two rooms with x<yx < y and knocks down every wall between room xx and room yy.
  • When the wall between two rooms falls, the two rooms become one room.
  • A wall that is already gone is skipped.
  • The villain does not want the building to collapse, so the wall on the left side of room 1 and the wall on the right side of room NN, the two walls that face the outside, are never knocked down.

As the number of club rooms dropped, Byeongchan got nervous. Given the number of rooms NN and the number of actions MM, count the club rooms that remain after all actions are done.

Input

The first line contains a positive integer NN (2≤N≤10000002 \le N \le 1000000), the number of club rooms. The second line contains a non-negative integer MM (0≤M≤50000 \le M \le 5000), the number of actions the villain takes. Each of the next MM lines contains one action as two positive integers xx and yy (1≤x<y≤N1 \le x < y \le N). One action knocks down every wall between room xx and room yy.

The villain is clumsy, so the same action can appear several times.

Output

Print on one line the number of club rooms that remain after all actions are done.

Hint

Suppose N=5N = 5 and the actions are (1,2)(1, 2) and then (2,4)(2, 4). The first action joins rooms 1 and 2, so the rooms are (1,2)(1, 2), (3)(3), (4)(4), (5)(5). The second action joins rooms 2, 3 and 4, so the rooms are (1,2,3,4)(1, 2, 3, 4), (5)(5). Two club rooms remain.

Examples4

  1. Example 1

    Input
    5
    2
    1 2
    2 4
    
    Expected output
    2
    
  2. Example 2

    Input
    5
    1
    1 5
    
    Expected output
    1
    
  3. Example 3

    Input
    10
    3
    1 3
    2 4
    5 8
    
    Expected output
    4
    
  4. Example 4

    Input
    100
    0
    
    Expected output
    100