This page is still under construction.

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

Lola's Schedule

Interview

Time limit1sMemory limit1024 MB

Summary
Choose the earliest first-dose time T within 480 minutes that minimizes how many of the recurring doses at T, T+X, T+2X, ... fall inside Lola's non-overlapping activities.
Level

Medium6 of 10

Topics
Intervals, Math, Brute force, Implementation
Solved
No attempts yet

Problem

Lola is an energetic girl with many interests, so each day is an ocean of possibilities for her, full of exciting activities she is more than willing to take part in. Unfortunately, many of the activities Lola takes part in happen in closed spaces, and because of that her vitamin D levels are slightly below ideal. To help, a doctor prescribed a vitamin supplement that she must take daily every X minutes.

Lola wrote an app to keep track of her activities. The main feature of the app is scheduling activities. Each activity consists of a title, its start time, and its end time. The app also allows creating one-off reminders, consisting simply of a title and the moment she should receive a single notification, and recurring reminders, consisting of a title, the time for the first notification, and how often the reminder should repeat. After buying the supplement, Lola wants to add a recurring reminder to the app that repeats every X minutes, so that she gets notified whenever it is time to take the supplement. Ideally, she prefers not to have to take the supplement while taking part in an activity. With such a busy schedule, she has a hard time coming up with the ideal moment to start taking it.

Your task is to help Lola choose the ideal time T after buying the supplement to take it for the first time. A time T is ideal if it is at most 8 hours past the moment when Lola bought the supplement, the number of times she needs to take the supplement that conflict with her activities is minimum, and no other time earlier than T would result in the same number of conflicts.

For clarity, suppose Lola needs to take the supplement every 30 minutes, bought it a certain day at 10:00, and has a single activity scheduled from 18:00 to 18:30 that day. If she chooses to take the supplement for the first time immediately, she would take it at 10:00, 10:30, . . . , 17:30, 18:00, 18:30, . . . . Thus she would have two conflicts with her activity (one at 18:00 and the other at 18:30).

Although Lola cannot wait until her activity ends to take the supplement for the first time (because of the 8 hours limit), she can reduce the number of conflicts by waiting 7 hours and 59 minutes. That way she would take the supplement at 17:59, 18:29, 18:59, . . . , with a single conflict at 18:29. Much better, isn't it? However, this is not ideal, because there is an earlier time that also produces a single conflict, which is waiting 1 minute and taking the supplement for the first time at 10:01, with a single conflict at 18:01. Since it is not possible to completely avoid conflicts, the ideal time in this case is taking the supplement for the first time 1 minute after buying it.

You will be given a list of activities, exported from the app Lola wrote, containing information about all her activities starting after the time when she bought the supplement. Lola already did some preprocessing of the data, and each activity is described by the number of minutes from the time she bought the supplement and its duration in minutes.

Input

The first line contains two integers N (1 ≤ N ≤ 104) and X (1 ≤ X ≤ 720), indicating that Lola has N upcoming activities exported from her app, and that she must take the supplement every X minutes. Each of the following N lines describes an activity with two integers S and D (1 ≤ S, D ≤ 105), meaning that the activity starts S minutes after buying the supplement and its duration is D minutes. Activities do not overlap, that is, given any pair of different activities, one of them ends strictly before the other starts, or vice versa.

Output

Output a single line with two integers T and C, indicating respectively the ideal time for taking the supplement for the first time, expressed in minutes since buying the supplement, and the number of conflicts that taking the supplement at this time will lead to.

Examples4

  1. Example 1

    Input
    1 30
    480 30
    
    Expected output
    1 1
    
  2. Example 2

    Input
    5 30
    195 30
    120 45
    240 30
    30 60
    300 180
    
    Expected output
    451 1
    
  3. Example 3

    Input
    4 720
    60 30
    150 75
    750 60
    1500 60
    
    Expected output
    0 0
    
  4. Example 4

    Input
    2 720
    1 479
    482 298
    
    Expected output
    0 1