# [05AB1E], <s>26</s> 25 bytes

    D0*«¹v{R¬U¦X¹gn‚£`s<ì}0QP

[Try it online!]

[05AB1E]: https://github.com/Adriandmen/05AB1E
[Try it online!]: https://tio.run/nexus/05ab1e#@@9ioHVo9aGdZdVBh9aEHloWcWhnet6jhlmHFicU2xxeU2sQGPD/f7SxjrGOERgagmDs17x83eTE5IxUAA "05AB1E – TIO Nexus"

Explanation to come after further golfing.