# define _CRT_SECURE_NO_WARNINGS 1
# define _USE_MATH_DEFINES
# include <iostream>
# include <numeric>
# include <string>
# include <bitset>
# include <vector>
# include <algorithm>
# include <cstdlib>
# include <cstdio>
# include <cstring>
# include <cstdlib>
# include <iomanip>
# include <queue>
# include <sstream>
# include <climits>
# include <complex>
# include <cmath>
# include <list>
# include <functional>
# include <string>
# include <ctime>
# include <set>
# include <forward_list>
# include <map>
# include <stack>
using namespace std;
# define INF ((int)(1<<25))
# define REP(i,n) for(int i=0;i<(int)n;i++)
# define FOR(i,n) REP(i,n)
# define FORI(i,k,n) for(int i=k;i<(int)n;i++)
# define TORAD 2.0*M_PI/360.0
# define INT(x) int x;cin>>x;
# define ALL(x) begin(x),end(x)
# define RALL(x) (x).rbegin(),(x).rend()
# define DEBUG(x) cout<<#x<<":"<<x<<endl;
# define EPS 1e-12
#ifdef _MSC_VER
#include <intrin.h>
#define __builtin_popcount __popcnt
#endif
typedef long long lint;
typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef pair<int, pii> piii;
const int dx[4] = { 0,1,0,-1 }, dy[4] = { -1,0,1,0 };
template<class T> void debug(T a) { for (auto i : a)cout << i << endl; }

struct dice
{
	string o;
	vs a;
	dice(string _a)
	{
		o = _a;
		init();
	}
	string & operator[](int n) { return a[n]; }

	void init()
	{
		string s;
		int d[6][10] = { { -1 },{ 1,-1 },{ 0,0,0,1,-1 },{ 0,1,-1 },{ 0,0,1,-1 },{ 1,1,-1 } };
		FOR(i, 6)
		{
			s = o;
			FOR(j, 10)
			{
				if (d[i][j] == -1)break;
				if (d[i][j] == 1)sn(s);
				else se(s);
			}
			FOR(j, 4)
			{
				a.push_back(s);
				se(s);
			}
		}
	}

	void sn(string &s)
	{
		swap(s[1], s[5]);
		swap(s[2], s[1]);
		swap(s[6], s[2]);
	}
	void se(string &s)
	{
		swap(s[2], s[3]);
		swap(s[4], s[2]);
		swap(s[5], s[4]);
	}
};

int main()
{
	int n;
	while (cin >> n, n)
	{
		vector<dice> a;
		FOR(i, n)
		{
			string o=" ";
			FOR(j, 6)
			{
				string s; cin >> s;
				o += s[0];
			}
			dice d(o);
			bool ok = true;
			FOR(j, a.size())
			{
				FOR(k, a[j].a.size())
				{
					if (o == a[j][k])ok = false;
				}
			}
			if (ok)a.push_back(d);
		}
		cout << n - a.size() << endl;
	}
}