class Solution {
public:
    int LastRemaining_Solution(int n, int m)
    {
        if (m <=0 || n <= 0) return -1;
        queue<int> que;
        for (int i = 0; i < n; ++ i) que.push(i);
        int k = 0;
        while(que.size() > 1) {
            k = (k+1)%m;
            if (k != 0) {
                que.push(que.front());
                que.pop();
            } else {
                que.pop();
            }
        }
        return que.front();
    }
};