#include <iostream>
using namespace std;

int main() {
	char g[8],c[6]={'R','Y','B','M','G','C'};
	int n,m,i,j,k,s,h,ic[6],cd[6],d[30],a[6][4]={{2,3,5,4},{6,3,1,4},{2,6,5,1},{2,1,5,6},{1,3,6,4},{2,4,5,3}};
	while(cin >> n) {
		if (n==0) break;
		for (i=0;i<30;i++) d[i]=0; h=0;
		for (i=0;i<n;i++) {
		     for (j=0;j<6;j++) {	
                  cin >> g;
                  for (k=0;k<6;k++) if (g[0]==c[k]) break;
                  ic[j]=k; if (k==0) s=j;
	              }
	    for (m=0,j=0;j<4;j++) { cd[j]=ic[a[s][j]-1]; if (cd[j]<cd[m]) m=j; }
	    d[i]=cd[m]*1000+cd[(m+1) % 4]*100+cd[(m+2) % 4]*10+cd[(m+3) % 4];
	    for (k=1,j=0;j<i;j++) if (d[i]==d[j]) { k=0; break;}
	    h+=k;
        }
        cout << n-h << endl;
	}   			
	return 0;
}