#include <bits/stdc++.h>
#define rep(i, a, n) for(int i = a; i < n; i++)
#define repb(i, a, b) for(int i = a; i >= b; i--)
#define all(a) a.begin(), a.end()
#define int long long
using namespace std;

class dice{
public:
    string top, bottom, right, left, front, back;
    void North();
    void East();
    void South();
    void West();
    void clockRotate();
};
void dice:: North(){
    string tmp = top;
    top = front;
    front = bottom;
    bottom = back;
    back = tmp;
}
void dice:: East(){
    string tmp = top;
    top = left;
    left = bottom;
    bottom = right;
    right = tmp;
}
void dice:: South(){
    string tmp = top;
    top = back;
    back = bottom;
    bottom = front;
    front = tmp;
}
void dice:: West(){
    string tmp = top;
    top = right;
    right = bottom;
    bottom = left;
    left = tmp;
}
void dice:: clockRotate(){
    string tmp = front;
    front = right;
    right = back;
    back = left;
    left = tmp;
}

bool sameDice(dice d1, dice d2){
    string s = "NRRRRNRRRRNRRRRNRRRRERRRRERRRRERRRR";
    rep(i, 0, s.size()){
        if(d1.top == d2.top && d1.front == d2.front && d1.right == d2.right && d1.back == d2.back && d1.left == d2.left && d1.bottom == d2.bottom){
            return true;
        }
        if(s[i] == 'N') d1.North();
        else if(s[i] == 'E') d1.East();
        else if(s[i] == 'S') d1.South();
        else if(s[i] == 'W') d1.West();
        else if(s[i] == 'R') d1.clockRotate();
    }
    return false;
}

signed main(){
    int n;
    while(cin >> n, n){
        vector<dice> dices;
        dice d;
        int ans = 0;
        rep(i, 0, n){
            cin >> d.top >> d.front >> d.right >> d.left >> d.back >> d.bottom;
            dices. push_back(d);
        }
        rep(i, 0, n){
            rep(j, i + 1, n){
                if(sameDice(dices[i], dices[j])) ans++;
            }
        }
        cout << ans << endl;
    }
}