We consider the problem of trajectory optimization by a switchable system whose continuous motion is described by differential equations; and discrete state changes (switching), by recurrent inclusions. Its motion is continuously controlled by choosing the state of the discrete part of the system. The number of switchings and time of switching are not predefined. The quality of the trajectory is characterized by a functional that takes into account the costs of each switch. Together with the task of optimizing the trajectories of motion, the task of finding the minimum number of switchings at which the value of the quality functional does not exceed the given value is solved.