#include <iostream>
#include <vector>

using namespace std;

enum Color {
    Red, Yellow, Blue,
    Magenta, Green, Cyan
};

struct Dice {
    Color dice[6];
};

bool Rotate(const Dice &a, Dice &b, int d)
{
    if (d == 1) {
        swap(b.dice[0], b.dice[4]);
        swap(b.dice[0], b.dice[1]);
    }
    else if (d == 2) {
        swap(b.dice[0], b.dice[3]);
        swap(b.dice[0], b.dice[2]);
        swap(b.dice[2], b.dice[5]);
    }
    else if (d == 3) {
        swap(b.dice[0], b.dice[2]);
        swap(b.dice[0], b.dice[5]);
        swap(b.dice[0], b.dice[3]);
    }
    else if (d == 4) {
        swap(b.dice[0], b.dice[1]);
        swap(b.dice[0], b.dice[4]);
    }
    else if (d == 5) {
        swap(b.dice[0], b.dice[5]);
        swap(b.dice[2], b.dice[3]);
    }

    if (a.dice[5] != b.dice[5])
        return false;

    for (int i = 0; i < 3; ++i) {
        bool flag = true;
        for (int j = 0; j < 6; ++j)
            if (a.dice[j] != b.dice[j]) {
                flag = false;
                break;
            }
        if (flag)
            return true;

        for (int j = 1; j <= 3; ++j)
            swap(b.dice[j], b.dice[j + 1]);
    }

    return false;
}

bool isSame(const Dice &a, Dice &b)
{
    for (int i = 0; i < 6; ++i)
        if (a.dice[0] == b.dice[i]) {
            if (Rotate(a, b, i))
                return true;
        }

    return false;
}

int main()
{
    int n;

    while (cin >> n, n) {
        Dice obj[n];
        string inp;

        for (int i = 0; i < n; ++i)
            for (int j = 0; j < 6; ++j) {
                cin >> inp;

                if (inp == "Red")
                    obj[i].dice[j] = Red;
                else if (inp == "Yellow")
                    obj[i].dice[j] = Yellow;
                else if (inp == "Blue")
                    obj[i].dice[j] = Blue;
                else if (inp == "Magenta")
                    obj[i].dice[j] = Magenta;
                else if (inp == "Green")
                    obj[i].dice[j] = Green;
                else if (inp == "Cyan")
                    obj[i].dice[j] = Cyan;
            }

        vector<bool> used(n, false);
        int same = 0;
        for (int i = 0; i < n; ++i) {
            if (!used[i])
                for (int j = i + 1; j < n; ++j) {
                    if (isSame(obj[i], obj[j])) {
                        used[j] = true;
                        ++same;
                    }
                }
        }

        cout << same << endl;
    }

    return 0;
}