Hands of Poker

Time limit1sMemory limit128 MB

Problem

The standard 52-card deck consists of 52 cards divided into 4 suits: clubs, diamonds, hearts, and spades. For each suit there are 13 ranks: 2, 3, 4, 5, 6, 7, 8, 9, 10, jack, queen, king, and ace, listed from the lowest to the highest.

A card is denoted by its rank ('2'...'9' for 2...9, 'T' for 10, 'J' for jack, 'Q' for queen, 'K' for king, and 'A' for ace) followed by its suit ('C' for clubs, 'D' for diamonds, 'H' for hearts, and 'S' for spades). Cards are ordered by their ranks; the suit does not play a role in this ordering.

A poker hand is a set of five distinct cards. Each hand has a certain ranking. A hand with a higher ranking beats a hand with a lower ranking. Two hands of the same ranking are compared using a tie-breaking rule specific to that ranking — either one of them beats the other, or they are tied.

The list of poker rankings is given below, from the worst to the best. If a hand satisfies several rankings, only the best one is considered.

  • High Card — Does not fit any ranking below. When comparing two High Card hands, the highest cards are compared first; if tied, the second-highest cards are compared, and so on. (For example, QS JH 9C 7H 3D.)
  • One Pair — Two cards of the same rank. A pair of higher rank beats a lower pair. On a tie, the remaining three cards break the tie, compared in descending order of rank (as in High Card). (For example, 6D 6H QD 9H 4S.)
  • Two Pairs — Two pairs of cards of the same rank. When comparing two such hands, the higher pair is compared first, then the lower pair, and finally the rank of the fifth card. (For example, JH JS TS TD 8S.)
  • Three of a Kind — Three cards of the same rank. A higher three-of-a-kind beats a lower one. On a tie, the remaining two cards break the tie, compared in descending order. (For example, 5S 5H 5D JH 6D.)
  • Straight — Five cards of consecutive rank. An ace may count either above a king or below a two, but not both, so wrapping is not allowed. Two straights are compared by the rank of the highest card (for A 2 3 4 5, the highest card is 5). (For example, QH JC TH 9D 8D.)
  • Flush — Five cards of the same suit. Two flushes are compared by the highest card, then the second-highest, and so on (as in High Card). (For example, AS JS 8S 6S 5S.)
  • Full House — Three cards of one rank and two cards of another rank. When comparing two full houses, the rank of the three cards is compared first, then the rank of the two cards. (For example, 7S 7H 7C JC JH.)
  • Four of a Kind — Four cards of the same rank. Two four-of-a-kinds are compared first by the rank of the four cards; on a tie, the rank of the fifth card breaks the tie. (For example, 4C 4D 4H 4S TD.)
  • Straight Flush — A hand that is both a Straight and a Flush. The tie-breaker is the same as for a Straight. (For example, TH 9H 8H 7H 6H.)

Consider the set $H$ of all poker hands. There is an evaluation function $v : H \to {1, \dots, 7462}$ such that for any two poker hands $a$ and $b$, hand $a$ beats hand $b$ if and only if $v(a) > v(b)$. Exactly one such evaluation function exists.

Given a poker hand $a$, find the value of $v(a)$.

Input

The input contains a space-separated list of five distinct card descriptions. Each card is described by two characters: its rank followed by its suit. Ranks are denoted by '2'...'9', 'T', 'J', 'Q', 'K', and 'A' (in ascending order). Suits are denoted by 'C', 'D', 'H', and 'S'.

Output

Output the value of the evaluation function $v(a)$ for the given hand $a$.