#include <iostream>
#include <string>
#include <string.h>
using namespace std;

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

bool ck(string* a, string* b) {
	int k = 0;
	while (b[k] != a[0])
		k++;

	if (b[5 - k] == a[5]) {
		int l = 0;
		while (b[l] != a[1])
			l++;

		if (b[5 - l] == a[4]) {
			int u = 0;
			while (b[u] != a[2])
				u++;

			for(int i = 0; i < 24; i++)
				if (g[i][0] == k && g[i][1] == l && g[i][2] == u)
					return 1;
		}
	}

	return 0;
}

int main()
{	
	int n;

	cin >> n;

	while (n > 0) {
		string c[30][6];
		for (int i = 0; i < n; i++)
			for (int j = 0; j < 6; j++)
				cin >> c[i][j];

		bool f[30];
		memset(f, 0, sizeof(f));

		for (int i = 0; i < n - 1; i++) {
			for (int j = i + 1; j < n; j++) {
				if (ck(c[i], c[j]))
					f[j] = 1;
			}
		}

		int m = 0;
		for (int i = 0; i < n; i++)
			if (f[i])
				m++;

		cout << m << endl;

		cin >> n;
	}
	
	return 0;
}

