#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;

struct cube {

    vector<string> c;

    cube(): c(6) {}

    static bool is_same(cube a, cube b) {

        {
            bool r = true;
            for (int l = 0; l < 6; l++) {
                if (a.c[l] != b.c[l]) {
                    r = false;
                    break;
                }
            }
            if (r) return true;
        }
        
        for (int i = 0; i < 4; i++) {
            for (int j = 0; j < 4; j++) {
                for (int k = 0; k < 4; k++) {
                    string temp = b.c[3];
                    b.c[3] = b.c[5];
                    b.c[5] = b.c[2];
                    b.c[2] = b.c[0];
                    b.c[0] = temp;
                    bool r = true;
                    for (int l = 0; l < 6; l++) {
                        if (a.c[l] != b.c[l]) {
                            r = false;
                            break;
                        }
                    }
                    if (r) return true;
                }
                string temp = b.c[4];
                b.c[4] = b.c[5];
                b.c[5] = b.c[1];
                b.c[1] = b.c[0];
                b.c[0] = temp;
            }
            string temp = b.c[3];
            b.c[3] = b.c[4];
            b.c[4] = b.c[2];
            b.c[2] = b.c[1];
            b.c[1] = temp;
        }

        return false;

    }


};

int main() {

    ll n;
    while (cin >> n, n) {

        vector<cube> cb(n);
        for (ll i = 0; i < n; i++) {
            for (ll j = 0; j < 6; j++) {
                cin >> cb[i].c[j];
            }
        }

        ll ans = 0;
        for (ll i = 0; i < n; i++) {
            bool is_kaburi = false;
            for (ll j = 0; j < i; j++) {
                if (cube::is_same(cb[i], cb[j])) {
                    is_kaburi = true;
                    break;
                }
            }
            if (is_kaburi) {
                ans++;
            }
        }

        cout << ans << endl;

    }

}
