#include <algorithm>
#include <iostream>
#include <vector>
#include <set>
using namespace std;

enum { U, B, L, F, R, D };
struct Dice {               //   .    .  U(2)   .
    int face[6];            // B(5) L(4) F(0) R(1)
    Dice() {                //   .    .  D(3)   .
        face[F] = 0; face[R] = 1; face[U] = 2;
        face[B] = 5; face[L] = 4; face[D] = 3;
    }
    int find_face(int f) {
        for (int i = 0; i < 6; i++) if (face[i] == f) return i;
        return -1;
    }
    void turn(int dir) {
        switch (dir) {
            case R: rotate(U, R, D, L); break;
            case B: rotate(U, B, D, F); break;
            case L: rotate(U, L, D, R); break;
            case F: rotate(U, F, D, B); break;
            case U: rotate(F, R, B, L); break;  // 上から見て反時計回り
            case D: rotate(F, L, B, R); break;  // 下から見て反時計回り
        }
    }
    int& operator[](int n) { return face[n]; }
    const int& operator[](int n) const { return face[n]; }
    vector<Dice> all_rolls() {              // 24 通り
        vector<Dice> res;
        for (int k = 0; k < 6; k++) {
            for (int i = 0; i < 4; i++) {
                res.push_back(*this);
                turn(R);
            }
            turn(k % 2 == 1 ? U : F);
        }
        return res;
    }
    void rotate(int a, int b, int c, int d) {
        int t = face[d]; face[d] = face[c]; face[c] = face[b];
        face[b] = face[a]; face[a] = t;
    }
    friend ostream& operator<<(ostream& os, const Dice d) {
        os << "[";                          // U, B, L, F, R, D
        for (int i = 0; i < 6; i++) os << d[i] << " ]"[i == 5];
        return os;
    }
};

int main() {
    Dice dice;
    vector<Dice> all_rolls = dice.all_rolls();
    int n;
    while (cin >> n, n) {
        int cnt = 0;
        set<string> s;
        while (n--) {
            string cur, t;
            for (int i = 0; i < 6; i++) cin >> t, cur += t[0];
            swap(cur[3], cur[4]);
            if (s.count(cur)) {
                cnt++;
            } else {
                swap(cur[3], cur[4]);
                for (Dice &dice: all_rolls) {
                    string t;
                    for (int i = 0; i < 6; i++) t += cur[dice[i]];
                    s.emplace(t);
                }
            }
        }
        cout << cnt << endl;
    }
    return 0;
}
