<t>Here is the algorithm, which doesn't use much memory. Suppose, you've got numbers 1...m*n. Mark every n number, and send marking numbers to the end of the queue. You should do it in the way that doesn't change the order of marked and unmarked elements, i.e on first step you'll get 1...n-1, n+1,.....