#include <iostream> 
#include <sstream> 
#include <iomanip> 
#include <algorithm> 
#include <cmath> 
#include <string> 
#include <vector> 
#include <list> 
#include <queue> 
#include <stack> 
#include <set> 
#include <map> 
#include <bitset> 
#include <numeric> 
#include <climits> 
#include <cfloat> 
using namespace std; 

class Dice
{
public:
    vector<char> pip; // ì¼kãºÌÚ
    Dice(){
        pip.assign(6, -1);
    }
    void roll(int d){ // ì¼kE¶Éñ]
        int dir[][4] = {{4,2,5,0},{4,3,5,1},{4,0,5,2},{4,1,5,3},{0,3,2,1},{0,1,2,3}};
        char tmp = pip[dir[d][0]];
        for(int i=0; i<3; ++i)
            pip[dir[d][i]] = pip[dir[d][i+1]];
        pip[dir[d][3]] = tmp;
    }
    bool operator==(const Dice& d) const{
        return pip == d.pip;
    }
};

bool same(Dice& dice1, Dice& dice2)
{
    for(int i=0; i<6; ++i){
        if(i < 4)
            dice1.roll(0);
        else{
            dice1.roll(1);
            if(i == 5)
                dice1.roll(1);
        }
        for(int j=0; j<4; ++j){
            dice1.roll(4);
            if(dice1 == dice2)
                return true;
        }
    }
    return false;
}

int main()
{
    for(;;){
        int n;
        cin >> n;
        if(n == 0)
            return 0;

        vector<Dice> dice(n);
        for(int i=0; i<n; ++i){
            vector<string> s(6);
            for(int j=0; j<6; ++j)
                cin >> s[j];

            dice[i].pip[4] = s[0][0];
            dice[i].pip[0] = s[1][0];
            dice[i].pip[1] = s[2][0];
            dice[i].pip[3] = s[3][0];
            dice[i].pip[2] = s[4][0];
            dice[i].pip[5] = s[5][0];
        }

        int ret = 0;
        for(int i=0; i<n; ++i){
            for(int j=i+1; j<n; ++j){
                if(same(dice[i], dice[j])){
                    ++ ret;
                    break;
                }
            }
        }
        cout << ret << endl;
    }
}