#include <iostream>
#include <stack>
#include <map>
#include <vector>
#include <algorithm>
#include <vector>
#include <string>

using namespace std;

typedef vector<int> vi;
typedef vector<vi> vvi;
#define loop(i,a,b) for(int i=(a); i<int(b); i++)
#define rep(i,b) loop(i,0,b)

enum { U, F, R, L, B, D };
struct dice {
    int face[6];
    dice(const dice& d){ rep(i, 6) this->face[i] = d.face[i]; }
    dice(){
        // デフォルトでは各面は 1..6 だがそうである必要はない
        face[F] = 1;
        face[R] = 2;
        face[U] = 3;
        face[B] = 6;
        face[L] = 5;
        face[D] = 4;
    }

    int find_face(int f){
        rep(i, 6) 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;
        default: throw "";
        }
    }
    int& operator[](int n){ return face[n]; }

    vector<dice> all() {
        vector<dice> res(24);
        rep(k, 6){
            rep(i, 4){ res.push_back(*this); turn(R); }
            (k & 1 ? turn(U) : turn(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;
    }

    bool operator==(const dice& d_) const {
        dice d = d_;
        vector<dice> ds = d.all();
        rep(i, ds.size()){
            bool res = true;
            rep(j, 6){
                if (ds[i][j] != this->face[j]) {
                    res = false;
                    break;
                }
            }
            if (res) return true;
        }
        return false;
    }
};

int main(){
    int n;
    while (cin >> n && n){
        vector<dice> made;
        rep(i, n){
            dice d;
            rep(j, 6){
                string s; cin >> s;
                d[j] = s.front();
            }
            made.push_back(d);
        }
        int c = 0;
        rep(i, made.size()){
            rep(j, i){
                if (made[j] == made[i]){
                    c++;
                }
            }
        }
        cout << c << endl;
    }
}