#include <iostream>
#include <string>
#include <vector>

using namespace std;

class DICE {
public:
	string top;
	string bottom;
	string left;
	string right;
	string front;
	string back;
	void TF();
	void TB();
	void TL();
	void TR();
	void TurnL();
	void TurnR();
	bool HANTEI(DICE d2);
	bool CHECK(DICE d2);
};

void DICE::TF() {
	string kari = front;
	front = top;
	top = back;
	back = bottom;
	bottom = kari;
}

void DICE::TB() {
	TF();
	TF();
	TF();
}

void DICE::TL() {
	string kari = left;
	left = top;
	top = right;
	right = bottom;
	bottom = kari;
}

void DICE::TR() {
	TL();
	TL();
	TL();
}

void DICE::TurnL() {
	string kari = left;
	left = back;
	back = right;
	right = front;
	front = kari;
}

void DICE::TurnR() {
	TurnL();
	TurnL();
	TurnL();
}

bool DICE::HANTEI(DICE d2) {
	if ((top==d2.top)&&(bottom==d2.bottom)&&(left==d2.left)&&(right==d2.right)&&(front==d2.front)&&(back==d2.back)) {
		return true;
	}
	return false;
}

bool DICE::CHECK(DICE d2) {
	DICE d3 = d2;
	for (int i = 0; i < 4; i++) {
		d3.TurnL();
		if (HANTEI(d3)) {
			return true;
		}
	}
	d3 = d2;
	d3.TF();
	d3.TF();
	for (int i = 0; i < 4; i++) {
		d3.TurnL();
		if (HANTEI(d3)) {
			return true;
		}
	}
	d3 = d2;
	d3.TF();
	for (int i = 0; i < 4; i++) {
		d3.TL();
		if (HANTEI(d3)) {
			return true;
		}
	}
	d3 = d2;
	d3.TB();
	for (int i = 0; i < 4; i++) {
		d3.TL();
		if (HANTEI(d3)) {
			return true;
		}
	}
	d3 = d2;
	d3.TL();
	for (int i = 0; i < 4; i++) {
		d3.TF();
		if (HANTEI(d3)) {
			return true;
		}
	}
	d3 = d2;
	d3.TR();
	for (int i = 0; i < 4; i++) {
		d3.TF();
		if (HANTEI(d3)) {
			return true;
		}
	}
	return false;
}

int main() {
	int n;
	while (cin >> n) {
		if (n == 0) {
			break;
		}
		vector<DICE> dd;
		for (int i = 0; i < n; i++) {
			DICE d;
			cin >> d.top >> d.front >> d.right >> d.left >> d.back >> d.bottom;
			bool h = true;
			for (int j = 0; j < dd.size(); j++) {
				if (dd[j].CHECK(d)) {
					h = false;
					break;
				}
			}
			if (h) {
				dd.push_back(d);
			}
		}
		int sz = dd.size();
		cout << n-sz << endl;
	}
	return 0;
}