#include<iostream>
#include<algorithm>
#include<set>
#include<vector>
#include<cstdio>

#define REP(i,s,n) for(int i=s;i<n;i++)
#define rep(i,n) REP(i,0,n)
#define inf (1<<29)

using namespace std;

enum FACE { TOP, BOTTOM, FRONT, BACK, LEFT, RIGHT };
template <class T>
class dice {
public:
  dice() {
    id[TOP] = 0; id[FRONT] = 1; id[LEFT] = 2;
    id[RIGHT] = 3; id[BACK] = 4; id[BOTTOM] = 5;
  }
  T& operator[] (FACE f) { return var[id[f]]; }
  const T& operator[] (FACE f) const { return var[id[f]]; }
  bool operator==(const dice<T>& b) const {
    const dice<T> &a = *this;
    return a[TOP] == b[TOP] && a[BOTTOM] == b[BOTTOM] &&
      a[FRONT] == b[FRONT] && a[BACK] == b[BACK] &&
      a[LEFT] == b[LEFT] && a[RIGHT] == b[RIGHT];
  }
  void roll_x() { roll(TOP, BACK, BOTTOM, FRONT); }
  void roll_y() { roll(TOP, LEFT, BOTTOM, RIGHT); }
  void roll_z() { roll(FRONT, RIGHT, BACK, LEFT); }
  vector<dice> all_rolls() {
    vector<dice> ret;
    for (int k = 0; k < 6; (k&1?roll_y():roll_x()),++k)
      for (int i = 0; i < 4; roll_z(), ++i)
        ret.push_back(*this);
    return ret;
  }
  bool equivalent_to(const dice& di) {
    for (int k = 0; k < 6; (k&1?roll_y():roll_x()),++k)
      for (int i = 0; i < 4; roll_z(), ++i)
        if (*this == di) return true;
    return false;
  }
private:
  void roll(FACE a, FACE b, FACE c, FACE d) {
    T tmp = id[a];
    id[a] = id[b]; id[b] = id[c];
    id[c] = id[d]; id[d] = tmp;
  }
  T var[6];
  int id[6];
};

FACE face[] = {TOP,FRONT,RIGHT,LEFT,BACK,BOTTOM};

class UnionFind
{
public:
  int par[40];

  void init(int n){ rep(i,n)par[i] = i; }

  int find(int x)
  {
    if(x == par[x])return x;
    return par[x] = find(par[x]);
  }

  void unit(int x,int y)
  {
    x = find(x), y = find(y);
    if(x != y)par[x] = y;
    find(x),find(y);
  }

};

int main()
{
  int n;
  while(cin >> n,n)
    {
      dice<char> dices[n];
      string fc;
      rep(i,n)rep(j,6)
	{
	  cin >> fc;
	  dices[i][face[j]] = fc[0];
	}
      

      UnionFind uf;
      uf.init(n);
      rep(i,n)REP(j,i+1,n)
	if(dices[i].equivalent_to(dices[j]))uf.unit(i,j);

      set<int> ans;
      rep(i,n)ans.insert(uf.find(i));

      cout << n - ans.size() << endl;

    }
  return 0;
}