eolymp
bolt
Try our new interface for solving problems
Problems

Roman Cities

Roman Cities

A well-known software development company has been commissioned by the Archaeological Society. One of the modules has to help the archaeologists to process data about the ruins of buildings they have found during their excavations of ancient cities. Development of this module has been assigned to Vasya. Vasya, being a seasoned programmer, at once noticed that the module would need a database to contain the descriptions of the ruins and the estimated construction times of the buildings. It would be all fine, but suddenly the manager got the genial idea that since the database describes Roman ruins, the years of construction should be stored in the Roman number system. Now Vasya is wondering how many symbols he needs to set aside for each year number field in the database. According to the functional specification, the software module must be able to handle years from \textbf{A} to \textbf{B} (inclusive). Help Vasya determine the minimal number of characters sufficient for storing any year number in the range from \textbf{A} to \textbf{B}. \InputFile he only input line contains the descriptions of the years \textbf{A} and \textbf{B}, separated by the "\textbf{-}". sign. A description of a year consists of one to four decimal digits (the number of the year), followed by either \textbf{AD} (Anno Domini, the current era) or \textbf{BC} (Before Christ, before the current era). In both directions the years are numbered starting from 1. It is known that \textbf{753BС} ≤ \textbf{A} ≤ \textbf{B} ≤ \textbf{2012AD}. \OutputFile The output should consist of a single integer, the minimal number of characters that have to be reserved in the database for the year number. \Note It is well known that the Roman number system uses the following seven letters for digits: \textbf{I - 1}, \textbf{V - 5}, \textbf{X - 10}, \textbf{L - 50}, \textbf{C - 100}, \textbf{D - 500} and \textbf{M - 1000}. Natural numbers are written by repeating these digits. For correct representation of a larger integer in the Roman system, first the thousands, then the hundreds, then the tens and finally the ones are written. In any of the positions, some digits (\textbf{I}, \textbf{X}, \textbf{C}, \textbf{M}) may be repeated, but no more than three times. If in any position a digit with lower or equal value is to the right of a digit of higher value, the lower value is added to the higher one. Only \textbf{I}, \textbf{X}, \textbf{C} and \textbf{M} may be added and there may be no more than three equal-valued digits in any position. If, however, a digit with lower value is to the left of a digit with higher value, the lower value is subtracted from the higher one. There are only six subtractions allowed: \textbf{IV - 4}, \textbf{IX - 9}, \textbf{XL - 40}, \textbf{XC - 90}, \textbf{CD - 400} and \textbf{CM - 900}. Any other combinations are not allowed. So, for example, \textbf{99} has to be written as \textbf{XCIX}, not as \textbf{IC}. It is also important to remember that the Romans did not use the \textbf{AD}/\textbf{BC} concepts. Instead, they counted the years from the mythical founding of Rome (Anno Urbis Conditae - \textbf{753BC}). Some examples of Roman year numbers: \begin{itemize} \item \textbf{753 BC - 1 AUC - I} \item \textbf{1 BC - 753 AUC - DCCLIII} \item \textbf{1 AD - 754 AUC - DCCLIV} \item \textbf{2012 AD - 2765 AUC - MMDCCLXV} \end{itemize}
Time limit 1 second
Memory limit 256 MiB
Input example #1
753BC-747BC
Output example #1
3
Source NEERC Western Subregional Contest 2012