This page is still under construction.

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

Economic Phone Calls

Interview

Time limit1sMemory limit128 MB

Summary
Given a chronologically ordered call log with some entries marked important, keep the fewest entries so all important ones stay and the listed year-recovery rule still gives each kept call its original year.
Level

Medium6 of 10

Topics
Dynamic programming, Greedy, Sorting, Implementation
Solved
No attempts yet

Problem

An old phone you own has a built-in memory that logs every call you receive. For each call it stores the date (month and day) and the time (hour and minute) together with the caller's number. Because memory was expensive back then, only a limited number of calls can be stored.

The log is almost full, so you want to delete some entries. When choosing which entries to remove, two rules must hold:

  1. Some entries are important and must be kept.
  2. For every entry you keep, you must still be able to recover the year of the call, even though the phone does not store it. The recovery procedure is described below.

Determine the minimum number of entries that must be kept so that both rules are satisfied.

Recovering the year

Given a chronological list of call timestamps (each a month, day, hour, and minute), the year of every call is reconstructed as follows:

  1. The last call in the list took place in the current year.
  2. Take a call with timestamp tt and the call immediately before it with timestamp t′t'. If t′<tt' < t, both calls happened in the same year. If t′≥tt' \ge t, the earlier call happened in the previous year.
  3. Apply rule 2 repeatedly, moving backwards through the list.

This procedure is not always correct in general, but you may assume it produces the true year for the given input. After deleting entries, applying the same procedure to the shortened log must yield the same year for every remaining call.

Input

The input contains several test cases. Each test case begins with a line holding the number of entries nn in the log, where 1≤n≤10001 \le n \le 1000. Each of the next nn lines contains one entry.

Every entry has the form mm:dd:HH:MM number ±: the month mm, day dd, hour HH, minute MM, the caller's number (1 to 16 digits), and finally a mark, + for a call you definitely want to keep or - for any other call. The entries are given exactly as stored by the phone, i.e. sorted by the time each call was received (the last entry is the most recent).

You may assume the recovery procedure above yields the correct year for every call.

The last test case is followed by a line containing a single 0, which is not processed.

Output

For each test case, print on its own line the minimum number of entries that must be kept so that both rules hold. In particular, applying the recovery procedure to the kept entries must assign every one of them the same year it had in the original log.

Examples1

  1. Example 1

    Input
    7
    12:31:23:59 0123456789012345 +
    07:21:19:00 1337 -
    01:01:00:00 0987654321 -
    07:21:14:00 1337 -
    11:11:11:11 11111111111 +
    01:01:00:00 0123456789 +
    01:01:00:00 0987654321 -
    0
    
    Expected output
    6