
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<string>
#include<queue>
#include<vector>
#include<functional>
#include<cmath>
#include<map>
#include<stack>
#include<set>
#include<numeric>
#include<limits>

#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()

using namespace std;
typedef long long ll;
typedef pair<int, int> pi;
typedef pair<ll, ll> pl;
typedef pair<ll, char> plc;

struct color {
	string c1, c2, c3, c4, c5, c6;
};

int main()
{
	int n; 
	while (cin >> n, n) {
		vector<color> v(n);
		for (int i = 0; i < n; i++) {
			cin >> v[i].c1 >> v[i].c2 >> v[i].c3 >> v[i].c4 >> v[i].c5 >> v[i].c6;
		}

		int cnt = 0;
		for (int i = 0; i < n - 1; i++) {
			map<string, string> m;
			m[v[i].c1] = v[i].c6;
			m[v[i].c6] = v[i].c1;
			m[v[i].c2] = v[i].c5;
			m[v[i].c5] = v[i].c2;
			m[v[i].c3] = v[i].c4;
			m[v[i].c4] = v[i].c3;
			for (int j = i + 1; j < n; j++) {
				if (m[v[j].c1] == v[j].c6) {
					if (m[v[j].c2] == v[j].c5) {
						if (m[v[j].c3] == v[j].c4)
							cnt++;
					}
				}
			}
		}
		cout << cnt << endl;
	}
	return 0;
}