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

typedef vector<int> V;
typedef vector<V> VV;

int N;
VV cubes;
int front[6] = {5, 4, 3, 2, 1, 0};
int sides[6][4] = {
  {1, 3, 4, 2},
  {0, 2, 5, 3},
  {0, 4, 5, 1},
  {0, 1, 5, 4},
  {0, 3, 5, 2},
  {1, 2, 4, 3}
};

int str2id(string s) {
  if (s == "Red") {
    return 0;
  } else if (s == "Yellow") {
    return 1;
  } else if (s == "Blue") {
    return 2;
  } else if (s == "Magenta") {
    return 3;
  } else if (s == "Green") {
    return 4;
  } else if (s == "Cyan") {
    return 5;
  } else {
    return -1;
  }
}

bool input() {
  cin >> N;
  if (!N) {
    return false;
  }
  cubes.resize(N);
  
  for (int i = 0; i < N; ++i) {
    cubes[i].resize(6);
    for (int j = 0; j < 6; ++j) {
      string s;
      cin >> s;
      cubes[i][j] = str2id(s);
    }
  }

  return true;
}

void align(V &v) {
  int tmp[6];
  int z = -1;
  for (int i = 0; i < 6; ++i) {
    if (v[i] == 0) {
      z = i;
    }
  }
  tmp[0] = 0;
  tmp[1] = v[front[z]];
  int x = 6;
  int y = -1;
  for (int i = 0; i < 4; ++i) {
    if (x > v[sides[z][i]]) {
      x = v[sides[z][i]];
      y = i;
    }
  }
  for (int i = 0; i < 4; ++i) {
    tmp[i + 2] = v[sides[z][(y + i) % 4]];
  }
  for (int i = 0; i < 6; ++i) {
    v[i] = tmp[i];
  }
}

int solve() {
  for (int i = 0; i < N; ++i) {
    align(cubes[i]);
  }
  sort(cubes.begin(), cubes.end());
  VV::iterator it = unique(cubes.begin(), cubes.end());
  int num_uniq = it - cubes.begin();
  return N - num_uniq;
}

int main() {
  while (input()) {
    int ans = solve();
    cout << ans << endl;
  }
  return 0;
}