Jim is writing a program to statistically analyze card games. He needs to store many different card hands in memory efficiently. Each card has one of four suits and one of thirteen values. In his implementation, each hand is stored as a linked list of cards in a canonical order.
The cards are first ordered by suit: all the clubs (C) come first, followed by all the diamonds (D), then all the hearts (H), and finally all the spades (S). Within each suit, the cards are ordered by value: A, 2, 3, 4, 5, 6, 7, 8, 9, 10, J, Q, K. Each hand contains at most one copy of any given card.
The card hands use a lot of memory, so Jim decides to try a more efficient representation. Whenever two lists share a common tail (a common suffix at the end of the lists), they can be updated to share a single copy of that tail, and the other copy can be discarded. This process is repeated until no two lists share a common tail.
Your job is to report how many linked list nodes Jim needs to store all the card hands.
The input contains several test cases followed by a line containing a single 0. The first line of each case contains the number of card hands. Each of the following lines describes one card hand: it starts with the number of cards in the hand, followed by the cards, separated by spaces, in the canonical order defined above. Each card is written with its value first, followed by its suit (C, D, H, or S). Across all hands there are at most 100,000 cards in total.
For each test case, output a single line containing the number of linked list nodes needed to store all the lists.