#include <bits/stdc++.h>
using namespace std;

int pad[3][6] = { { 3, 1, 0, 5, 4, 2 }, { 1, 5, 2, 3, 0, 4 }, { 0, 2, 4, 1, 3, 5 } };
void rotate ( int op, string &S )
{
    string temp;
    for ( int i = 0; i < 6; i++ ) {
        temp.push_back ( S[pad[op][i]] );
    }
    S = temp;
}

bool search ( string S, string T )
{
    for ( int i = 0; i < 4; i++ ) {
        for ( int j = 0; j < 4; j++ ) {
            for ( int k = 0; k < 4; k++ ) {
                if ( S == T ) {
                    return ( false );
                }
                rotate ( 2, S );
            }
            rotate ( 1, S );
        }
        rotate ( 0, S );
    }
    return ( true );
}

int main()
{
    int N;
    int rotate_pos[35];

    while ( cin >> N, N ) {
        vector < string > data(N);
        int ans = 0;
        bool flag[35];

        fill_n(flag, 35, true);
        for ( int i = 0; i < N; i++ ) {
            string S;
            for ( int j = 0; j < 6; j++ ) {
                cin >> S;
                data[i] += S[0];
            }
            for ( int j = 0; j < i; j++ ) {
                flag[i] &= search ( data[i], data[j] );
            }
        }
        for ( int i = 0; i < N; i++ ) {
            if ( !flag[i] ) {
                ans++;
            }
        }
        cout << ans << endl;
    }

    return ( 0 );
}

