Yeah, the M number is the outside diameter of the thread, so it goes up as it gets thicker. Most stem bolts are M5 or M6. It’s possible to check it with a ruler but quite easy to get that wrong tbh, you learn to do it by eye over time, calipers work but most folks don’t have those! Course, you need to get the length right too. (adjustable spanners often have really dubious rulers on them but that would be accurate enough for this job, btw)
If in doubt, you could undo one of the bolts attaching the brakes to the frame (ideally, if you have an IS-mount rear brake, as that’ll avoid moving the brakes around). These are always M6 so you can compare. Or, obviously if you’ve got a spare one lying around use that!