#include <iostream>
#include <complex>
#include <sstream>
#include <string>
#include <algorithm>
#include <deque>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <vector>
#include <set>
#include <limits>
#include <cstdio>
#include <cctype>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <ctime>
using namespace std;
#define REP(i, j) for(int i = 0; i < (int)(j); ++i)
#define FOR(i, j, k) for(int i = (int)(j); i < (int)(k); ++i)
#define SORT(v) sort((v).begin(), (v).end())
#define REVERSE(v) reverse((v).begin(), (v).end())
typedef complex<double> P;
const int L = 6;

int H[6][6] = {
    {1,5,2,3,0,4}, // 0 : North : ?\\????§????   ( y:-1 )
    {3,1,0,5,4,2}, // 1 : East  : ???????§????   ( x:+1 )
    {2,1,5,0,4,3}, // 2 : West  : ???????§????   ( x:-1 )
    {4,0,2,3,5,1}, // 3 : South : ??????????§???? ( y:+1 )
    {0,2,4,1,3,5}, // 4 : Right : ?????????????????¢ (?§??????????)
    {0,3,1,4,2,5}  // 5 : Left  : ?????????????????¢ (?§??????????)
};
 
// d[0] := top,   d[1] := front 
// d[2] := right, d[3] := left
// d[4] := back,  d[5] := bottom
enum{TOP, FRONT, RIGHT, LEFT, BACK, BOTTOM};
struct Cube{
    vector<string> d;
    Cube(vector<string> v) { d = v; }
    Cube(){ d = vector<string>(6); }
    void roll(int dir){
        vector<string> nd(6);
        for(int i = 0 ; i < 6 ; i++ ) nd[i] = d[ H[dir][i] ];
        d = nd;
    }
};

bool preCompareCube(Cube &a, Cube b){
  if(a.d == b.d) return true;
  b.roll(4);
  if(a.d == b.d) return true;
  b.roll(4);
  if(a.d == b.d) return true;
  b.roll(4);
  if(a.d == b.d) return true;
  b.roll(4);
  return false;
}

bool compareCube(Cube &a, Cube b){
  if(preCompareCube(a, b)) return true;
  b.roll(0);
  if(preCompareCube(a, b)) return true;
  b.roll(0);
  if(preCompareCube(a, b)) return true;
  b.roll(0);
  if(preCompareCube(a, b)) return true;
  b.roll(0);

  b.roll(1);
  if(preCompareCube(a, b)) return true;
  b.roll(1);
  if(preCompareCube(a, b)) return true;
  b.roll(1);
  if(preCompareCube(a, b)) return true;
  return false;
}

int main() {
  int N;
  while(cin >>N && N){
    Cube c; REP(i, L) cin >>c.d[i];
    vector<Cube> res;
    res.push_back(c);
    FOR(n, 1, N){
      bool f = true;
      REP(i, L) cin >>c.d[i];
      REP(i, res.size()) if(compareCube(res[i], c)) f = false;
      if(f) res.push_back(c);
    }
    cout <<N - res.size() <<endl;
  }
  return 0;
}