#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime>
#include <cassert>
#include <iostream>
#include <cctype>
#include <sstream>
#include <string>
#include <list>
#include <vector>
#include <queue>
#include <set>
#include <stack>
#include <map>
#include <utility>
#include <numeric>
#include <algorithm>
#include <iterator>
#include <bitset>
#include <complex>
#include <fstream>
using namespace std;
typedef long long ll;
const double EPS = 1e-9;
typedef vector<int> vint;
typedef pair<int, int> pint;
#define rep(i, n) REP(i, 0, n)
#define ALL(v) v.begin(), v.end()
#define MSG(a) cout << #a << " " << a << endl;
#define REP(i, x, n) for(int i = x; i < n; i++)
template<class T> T RoundOff(T a){ return int(a+.5-(a<0)); }
template<class T, class C> void chmax(T& a, C b){ if(a < b) a = b; }
template<class T, class C> void chmin(T& a, C b){ if(b < a) a = b; }
template<class T, class C> pair<T, C> mp(T a, C b){ return make_pair(a, b); }

const int trans[6][6] =
{
	{1, 3, 5, 2, 4, 6},
	{5, 1, 3, 4, 6, 2},
	{4, 2, 1, 6, 5, 3},
	{3, 2, 6, 1, 5, 4},
	{2, 6, 3, 4, 1, 5},
	{6, 5, 3, 4, 2, 1}	
};

struct Cube
{
	vector<string> colors;
	Cube() : colors(6) {}
	
	Cube rotate(int s)
	{
		vector<string> tmp(6);
		rep(i, 6) tmp[trans[s][i]-1] = colors[i];
		colors = tmp;
		
		return *this;
	}
};

bool same(Cube a, Cube b)
{
	rep(i, 6) if(a.colors[0] == b.colors[i]) b.rotate(i);
	
	rep(i, 4) if(a.colors == b.rotate(0).colors) return true;
	
	return false;
}

struct UnionFind {
	vector<int> data;
	UnionFind(int size) : data(size, -1) { }
	bool unite(int x, int y) {
		x = root(x); y = root(y);
		if (x != y) {
			if (data[y] < data[x]) swap(x, y);
			data[x] += data[y]; data[y] = x;
		}
		return x != y;
	}
	bool same(int x, int y) {
		return root(x) == root(y);
	}
	int root(int x) {
		return data[x] < 0 ? x : data[x] = root(data[x]);
	}
	int size(int x) {
		return -data[root(x)];
	}
	int count() {
		int res = 0;
		rep(i, data.size()) res += data[i] < 0;
		return res;
	}
};

int main()
{
    int n;
	while(cin >> n && n)
	{
		UnionFind uft(n);
		vector<Cube> cubes(n);
		
		rep(i, n) rep(j, 6) cin >> cubes[i].colors[j];
		
		rep(i, n) REP(j, i+1, n) if(same(cubes[i], cubes[j])) uft.unite(i, j);
		
		cout << n - uft.count() << endl;
	}
}