#include<iostream>
#include<string>
#include<vector>

#define REP(i, b, n) for(int i = b; i<(int)n; i++)
#define rep(i, n) REP(i, 0, n)

using namespace std;

enum{TOP=0,FRONT,RIGHT,LEFT,BACK,BOTTOM}; 
#define rotate_swap(x,a,b,c,d) swap(x.m[a],x.m[b]);swap(x.m[b],x.m[c]);swap(x.m[c],x.m[d]);
  
const int dy[] = {-1, 0, 1, 0};
const int dx[] = {0, 1, 0, -1};
 
class Dice{
public:
  int y, x;
  string m[6];
  Dice(){
    y = x = 0;
  }
  bool operator<(const Dice &a)const{
    rep(i,6)if (m[i] != a.m[i])return m[i] < a.m[i];
    return false;
  }
  bool operator==(const Dice &a)const{
    rep(i,6)if (m[i] != a.m[i])return false;
    return true;
  }
};
 
void rotate_r(Dice &x){
  x.x++;
  rotate_swap(x,TOP,LEFT,BOTTOM,RIGHT);
}
  
void rotate_l(Dice &x){
  x.x--;
  rotate_swap(x,TOP,RIGHT,BOTTOM,LEFT);
}
  
void rotate_f(Dice &x){
  x.y--;
  rotate_swap(x,TOP,BACK,BOTTOM,FRONT);
}
  
void rotate_b(Dice &x){
  x.y++;
  rotate_swap(x,TOP,FRONT,BOTTOM,BACK);
}
  
void rotate_cw(Dice &x){
  rotate_swap(x,BACK,LEFT,FRONT,RIGHT);
}
  
void rotate_ccw(Dice &x){
  rotate_swap(x,BACK,RIGHT,FRONT,LEFT);
}
  
vector<Dice> generate_all(Dice x){
  vector<Dice> res;
  rep(i,6){
    rep(j,4){
      res.push_back(x);
      rotate_cw(x);
    }
    if (i%2 == 0)rotate_r(x);
    else rotate_f(x);
  }
  return res;
}

int main() {
  while (true) {
    int n;
    cin >> n; 
    if (n == 0) break;
    vector<vector<Dice> > v;
    rep (i, n) {
      Dice dice;
      cin >> dice.m[TOP];
      cin >> dice.m[FRONT];
      cin >> dice.m[RIGHT];
      cin >> dice.m[LEFT];
      cin >> dice.m[BACK];
      cin >> dice.m[BOTTOM];
      v.push_back(generate_all(dice));
    }
    int res = 0;
    rep (i, n) {
      rep (j, i) rep (ii, 24) rep (jj, 24) {
	if (v[i][ii] == v[j][jj]) {
	  ++res;
	  goto next;
	}
      }
    next:;
    }
    cout << res << endl;
  }
  return 0;
}