Cards

Interview

Time limit1sMemory limit128 MB

Summary
Simulate a card-sorting game to find which numbers remain consistent with a sequence of column answers after repeated row/column reshuffling.
Level

Medium5 of 10

Topics
Simulation, Math
Solved
No attempts yet

Problem

Dave and Hal play a game with cards. Dave has NN cards, where NN is a multiple of three (N=3KN = 3K), and the cards are numbered from 11 to NN.

The same number is printed on both sides of a card, no two cards share a number, and the cards start sorted in increasing order.

First, Hal secretly picks one number from the set {1,2,…,N}\{1, 2, \ldots, N\}.

Then Dave deals all the cards face up into a grid of KK rows and 33 columns. He fills the grid row by row: card 11, then card 22, then card 33 go into the first row from left to right, the next three cards fill the second row, and so on until the last card completes the last row.

Hal then tells Dave which column (the first, second, or third) currently holds the card with his secret number.

Dave gathers the cards column by column: he collects the entire first column from top to bottom (row 11, row 22, ..., row KK), then the second column the same way, and finally the third column. Without shuffling, he deals this pile back onto the table exactly as before, row by row.

This repeats. Every time Dave finishes dealing, Hal again names the column that contains his card. After all of Hal's answers, several numbers may still be consistent with everything he said.

Write a program that uses Hal's answers to determine the smallest set of numbers that are still candidates for Hal's secret number.

Input

The first line contains an integer NN (3≤N≤9993 \le N \le 999, and NN is divisible by 33), the number of cards.

The second line contains an integer DD (1≤D≤101 \le D \le 10), the number of deals, that is, the number of Hal's answers.

Each of the next DD lines contains one of the words first, second, or third - Hal's answer for that deal, given in order.

Output

Print, on a single line, every number that is still a candidate for Hal's secret number, in increasing order, separated by single spaces.

Examples3

  1. Example 1

    Input
    6
    1
    second
    
    Expected output
    2 5
    
  2. Example 2

    Input
    12
    2
    third
    first
    
    Expected output
    6
    
  3. Example 3

    Input
    18
    2
    first
    third
    
    Expected output
    7 16