Koumakan's daily life

Time Limit: 2 Seconds

Memory Limit: 65536 KB

Description

Koumakan's daily life is peace as usual, but Izayoi Sakuya feels a little bored.So she assembles his friends to play mahjong.But she soon finds that not only Scarlet sisters,Patchouli is also very good at mahjong. Among the table, there is a only one green hand:herself. After losing for times, her mood boils. She wants to win, even cheating by using magic. Her magic could help her to change the tile to she wants. But If it was found, she would lose the game again directly, the more times she uses magic, the higher possibility of being found cheating. Now, you have known the cards you have, please help Izayoi to find the minimum steps to get the ready hand.

Mahjong is a game of skill, strategy and calculation and involves a certain degree of chance. In this problem, we concentrate on Japanese Mahjong, a variation of mahjong. For brief, all of the word mahjong mentioned following refer to Japanese Mahjong.

Japanese mahjong is usually played with 136 tiles, which can be organized into several categories:

  • Suited tiles. All suited tiles are of a rank and a suit.There are three suits of tiles, with ranks ranging from one to nine. There are four tiles of each rank and suit combination, thus there are 36 tiles in a suit, and 108 suited tiles in total.
    • The circle suit
    • The bamboo suit
    • The character suit
  • Honor tiles. Honor Tiles are tiles that do not have a rank or suit. They are divided into two categories. There are four types of Wind tiles and three types of Dragon tiles, with four of each type of honor tile. Thus, there are 16 wind tiles and 12 Dragon tiles for 28 honor tiles.
    • Wind tiles. The Wind tiles consist of four kinds of tile: East, South, West, and North.
    • Dragon tiles. The Dragon titles consist of three types of tile: Red, Green, White.

A winning hand consists of fourteen tiles, which is made of four melds (a specific pattern of three pieces) and the eyes (a pair of two identical pieces). The definition of melds and eyes is given as followed:

  • Melds are listed as followed:
    • Pong is a set of three identical tiles. You can form a pong with any tile. The tiles must be identical (you cannot mix suits). For example:
    • Kong is a set of four identical tiles, which is similar to Pong. For example:
    • Chow is a meld of three suited tiles in sequence. The meld must be in absolute numerical sequence. There is no skipping of numbers, nor does 9 loop around to 1. The sequence must be in the same suit. Honours cannot be used to make chows. For example:
  • Eyes, also known as a pair, are two identical tiles which are a component to the standard hand. For example:

In this problem, we introduce two abnormal kinds of winning hand different from the normal winning hand, which consist of four melds and one eyes.

  1. Seven Pairs: the hand consists of seven different pairs. No two pairs are the same. For example:
  2. Thirteen Orphans: the hand consists of thirteen kinds of tiles: Circle One, Circle Nine, Bamboo One, Bamboo Nine, Character One, Character Nine, East, South, West, North, Red, Green and White. Only one kind of them are two tiles, while the others are all only one tile. For example:
  3. Normal: the hand consists of four melds and one eyes. For example:

When a hand is one tile short of winning, the hand is said to be a ready hand, or more figuratively, "on the pot".

Notice: 1111M doesn't mean two pairs.And you can't get the same 5-th tile to win. For example: you have 1111M and you want 1M to win, it doesn't mean the ready hand.
1 - 7Z stands for East, South, West, North, Red, Green and White
P, S, M stands for The circle, The bamboo, The character

Input

Izayoi and her friend will play many times.Her tiles' number is always 13.She will tell you her hand.
You need to tell her the number that the minimum magic she make.

Output

Output a single integer in a line corresponding to the answer of each query.

Sample Input

19S19M19P1234567Z
136M257P1234567Z

Sample Output

0
5

Hint

None

Source

None

提交代码