At a warehouse, containers need to be organized in numbered slots. The containers are divided into three types: small (denoted as ), medium (), and large (). The task is to arrange all small containers first, followed by medium containers, and finally large containers.
Since moving containers requires time and effort, you need to find the minimum number of swaps necessary to organize them.
You are given a sequence of symbols, each representing one of three types: , , or . For convenience, the sequence is provided in blocks of consecutive identical symbols. Your goal is to determine the minimum number of swaps needed to sort the sequence in ascending order by letters.
The first line contains a single natural number (1 \leq N \leq 1000). The next lines each describe a block: a letter ('c', 'f', or 'm'), followed by a space and a natural number (1 \leq K \leq 1000), the size of the block.
Print a single number — the minimum number of swaps required.
Example 1