import std.stdio;
import std.c.stdio;
import std.range;
import std.array;
import std.functional;
import std.algorithm;
import std.conv;
import std.container;
import std.math;
import std.numeric;
import std.string;
import std.c.string;
import std.regex;
import std.typecons;

class Dice {
    enum { TOP, FRONT, RIGHT, LEFT, BACK, BOTTOM }
    char v[6];
    override bool opEquals(Object o) const {
        Dice d = cast(Dice)o;
        foreach (i; 0 .. 6) {
            if (v[i] != d.v[i]) return false;
        }
        return true;
    }
    void rollX() { roll(TOP, FRONT, BOTTOM, BACK); }
    void rollY() { roll(TOP, LEFT, BOTTOM, RIGHT); }
    void rollZ() { roll(FRONT, RIGHT, BACK, LEFT); }
    void roll() (int a, int b, int c, int d) {
        char t = v[a];
        v[a] = v[b]; v[b] = v[c]; v[c] = v[d]; v[d] = t;
    }
    Dice dup() const {
        Dice ret = new Dice;
        foreach (i; 0 .. 6) {
            ret.v[i] = v[i];
        }
        return ret;
    }
    Dice[] allState() const {
        Dice[] Ret;
        Dice d = this.dup;
        foreach (i; 0 .. 6) {
            if (i % 2 == 0) d.rollX();
            else            d.rollY();
            foreach (j; 0 .. 4) {
                d.rollZ();
                Ret ~= d.dup;
            }
        }
        return Ret;
    }
    bool isSameDice(in Dice d) const {
        Dice[] states = allState;
        return states.any!((a) => a == d);
    }
    override string toString() const {
        return "Dice: " ~ v.to!string;
    }
}
 
void main() {
    int N;
    Dice[] Ds;
    bool input() {
        scanf("%d\n", &N);
        if (N == 0) return false;
        Ds = new Dice[N];
        foreach (i; 0 .. N) {
            Ds[i] = new Dice;
            auto F = readln.chomp.split(" ");
            foreach (j; 0 .. 6) {
                Ds[i].v[j] = F[j][0];
            }
        }
        return true;
    }
    void solve() {
        auto dup = new bool[N];
        int A = 0;
        foreach (i; 0 .. N) {
            if (dup[i]) continue;
            foreach (j; i + 1 .. N) {
                if (!dup[j] && Ds[i].isSameDice(Ds[j])) {
                    dup[j] = true;
                    A++;
                }
            }
        }
        writeln(dup.count!"a");
    }
    while (input) solve;
}