#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 F first
#define S second
#define INF 1 << 30

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

int n;

bool judge(vector<string>a, vector<string>b){
  int ret = 1, j;
  rep(i, 6){
    for(j = 0; j < 6; j++) if(a[i] == b[j]) break;
    if(a[5-i] != b[5-j]) ret = 0;
  }

  /*  rep(i, 6) cout << a[i] << " "; puts("");
  rep(i, 6) cout << b[i] << " "; puts("");
  cout << ret << endl;*/
  return ret;
}

int main(){
  while(scanf("%d", &n) && n){

    vector<vector<string> >c(n, vector<string>(6));
    rep(i, n) rep(j, 6) cin >> c[i][j];

    vector<int>f(n, 0);
    rep(i, n){
      if(f[i]) continue;
      REP(j, i+1, n) if(judge(c[i], c[j])) f[j] = 1;
    }
    int res = 0;
    rep(i, n) if(f[i]) res++;
    printf("%d\n", res);
  }
  return 0;
}