Python solution (VERY SLOW).