#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;

struct Dice {
  int face[6];
  Dice() {}
  int& operator [] (int i) { return face[i]; }
  bool operator == (const Dice& other) {
    for (int i = 0; i < 6; i++) if (face[i] != other.face[i]) return false;
    return true;
  }
  bool operator != (const Dice& other) {
    return !(*this == other);
  }
  void roll(int a, int b, int c, int d) {
    int tmp = face[a];
    face[a] = face[b];
    face[b] = face[c];
    face[c] = face[d];
    face[d] = tmp;
  }
  void roll(char c) {
    switch (c) {
      case 'N' : roll(0, 1, 5, 4); break;
      case 'S' : roll(0, 4, 5, 1); break;
      case 'E' : roll(0, 3, 5, 2); break;
      case 'W' : roll(0, 2, 5, 3); break;
      case 'R' : roll(1, 2, 4, 3); break;
      case 'L' : roll(1, 3, 4, 2); break;
    }
  }
  bool identical(Dice other) {
    for (int i = 0; i < 6; i++) {
      other.roll(i & 1 ? 'E' : 'N');
      for (int j = 0; j < 4; j++) {
        other.roll('R');
        if (*this == other) return true;
      }
    }
    return false;
  }
};

vector<string> ord = {"Red", "Yellow", "Blue", "Magenta", "Green", "Cyan"};

int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);
  cout << fixed << setprecision(10);
  
  int n;
  while (cin >> n, n) {
    vector<Dice> D(n);
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < 6; j++) {
        string s;
        cin >> s;
        D[i][j] = find(ord.begin(), ord.end(), s) - ord.begin();
      }
    }
    int ans = n;
    for (int i = 0; i < n; i++) {
      bool no = false;
      for (int j = 0; j < i; j++) {
        no |= D[i].identical(D[j]);
      }
      if (no) continue;
      ans--;
    }
    cout << ans << endl;
  }

  return 0;
}
