Set

Given multiset of cards, each with a count of figures (1 to 3) and a shape (circle, square, triangle), find the maximum number of disjoint triples where each characteristic is all-same or all-different.

Medium6CombinatoricsGreedyHash mapNo attempts yetTime limit2sMemory limit512 MB

Problem

Set is a game played with a deck in which each card shows one, two, or three figures. All figures on a card are the same, and each figure is a circle, a square, or a triangle.

A set is a group of three cards such that, for each characteristic (the number of figures and the kind of figure), either all three cards are equal or all three cards are different. For example, in the figure below, (a) is a valid set, because all cards have the same kind of figure and all of them have different numbers of figures.

In (b), both the figures and the numbers are different on every card. On the other hand, (c) is not a set, because the last two cards have the same figure, but that figure differs from the figure on the first card. Three identical cards are equal in both characteristics, so they form a set.

The goal of the game is to form as many sets as possible with the cards on the table. Each time a set is formed, its three cards are removed from play, so every card belongs to at most one set.

With few cards on the table, the maximum number of sets is easy to find. With many cards, there are many possible combinations. Your friend wants to train for the Set world championship and has asked you to write a program that computes the maximum number of sets that can be formed from a given collection of cards.

Input

The input contains several test cases. The first line of each test case contains an integer NN (3N3×1043 \le N \le 3 \times 10^4), the number of cards on the table. Each of the next NN lines describes one card.

A card is described by two words separated by a space. The first word is um, dois, or tres and gives the number of figures on the card (1, 2, or 3 respectively). The second word is circulo (or circulos), quadrado (or quadrados), or triangulo (or triangulos) and gives the kind of figure on the card (circle, square, or triangle respectively).

The end of the input is marked by a line containing a single zero.

Output

For each test case, print a single line with one integer: the maximum number of sets that can be formed with the given cards.