#K74167. Maximum Domino Sequence

    ID: 34138 Type: Default 1000ms 256MiB

Maximum Domino Sequence

Maximum Domino Sequence

You are given n domino pieces. Each domino is represented by two strings denoting its left and right halves respectively. A domino can be placed next to another if the right half of the previous domino is equal to the left half of the next domino. You may flip any domino piece, meaning a domino represented by (a, b) can be used either in the order (a, b) or (b, a). Each domino piece can be used at most once. Your task is to determine the maximum number of domino pieces that can be arranged to form a valid sequence.

Note: The sequence does not necessarily need to use all dominoes. The matching condition is defined as: if a domino with halves (x, y) is placed before a domino with halves (u, v), then y must equal u.

Example:

Input:
5
2R 3B
3B 4G
4G 5R
1R 2R
2R 3B

Output: 4

</p>

inputFormat

The first line of input contains an integer n representing the number of domino pieces. Each of the next n lines contains two strings separated by a space, representing the left and right parts of a domino piece. You can assume that the strings do not contain spaces.

outputFormat

Output a single integer — the maximum number of domino pieces that can be arranged in a valid sequence following the rules described above. The output should be printed to standard output.

## sample
5
2R 3B
3B 4G
4G 5R
1R 2R
2R 3B
4

</p>