This page is still under construction.

Parts of this page are still being built. What you see may change.

Surveillance Cameras

Interview

Time limit4sMemory limit512 MB

Summary
Choose the fewest circular arcs that cover all N rooms on a circle, or report impossible.
Level

Medium6 of 10

Topics
Greedy, Intervals, Sorting
Solved
No attempts yet

Problem

N rooms sit on a circle. K cameras each watch a contiguous arc, possibly wrapping: if ai≤bia_i \le b_i then rooms aia_i through bib_i; if ai>bia_i > b_i then rooms 11 through bib_i and aia_i through N.

Find the minimum number of cameras needed to watch every room. Print impossible if it cannot be done.

Input

  • Line 1: NN, KK.
  • Next KK lines: aia_i, bib_i.

Output

Minimum number of cameras, or impossible.

Examples5

  1. Example 1

    Input
    100 7
    1 50
    50 70
    70 90
    90 40
    20 60
    60 80
    80 20
    
    Expected output
    3
    
  2. Example 2

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

    Input
    8 2
    1 8
    3 6
    
    Expected output
    1
    
  4. Example 4

    Input
    20 5
    5 8
    9 12
    16 5
    16 4
    7 10
    
    Expected output
    impossible
    
  5. Example 5

    Input
    30 6
    28 1
    3 7
    12 17
    24 7
    28 5
    9 21
    
    Expected output
    impossible