#P2652. Minimum Card Replacements for a Straight Flush

    ID: 15918 Type: Default 1000ms 256MiB

Minimum Card Replacements for a Straight Flush

Minimum Card Replacements for a Straight Flush

You are given n playing cards. Each card has a suit and a value. A straight flush is defined as a sequence of n cards that:

  • All have the same suit.
  • The values form a sequence of n consecutive integers. In other words, if the smallest value is \(x\), then the sequence must be \(x, x+1, \dots, x+n-1\).

You can replace any card with any other card (i.e. change both its suit and value). Your task is to determine the minimum number of cards that need to be replaced so that all n cards form a straight flush.

Note: The consecutive integer sequence is expressed in \(\LaTeX\) as \(x, x+1, \dots, x+n-1\).

inputFormat

The first line contains a single integer n, the number of cards. Each of the following n lines contains a card description with a suit and an integer value separated by a space.

The suit is given as one of the characters: C (Clubs), D (Diamonds), H (Hearts), or S (Spades).

outputFormat

Output a single integer denoting the minimum number of cards that must be replaced so that the n cards form a straight flush.

sample

5
H 1
H 2
H 3
H 4
H 8
1