import java.lang.Math.*
fun main(args: Array<String>?): Unit {
    while(true){
        val n = readLine()!!.trim().toInt()
        if (n == 0) break
        val dices = List(n){Dice(readLine()!!.trim().split(' ')).normalization()}.distinct()
        println(n - dices.size)
    }
}
enum class Color : Comparable<Color>{
    Red, Yellow, Blue, Magenta, Green, Cyan
}
fun String.asColor(): Color {
    return when(this){
        "Red" -> Color.Red
        "Yellow" -> Color.Yellow
        "Blue" -> Color.Blue
        "Magenta" -> Color.Magenta
        "Green" -> Color.Green
        "Cyan" -> Color.Cyan
        else -> TODO()
    }
}
data class Dice(val top: Color, val bottom: Color, val up: Color, val down: Color, val right: Color, val left: Color) : Comparable<Dice> {
    override fun compareTo(other: Dice): Int {
        return listOf(top, bottom, up, down, right, left).compareTo(listOf(other.top, other.bottom, other.up, other.down, other.right, other.left))
    }
    fun normalization(): Dice = allPerspective().min()!!
    private fun allPerspective(): List<Dice> {
        return listOf(this, rotateUp(), rotateDown(), rotateRight(), rotateLeft(), flip()).flatMap{listOf(it, it.rotate90(), it.rotate180(), it.rotate270())}
    }
    private fun rotateUp(): Dice = Dice(down, up, top, bottom, right, left)
    private fun rotateDown(): Dice = rotateUp().rotateUp().rotateUp()
    private fun rotateRight(): Dice = Dice(left, right, up, down, top, bottom)
    private fun rotateLeft(): Dice = rotateRight().rotateRight().rotateRight()
    private fun flip(): Dice = rotateRight().rotateRight()
    private fun rotate90(): Dice = Dice(top, bottom, left, right, up, down)
    private fun rotate180(): Dice = rotate90().rotate90()
    private fun rotate270(): Dice = rotate90().rotate90().rotate90()
    companion object {
        operator fun invoke(faces: List<String>): Dice {
            return Dice(faces[0].asColor(), faces[5].asColor(), faces[4].asColor(), faces[1].asColor(), faces[2].asColor(), faces[3].asColor())
        }
    }
}
fun <T: Comparable<T>> List<T>.compareTo(that: List<T>): Int {
    for ((a, b) in this.zip(that)) {
        if (a != b) return a.compareTo(b)
    }
    return this.size.compareTo(that.size)
}
