#include <iostream>
#include <string>
#include <map>
#include <set>
#include <algorithm>
using namespace std;

string d;

void adjust() {
    if (d[1] == '0') {
        swap(d[4], d[0]);
        swap(d[0], d[1]);
        swap(d[1], d[5]);
    } else if (d[2] == '0') {
        swap(d[3], d[0]);
        swap(d[0], d[2]);
        swap(d[2], d[5]);
    } else if (d[3] == '0') {
        swap(d[5], d[2]);
        swap(d[2], d[0]);
        swap(d[0], d[3]);
    } else if (d[4] == '0') {
        swap(d[1], d[0]);
        swap(d[0], d[4]);
        swap(d[4], d[5]);
    } else if (d[5] == '0') {
        swap(d[0], d[5]);
        swap(d[2], d[4]);
    }
    int min_idx = 1;
    for (int i = 2; i <= 4; i++) {
        if (d[i] < d[min_idx]) {
            min_idx = i;
        }
    }
    switch (min_idx) {
    case 2:
        swap(d[1], d[2]);
        swap(d[2], d[4]);
        swap(d[4], d[3]);
        break;
    case 3:
        swap(d[1], d[3]);
        swap(d[3], d[4]);
        swap(d[4], d[2]);
        break;
    case 4:
        swap(d[1], d[4]);
        swap(d[2], d[3]);
        break;
    }
}

int main() {
    map<string, char> cs;
    cs["Red"]     = '0';
    cs["Yellow"]  = '1';
    cs["Blue"]    = '2';
    cs["Magenta"] = '3';
    cs["Green"]   = '4';
    cs["Cyan"]    = '5';
    int n;
    while (cin >> n, n) {
        set<string> s;
        for (int i = 0; i < n; i++) {
            d.clear();
            for (int j = 0; j < 6; j++) {
                string c;
                cin >> c;
                d += cs[c];
            }
            adjust();
            s.insert(d);
        }
        cout << n - s.size() << endl;
    }
}