Time

Time limit1sMemory limit128 MB

Summary
Given two valid dates and a period such as 3 months or 2 days, count how many whole periods aligned to unit boundaries fit between them using Gregorian leap year rules.
Level

Medium7 of 10

Topics
Math, Implementation, Simulation, Binary search
Solved
No attempts yet

Problem

A time point is given as six integers y,m,d,h,min,sy, m, d, h, \mathit{min}, s standing for year, month, day, hour, minute, and second, with 1970≤y<20301970 \le y < 2030, 0<m<130 < m < 13, 0<d<320 < d < 32, 0≤h<240 \le h < 24, 0≤min<600 \le \mathit{min} < 60, and 0≤s<600 \le s < 60.

Write a program that computes how many periods of a given length fit between two given time points. A period is given by a pair consisting of a positive integer and a word naming a time unit, one of year, month, day, hour, minute, or second.

Every 4th year is a leap year, except every 100th year, which is not, except every 400th year, which is. The length of a year varies according to leap years, and so does the length of February.

Time units always start as usual: a year starts on January 1st, a month starts on its 1st day, a day starts at 0 hours 0 minutes 0 seconds, and so on. A period ends after its last second. Hence you must report the maximum number of non-overlapping periods that fit exactly on these unit boundaries between the two time points.

Input

The input consists of blocks of lines. Each block has three lines. The first line of a block contains a time point D1D_1 and the second line a time point D2D_2; D1D_1 always precedes D2D_2. All numbers on a line are separated by one space. You may assume that every given time point is valid. The third line contains a time period, with exactly one space between the number and the word. After each block there is one empty line.

Output

Output one line for each block of the input. The line corresponding to a block contains a single integer telling how many of the specified periods are contained between the two given time points.

Examples1

  1. Example 1

    Input
    1997 12 31 23 59 59
    1998 1 1 0 0 0
    1 second
    
    2000 2 29 0 0 0
    2000 2 29 23 59 59
    1 day
    
    2000 2 29 0 0 0
    2000 3 1 0 0 0
    24 hour
    
    1996 12 31 20 30 0
    1997 1 1 7 30 0
    60 minute
    
    1996 12 31 20 30 0
    1997 1 1 7 30 0
    1 hour
    
    Expected output
    1
    0
    1
    11
    10