#include <cstdio>
#include <vector>
#include <algorithm>
#include <string>
#include <set>
#include <utility>

using die = std::pair<char, std::string>;

die get_die() {
  char a, b, c, d, e, f;
  scanf(" %c%*s %c%*s %c%*s %c%*s %c%*s %c%*s",
        &a, &b, &c, &d, &e, &f);

  char bot;
  std::string side;
  if (a == 'R') {
    bot = f;
    side += b;
    side += c;
    side += e;
    side += d;
  } else if (b == 'R') {
    bot = e;
    side += c;
    side += a;
    side += d;
    side += f;
  } else if (c == 'R') {
    bot = d;
    side += f;
    side += e;
    side += a;
    side += b;
  } else if (d == 'R') {
    bot = c;
    side += a;
    side += e;
    side += f;
    side += b;
  } else if (e == 'R') {
    bot = b;
    side += d;
    side += a;
    side += c;
    side += f;
  } else if (f == 'R') {
    bot = a;
    side += b;
    side += d;
    side += e;
    side += c;
  }

  std::string mside = side;
  for (int i = 1; i < 4; ++i) {
    std::rotate(side.begin(), side.begin()+1, side.end());
    mside = std::min(mside, side);
  }

  return {bot, mside};
}

int testcase_ends() {
  size_t n;
  scanf("%zu", &n);
  if (n == 0) return 1;

  std::set<die> d;
  for (size_t i = 0; i < n; ++i) {
    d.insert(get_die());
  }
  printf("%zu\n", n-d.size());
  return 0;
}

int main() {
  while (!testcase_ends()) {}
}

