Your old i5 iMac has two Thunderbolt 2 ports (the ones with the lightning bolt icon above them). To use that for migrating your data you will need an Apple Thunderbolt 3 (USB-C) to Thunderbolt 2 adapter plus a Thunderbolt 2 cable like this one. If you do this, make sure it's a Thunderbolt 2-Thunderbolt 2 cable, not a miniDisplayPort cable (the connectors are the same).
The adapter plugs into your M4 iMac, the Thunderbolt cable goes from the adapter to your old iMac.
Thunderbolt will be the fastest connection but it is also possible to do the migration using either of the following:
- WiFi
- Ethernet cable connection directly between the two Macs
For more information see Transfer to a new Mac with Migration Assistant - Apple Support