You have placed the node removal line in the wrong place.
for (count = N; count > 1; --count) {
for (i = 0; i < M - 1; ++i)
p = p->next;
p->next = p->next->next;
}
You placed this line after the loop that count M nodes from the beginning so it always start with removal of (M+1)th node. You should move it before it so that it starts from the first node.
for (count = N; count > 1; --count) {
p->next = p->next->next;
for (i = 0; i < M - 1; ++i) p = p->next;
}
Is this what You're looking for?