Solution

by MmcM / Sage