#include <iostream>
#include <iomanip>
#include <string>
#include <sstream>
#include <algorithm>
#include <vector>
#include <utility>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <functional>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <cassert>
 
using namespace std;
 
#define REP(i,s,e) for(int i=(s); i<(int)(e); i++)
#define rep(i,n) REP(i, 0, n)
#define pb push_back
#define mp make_pair
#define all(r) (r).begin(),(r).end()
#define rall(r) (r).rbegin(),(r).rend()
#define fi first
#define se second
 
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vii;
typedef vector<ll> vl;
typedef vector<vl> vll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
 

 
const int INF = 1e9;
const double EPS = 1e-8;
const int mod = 1e9 + 7;
const ll LINF = 1e18;
const double PI = acos(-1);

struct Input
{
	int n;
	char c;
	char str[1024];
	string s;
	ll l;
	int nextInt(){
		scanf("%d", &n);
		return n;
	}
	ll nextLong(){
		scanf("%lld", &l);
		return l;
	}
	char nextChar(){
		scanf("%c", &c);
		return c;
	}
	string next(){
		scanf("%s", str);
		return string(str);
	}
	string nextLine(){
		getline(cin, s);
		return s;
	}
};
Input in;

struct Dice
{
    vi d;
    static const int top = 0, frt = 1, rit = 2, lft = 3, bck = 4, btm = 5;
    Dice(){
        rep(i, 6) d.pb(i + 1);
    }
    Dice(vi d):d(d){}
    Dice(int _d[]){
        rep(i, 6) d.pb(_d[i]);
    }
    void spin(int f1, int f2, int f3, int f4){
        int tmp = d[f1];
        d[f1] = d[f2];
        d[f2] = d[f3];
        d[f3] = d[f4];
        d[f4] = tmp;
    }
    void spinN(){
        spin(top, frt, btm, bck);
    }
    void spinS(){
        spin(top, bck, btm, frt);
    }
    void spinE(){
        spin(top, lft, btm, rit);
    }
    void spinW(){
        spin(top, rit, btm, lft);
    }
    void spinCW(){
        spin(frt, rit, bck, lft);
    }
    void spinCCW(){
        spin(frt, lft, bck, rit);
    }
 
    void spin(char c){  
        if(c == 'N') spinN();
        else if(c == 'S') spinS();
        else if(c == 'E') spinE();
        else if(c == 'W') spinW();  
    }
    void spin(string s){
        for(char c : s){
            spin(c);
        }
    }
    void spin(int t, int f){
        rep(i, 4){
            if(d[top] == t) break;
            spinN();
        }
        rep(i, 4){
            if(d[top] == t) break;
            spinE();
        }
        rep(i, 4){
            if(d[frt] == f) break;
            spinCW();
        }
    }
    void spinTop(int f){
        if(f == frt) spinN();
        else if(f == btm) rep(i, 2) spinN();
        else if(f == bck) spinS();
        else if(f == lft) spinE();
        else if(f == rit) spinW();
    }
 
    void spinSmall(){
        Dice tmp(d);
        rep(i, 6){
            if(tmp[top] < d[i]) continue;
            Dice d2(d);
            d2.spinTop(i);
            rep(j, 4){
                if(d2 < tmp) tmp.d = d2.d;
                d2.spinCW();
            }
        }
        d = tmp.d;
    }
 
    int operator[] (int n) const{
        return d[n];
    }
    bool operator == (const Dice& dice) const{
        rep(i, 6){
            if(d[i] != dice[i]) return false;
        }
        return true;
    }
    bool operator < (const Dice& dice) const{
        rep(i, 6) {
            if(d[i] != dice[i]) return d[i] < dice[i];
        }
        return false;
    }
};


int main(){
	map<string, int> mp;
	mp["Cyan"]		= 0;
	mp["Yellow"]	= 1;
	mp["Red"]		= 2;
	mp["Magenta"]	= 3;
	mp["Green"]		= 4;
	mp["Blue"]		= 5;

	while(1){
		int n = in.nextInt();
		if(n == 0) break;

		vector<Dice> d;
		int t[6];
		rep(i, n) {
			rep(j, 6) t[j] = mp[in.next()];
			d.pb(t);
			d.back().spinSmall();
		}

		sort(all(d));

		int ans = 0;
		rep(i, d.size() - 1) if(d[i] == d[i+1]) ans++;
		
		printf("%d\n", ans);
		// rep(i, n){
		// 	rep(j, 6) printf(" %d", d[i][j]);
		// 	printf("\n");
		// }
	}
}