#include <iostream>
#include <string>
#include <vector>
using namespace std;

int rot[24][6] = {
    {0,1,2,3,4,5}, {0,2,4,1,3,5}, {0,4,3,2,1,5}, {0,3,1,4,2,5},
    {1,5,2,3,0,4}, {1,2,0,5,3,4}, {1,0,3,2,5,4}, {1,3,5,0,2,4},
    {2,5,4,1,0,3}, {2,4,0,5,1,3}, {2,0,1,4,5,3}, {2,1,5,0,4,3},
    {3,4,5,0,1,2}, {3,5,1,4,0,2}, {3,1,0,5,4,2}, {3,0,4,1,5,2},
    {4,0,2,3,5,1}, {4,2,5,0,3,1}, {4,5,3,2,0,1}, {4,3,0,5,2,1},
    {5,4,2,3,1,0}, {5,2,1,4,3,0}, {5,1,3,2,4,0}, {5,3,4,1,2,0}
};

int main() {
    int n;
    while (cin >> n, n) {
        vector<vector<string> > work;
        for (int i=0; i<n; ++i) {
            vector<string> v(6);
            for (int j=0; j<6; ++j) {
                cin >> v[j];
            }
            work.push_back(v);
        }
        int res = 0;
        for (int i=0; i<n; ++i) {
            bool ok = true;
            for (int j=i+1; j<n && ok; ++j) {
                for (int k=0; k<24 && ok; ++k) {
                    if (work[i][0] == work[j][rot[k][0]] &&
                        work[i][1] == work[j][rot[k][1]] &&
                        work[i][2] == work[j][rot[k][2]] &&
                        work[i][3] == work[j][rot[k][3]] &&
                        work[i][4] == work[j][rot[k][4]] &&
                        work[i][5] == work[j][rot[k][5]]) {
                        ok = false;
                    }
                }
            }
            if ( ! ok) ++res;
        }
        cout << res << endl;
    }
    return 0;
}