Suppose you have two variations of a gene; one produces larvae that prefer the taste of a vital organ, and eat that one first. The other variation prefers the taste of a less important organ, eating that first and eating the important organ only when it has exhausted its preferred option. If the second one is on average 5% more likely to complete its growing before the roach dies and its food supply rots, the second variation is likely to displace the first.
Why would larvae eat only one organ at a time in the order of taste preference in the first place? Well if two variations of a gene exist, and one produces larvae that are not picky and damage many organs while feeding, while another produces larvae that are picky and thereby confine their damage to.one organ at a time, then if the second behavior is more successful it will begin to replace the first.
The central.message of modern Darwinism is the accumulation of small changes can lead to big evolutions. Obviously I cannot say what the exact mechanism of this instance was but what I gave are examples of the types of incremental changes that may have contributed. Your incredulity is based on a straw man argument: you ask how does the animal know what the right order is, as if that had to come all at once ex nihilo.