Clock Patience

No attempts yetTime limit1sMemory limit128 MB

Problem

Clock layout

Card sharp Albert "Foxy" Smith is writing a book on patience (solitaire) games. To double-check the examples in the book, he writes programs that find the optimal play for a given deal. The rules of Clock Patience are as follows.

The cards are dealt face down into a circle that represents a clock, with one pile at each hour position (1 through 12) and an extra pile in the centre. The first card is dealt to the one o'clock pile, the next to two o'clock, and so on clockwise, with every thirteenth card going to the centre pile. This produces thirteen piles, each holding four face-down cards.

The game then begins. The top card of the "king" pile (the last card dealt, in the centre) is turned face up to become the current card. Each move takes the current card, places it face up beneath the pile whose value it matches, and turns over the top card of that pile to become the new current card. For example, if the current card is an Ace it is placed under the "one" pile and the top card of that pile becomes the current card. The game ends when the pile named by the current card has no face-down cards left. You win if the entire deck is exposed.

Write a program that reads several shuffled decks and plays the game for each one.

Input

The input consists of several decks. Each deck is given as four lines of 13 cards, with the cards on a line separated by a single space. Each card is written as two characters: the rank (A, 2, 3, 4, 5, 6, 7, 8, 9, T, J, Q, K) followed by the suit (H, D, C, S). The input ends with a line containing a single #. Within a deck the cards are listed from the bottom of the pack to the top, so the first card dealt is the last card listed.

Output

For each deck output one line. The line contains the number of cards exposed during the game, written as two digits with a leading zero if necessary, then a comma, then the last card exposed, written in the same format as the input.