logic error M>N-1 is correct