#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <algorithm>

using namespace std;

#define rep(i,j) REP((i), 0, (j))
#define REP(i,j,k) for(int i=(j);(i)<(k);++i)
#define BW(a,x,b) ((a)<=(x)&&(x)<=(b))
#define PB(a) push_back(a)
#define F first
#define S second
#define INF 1 << 30

typedef pair<int, int> pi;
typedef pair<string, string> P;
typedef pair<int, pi> pii;
typedef vector<int> vi;
typedef queue<int> qi;
typedef long long ll;

int N;

int c(P a, P b){
  if(a.F == b.F && a.S == b.S) return 1;
  if(a.F == b.S && a.S == b.F) return 1;
  return 0;
}

int judge(vector<string> A, vector<string> B){
  P a1, a2, a3, b1, b2, b3;
  a1 = P(A[0], A[5]); a2 = P(A[1], A[4]); a3 = P(A[2], A[3]);
  b1 = P(B[0], B[5]); b2 = P(B[1], B[4]); b3 = P(B[2], B[3]);
  if(c(a1, b1) && c(a2, b2) && c(a3, b3)) return 1;
  if(c(a1, b1) && c(a2, b3) && c(a3, b2)) return 1;
  if(c(a1, b2) && c(a2, b1) && c(a3, b3)) return 1;
  if(c(a1, b2) && c(a2, b3) && c(a3, b1)) return 1;
  if(c(a1, b3) && c(a2, b1) && c(a3, b2)) return 1;
  if(c(a1, b3) && c(a2, b2) && c(a3, b1)) return 1;
  return 0;
}

int main(){
  while(scanf("%d", &N) && N){
    vector<vector<string> >D(N);
    rep(i, N){
      rep(j, 6){
	string s;
	cin >> s;
	D[i].PB(s);
      }
    }

    rep(i, D.size()){
      rep(j, D.size()){
	if(i == j) continue;
	if(judge(D[i], D[j])){
	  D.erase(D.begin()+j);
	  j--;
	}
      }
    }
    //    printf("%d\n", (int)D.size());
    printf("%d\n", N - (int)D.size());
  }
  return 0;
}