This page is still under construction.

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

Reading Room Distancing

Time limit1sMemory limit512 MB

Summary
Simulate seat assignments for reservation holders who each pick the seat farthest from any occupied seat, then report how many minutes Mingyu's preferred seat stays free.
Level

Medium6 of 10

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

Problem

Social distancing is in full swing because of the coronavirus. But even before these times, there was a place where distancing was observed well: the reading room.

Mingyu, who worked as a manager at a reading room, discovered something surprising. People always try to sit farther away from each other.

Mingyu observed this and organized it as follows.

  1. People prefer the seat that is farthest from the person sitting closest to them. If nobody is using the reading room, they prefer seat number 1 the most.
  2. If rule 1 cannot decide, they prefer the seat with the smallest seat number among the farthest seats.

Mingyu, who has worked as a reading room manager for a long time, has a preferred seat. But Mingyu is very timid, so he wants to avoid people being unable to use the seat they wanted to use because of him.

The reading room where Mingyu works is open from 09:00 to 21:00, and it runs strictly by reservation, so Mingyu can know when each person will use the reading room.

Based on this information, Mingyu wants to calculate how much he can use his preferred seat.

Input

The first line gives the number of reading room seats N, the number of reading room reservation holders T, and the seat number P that Mingyu likes, separated by spaces. (1 ≤ N ≤ 100, 1 ≤ T ≤ 500, 1 ≤ P ≤ N)

The next T lines give the reading room entry time and exit time in the form HHMM HHMM.

(0900 ≤ HHMM ≤ 2100, and there is no case where the exit time is earlier than the entry time, such as 0910 0900)

Output

Print the total number of minutes for which Mingyu can use his preferred seat.

Constraints

All seats in the reading room start empty.

If reservations start at the same time, the person with the shorter usage time is seated first.

There is no state in which a reservation holder in the reading room reservation list cannot sit because there are no seats.

Mingyu wants to calculate how much he can use his preferred seat, so the reservation holders do not affect his use of seats.

Examples2

  1. Example 1

    Input
    5 6 1
    0915 0930
    0940 2040
    0910 0920
    2040 2050
    2043 2047
    2044 2046
    
    Expected output
    40
    
  2. Example 2

    Input
    8 6 3
    2000 2020
    2020 2030
    0900 2100
    0910 2100
    0920 2100
    0930 2000
    
    Expected output
    720