Fire
Time limit2sMemory limit1024 MB
Pick the fewest helper shifts, each inside its [s_i, e_i] window on a repeating M-minute day, so that every minute of the day is covered. Print -1 if impossible.
Problem
In the old Baltic religion, a holy fire must keep burning. A priest called krivis is responsible for keeping it from going out. He has many trustworthy helpers called vaidilutės, and he wants a schedule for them to stoke and guard the fire. At every moment, some vaidilutė must be keeping the fire alive.
Input
Krivis uses his own time system, where each day has minutes. There are vaidilutės in his village. The -th vaidilutė can work within a window given by two integers and . is the earliest minute of the day she may start, and is the latest minute by which she must finish. Time is counted in minutes from the start of the day. When , she is willing to work overnight.
Krivis asks you to choose some vaidilutės and arrange their shifts. A chosen vaidilutė must start her shift no earlier than and end it no later than . Every shift is shorter than a full day. The chosen vaidilutės repeat their shifts every day.
Each handover raises the risk that the fire goes out. Krivis wants few handovers during the day, and he wants a schedule that uses as few vaidilutės as possible.
Output
Output one integer, the minimum number of vaidilutės to choose so that the fire is maintained at all times. If it is impossible, output .
Constraints
The first line contains and . Then lines follow, and the -th of them contains and .