pupils in class B and A

I need help in the below question,Thanks

One-fifth of the pupils in class B and seven-eighth of those in class A were girls. After George reshuffled an equal number of pupils between the two classes, the number of boys in each class became the same as the number of girls. A total of 24 pupils were affected. How many pupils were there in both classes at first?

Re: pupils in class B and A

Let number in A=x and in B=y. Let m =number of girls moved A to B. That increased proportion in B from 1/5 to 1/2. So increased it by 3/10 Hence m=3/10y.
m boys moved from B to A. Before proportion of boys was 1/8. Now it is 1/2. That is an increase of 3/8 Hence m=3/8x But 2m=24 so m=12
Hence 12=3/10y so y=40. 12=3/8x so x=32. So there were 32 in A and 40 in B.