loop do
    n = gets.to_i
    break if n == 0
    k = n
    hash = {}
    n.times do
        dice = [0] + gets.chomp.split
        class << dice
            def roll!
                self[1], self[3], self[6], self[4] = self[3], self[6], self[4], self[1]
            end
            def spin!
                self[2], self[3], self[5], self[4] = self[3], self[5], self[4], self[2]
            end
        end
        if dice[6] == 'Red'
            2.times { dice.roll! }
        elsif dice[1] != 'Red'
            dice.spin! until dice[3] == 'Red'
            dice.roll! 
        end
        c = dice[2..5].min
        dice.spin! until dice[2] == c
        k -= 1 if hash[dice] 
        hash[dice] = 1
    end
    p n - k
end