#include <bits/stdc++.h>
using namespace std;

struct Cube {
  string label[6];
  void swap(int a, int b, int c, int d) {
    string temp = label[a];
    label[a] = label[b];
    label[b] = label[c];
    label[c] = label[d];
    label[d] = temp;
  }
  void roll(char dir) {
    switch (dir) {
    case 'N': swap(0, 1, 5, 4); break;
    case 'E': swap(0, 3, 5, 2); break;
    case 'R': swap(1, 2, 4, 3); break;
    }
  }
  bool is_same(Cube& c) {
    for (auto& dir : "RRRRNRRRRERRRRNRRRRERRRRNRRRR") {
      if (sequence_equal(c)) return true;
      roll(dir);
    }
    return false;
  }
  bool sequence_equal(Cube& c) {
    for (int i = 0; i < 6; i++) {
      if (label[i] != c.label[i]) return false;
    }
    return true;
  }
};

int main() {
  int n;
  while (cin >> n, n) {
    vector<Cube> c(n);
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < 6; j++) {
        cin >> c[i].label[j];
      }
    }
    vector<int> m(n, 1);
    int cnt = 0;
    for (int i = 0; i < n; i++) {
      if (!m[i]) continue;
      for (int j = i + 1; j < n; j++) {
        if (m[j] && c[i].is_same(c[j])) {
          m[j] = 0;
          cnt++;
        }
      }
    }
    cout << cnt << "\n";
  }
}
