Let $B_1,B_2,B_3,B_4$ be balls and

$A_1,A_2,A_3,A_4$ be the respective boxes of respective colour.

Let ball $B_1 $ does not go in box $A_1$ which is of same colour of that of the ball.

$i.e.,B_1$ can be placed in any of the three boxes $A_1,A_2,A_3$.

If $B_1$ is placed in box $A_2$, then the other 3 balls can be placed in the following 3 ways.

$A_1$ | $B_2$ | $B_3$ | $B_4$ |

$A_3$ | $B_4$ | $B_4$ | $B_2$ |

$A_4$ | $B_3$ | $B_2$ | $B_3$ |

Similarly if$B_1$ is placed in box $A_3$ or in $A_4$ we get 3 ways in each.

$\therefore$ The required no. of ways = $3 \times 3=9$