D-Flow: Differentiating through Flows for Controlled Generation