#K8116. Baggage Tag Groups

    ID: 35692 Type: Default 1000ms 256MiB

Baggage Tag Groups

Baggage Tag Groups

You are given a collection of baggage tags represented as strings. Two baggage tags are considered to belong to the same group if one of them can be transformed into the other by performing any number of rotations or by reversing the string and then performing any number of rotations.

More formally, let a tag be a string s. For every rotation i (0 ≤ i < |s|), consider the string s[i:] + s[:i]. Also, let rev be the reverse of s; for every rotation i (0 ≤ i < |s|), consider rev[i:] + rev[:i]. Two tags are in the same equivalence group if their minimum (in lexicographical order) among all these rotations matches.

Your task is to determine the number of distinct equivalence groups among a given list of baggage tags.

Input: The first line contains a single integer n representing the number of tags. The next n lines each contain a non-empty string representing a baggage tag.

Output: Output a single integer denoting the number of distinct groups.

Example:

Input:
5
ABC
BCA
CAB
AB
BA

Output: 2

</p>

inputFormat

The input is read from stdin and has the following format:

  1. The first line contains an integer n (1 ≤ n ≤ 105), representing the number of baggage tags.
  2. The next n lines each contain a string consisting of uppercase letters, representing a baggage tag. The length of each tag is at least 1 and may be up to 100 characters.

outputFormat

Output a single integer to stdout, representing the number of distinct baggage tag groups determined by the rotation and reversal equivalence described above.

## sample
5
ABC
BCA
CAB
AB
BA
2