class Dice
    @faces = nil
 
    def initialize arr
        @faces = arr
    end

    def rotate news
        f = @faces.dup
        case news
        when "N"
            @faces = [f[1], f[5], f[2], f[3], f[0], f[4]]
        when "E"
            @faces = [f[3], f[1], f[0], f[5], f[4], f[2]]
        when "W"
            @faces = [f[2], f[1], f[5], f[0], f[4], f[3]]
        when "S"
            @faces = [f[4], f[0], f[2], f[3], f[5], f[1]]
        end
    end
 
    def right top, front
        "SWSWS".each_char{|c|
            break if @faces[0] == front
            rotate c
        }
        rotate "S"
        "WWW".each_char{|c|
            break if @faces[0] == top
            rotate c
        }
        @faces[2]
    end
 
    def same? faces2
        return true if @faces == faces2
        "WWWWSWWWWSWWWWSWWWWSWSWWWWSSWWW".each_char{|c|
            rotate c
            return true if @faces == faces2
        }
        return false
    end
end
 
while true
    n = gets.to_i
    break if n == 0
    arr = [gets.chomp.split]
    (n-1).times{
        line = gets.chomp.split
        dice = Dice.new(line)
        same = false
        arr.each{|line2|
            if dice.same?(line2)
                same = true
                break
            end
        }
        arr << line unless same
    }
    puts n - arr.length
end
