Under construction

Still being built. Things may change

Sleep Pattern

Interview

Time limit1sMemory limit1024 MB

Summary
Given weekday sleep intervals on a Mon-Fri timeline, compute the minimum weekend hours needed so total weekly sleep reaches T, or report that even 48 hours is not enough.
Level

Medium4 of 10

Topics
Implementation, Math, Sorting, Simulation
Solved
No attempts yet

Statement

Minty, who got used to staying home because of social distancing and a leave of absence, is worried that their sleep pattern keeps breaking down.

For their health, Minty has decided to sleep at least TT hours a week from now on. Sleeping and waking at the same time every day is still too hard, so Minty writes down the times they slept on weekdays and plans to catch up on the weekend. Once Minty falls asleep, they sleep at least 1 hour, and sometimes they are so sleepy that they fall right back asleep the moment they wake up.

Now the weekdays are over, and Minty has a 48-hour weekend to sleep as much as they want. Minty looks at the records written on the weekdays and tries to compute how many hours to sleep over the weekend.

Given the times Minty fell asleep and woke up during the weekdays, output the minimum number of hours Minty has to sleep on the weekend.

Input

The first line gives the integer TT, the number of hours to sleep per week, and the integer NN, the number of times Minty slept during the weekdays.

The next NN lines give the times Minty slept. Each line has D1D_1, H1H_1, D2D_2, H2H_2 in that order: the time Minty fell asleep and the time Minty woke up. D1D_1 is the day of falling asleep, H1H_1 is the hour of falling asleep, D2D_2 is the day of waking up, and H2H_2 is the hour of waking up. A day is one of Mon, Tue, Wed, Thu, Fri, meaning Monday, Tuesday, Wednesday, Thursday, Friday. An hour is an integer from 0 to 23, meaning the hours from 0 o'clock to 23 o'clock.

The sleeps do not overlap each other, but since Minty can fall right back asleep after waking, the falling-asleep time of one sleep can equal the waking time of another. The start and end of every sleep are always on weekdays, and the input is given sorted by falling-asleep time in increasing order. Note that Minty may not sleep at all during the weekdays.

Output

Output the number of additional hours Minty has to sleep on the weekend. If sleeping no hours on the weekend is enough, output 0; if sleeping the entire weekend is not enough, output -1.

Constraints

  • 0≤T≤1680 \le T \le 168
  • 0≤N≤100 \le N \le 10

Examples3

  1. Example 1

    Input
    56 6
    Mon 22 Tue 2
    Tue 15 Tue 19
    Tue 19 Wed 4
    Wed 17 Thu 1
    Thu 12 Thu 17
    Fri 4 Fri 12
    
    Expected output
    18
    
  2. Example 2

    Input
    50 5
    Mon 0 Mon 11
    Tue 2 Tue 12
    Wed 0 Wed 10
    Thu 3 Thu 15
    Fri 11 Fri 23
    
    Expected output
    0
    
  3. Example 3

    Input
    120 5
    Mon 0 Mon 11
    Tue 2 Tue 12
    Wed 0 Wed 10
    Thu 3 Thu 15
    Fri 11 Fri 23
    
    Expected output
    -1