table = [[0, 1, 2, 3, 4, 5], [3, 1, 0, 5, 4, 2], [1, 5, 2, 3, 0, 4],
         [4, 0, 2, 3, 5, 1], [2, 1, 5, 0, 4, 3], [0, 2, 4, 1, 3, 5],
         [0, 3, 1, 4, 2, 5], [5, 1, 3, 2, 4, 0], [1, 2, 0, 5, 3, 4],
         [4, 3, 0, 5, 2, 1], [3, 0, 4, 1, 5, 2], [3, 5, 1, 4, 0, 2],
         [5, 4, 2, 3, 1, 0], [2, 5, 4, 1, 0, 3], [1, 3, 5, 0, 2, 4],
         [2, 0, 1, 4, 5, 3], [4, 2, 5, 0, 3, 1], [0, 4, 3, 2, 1, 5],
         [1, 0, 3, 2, 5, 4], [4, 5, 3, 2, 0, 1], [5, 3, 4, 1, 2, 0],
         [5, 2, 1, 4, 3, 0], [2, 4, 0, 5, 1, 3], [3, 4, 5, 0, 1, 2]]
color_table = %w(Red Yellow Blue Magenta Green Cyan).map
                .with_index {|c, i| [c, i]}.to_h

until (n = gets.to_i).zero?
  dices = n.times.map { gets.split.map {|c_name| color_table[c_name]} }
  pool = []
  count = 0
  
  dices.each do |color|
    check_f = true
    table.each do |t|
      next_dice = t.map {|idx| color[idx]}
      if pool.include?(next_dice)
        count += 1 if check_f
        check_f = false
      else
        pool << next_dice
      end
    end
  end
  
  puts count
end
