#define _USE_MATH_DEFINES
#include <iostream>
#include <complex>
#include <algorithm>
#include <vector>
#include <stack>
#include <string>
#include <queue>
#include <cmath>
#include <math.h>
#include <numeric>
#include <list>
#include <sstream>
#include <fstream>
#include <iomanip>
#include <climits>
#include <set>
#include <memory.h>
#include <memory>
#include <cstdio>
#include <cstdlib>
#include <cctype>
#include <cassert>
#include <map>
#include <cassert>
#include <time.h>
#include <ctime>

using namespace std;

typedef complex<double> xy_t;
typedef pair<xy_t, xy_t> line;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<int , P> PP;
typedef pair<int, string> Ps;
typedef vector<int> vec;
typedef vector<vec> mat;
const int INF = 1 << 27;
const double EPS = 1e-9;
const double PI = 3.1415926535897932384626433832795;
const int CLK = CLOCKS_PER_SEC;

#define rep(i, n) for(int i = 0; i < n; i++)
#define rep2(i, m, n) for(int i = m; i < n; i++)

int dice[24][6] = {
	{0, 1, 2, 4, 3, 5}, {0, 2, 4, 3, 1, 5}, {0, 4, 3, 1, 2, 5}, {0, 3, 1, 2, 4, 5},
	{1, 5, 2, 0, 3, 4}, {1, 2, 0, 3, 5, 4}, {1, 0, 3, 5, 2, 4}, {1, 3, 5, 2, 0, 4},
	{2, 1, 5, 4, 0, 3}, {2, 5, 4, 0, 1, 3}, {2, 4, 0, 1, 5, 3}, {2, 0, 1, 5, 4, 3},
	{3, 1, 0, 4, 5, 2}, {3, 0, 4, 5, 1, 2}, {3, 4, 5, 1, 0, 2}, {3, 5, 1, 0, 4, 2},
	{4, 0, 2, 5, 3, 1}, {4, 2, 5, 3, 0, 1}, {4, 5, 3, 0, 2, 1}, {4, 3, 0, 2, 5, 1},
	{5, 1, 3, 4, 2, 0}, {5, 3, 4, 2, 1, 0}, {5, 4, 2, 1, 3, 0}, {5, 2, 1, 3, 4, 0}
};

map<vector<string>, bool> used;


int main(){
	vector<string> vs(6, "");
	vector<string> cs(6, "");
	int n, res;
	while(cin >> n && n ){
		res = 0;
		used.clear();
		rep(i, n){
			rep(j, 6) cin >> cs[j];
			rep(j, 24){
				rep(k, 6){
					vs[k] = cs[dice[j][k]];
				}
				if(used[vs]) res++;
				used[vs] = true;
			}
		}
		cout << res / 24 << endl;
	}
	return 0;
}