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?