#include<iostream>
#include<vector>
using namespace std;

void roll_x(vector<int> &v) {
  vector<int> res = v;
  
  res[0] = v[4];
  res[1] = v[0];
  res[5] = v[1];
  res[4] = v[5];
  v = res;
}

void roll_y(vector<int> &v) {
  vector<int> res = v;
  res[0] = v[3];
  res[2] = v[0];
  res[5] = v[2];
  res[3] = v[5];
  v = res;
}

void roll_z(vector<int> &v) {
  vector<int> res = v;
  res[1] = v[2];
  res[2] = v[3];
  res[3] = v[4];
  res[4] = v[1];
  v = res;
}

void allpattern(vector<int> v, vector<vector<int> > &out) {
  out.clear();  
  for(int i = 0; i < 6; i++) {
    if( i & 1 ) roll_x(v);
    else roll_y(v);
    for(int j = 0; j < 4; j++) {
      roll_z(v);
      out.push_back(v);
    }
  }
  
}

bool same(vector<int> a, vector<int> b) {
  vector<vector<int> > aa, bb;
  
  allpattern(b, bb);

  for(int i = 0; i < bb.size(); i++) 
    if( bb[i] == a ) return true;
  return false;
}

bool dup(vector< vector<int> > have, vector<int> b) {
  for(int i = 0; i < (int)have.size(); i++) {
    if( same( have[i], b ) ) return true;
  }
  return false;
}

int main() {
  for(int N; cin >> N, N; ) {

    vector<int> dice[N];

    for(int n = 0; n < N; n++) {
      for(int i = 0; i < 6; i++) {
        string s; cin >> s;
        dice[n].push_back( s[0] - 'A' );
      }
    }
    
    vector< vector<int> > uni;
    for(int i = 0; i < N; i++) {
      if( ! dup(uni, dice[i]) ) {
        uni.push_back( dice[i] );
      }
    }

    cout << N - (int)uni.size() << endl;
    
  }
}