This is not possible using Apple's "Target Display Mode" on the iMac, from an M1 Mac mini. Only Macs introduced in 2019 or earlier (i.e., Intel-based Macs) can connect to an older iMac's Target Display Mode (newer iMacs don't have Target Display Mode), according to the Apple support article https://support.apple.com/en-us/HT204592 which states:
The iMac used as a display must have macOS High Sierra or earlier installed.
The other Mac that you're connecting it to must have been introduced in 2019 or earlier and have macOS Catalina or earlier installed.
It is possible to modify some older iMac models, converting them from a computer to a monitor, by disconnecting their main circuit boards and replacing them with 3rd-party display driver boards. The boards currently sell for $200-300. You can search eBay, AliExpress, etc. for 5K universal HD driver boards. This is a very complicated procedure, it is not just buying a cable.