Group size. The group size must divide 126 exactly and 168 exactly, so it is a common factor of 126 and 168. Fewest groups means the biggest groups, so the group size is the HCF.
126=2×3×3×7
168=2×2×2×3×7
The primes common to both are 2, 3 and 7. Take each the minimum number of times it appears: 2 once (126 has only one 2), 3 once (168 has only one 3) and 7 once.
HCF=2×3×7=42
Each group has 42 students.
Number of groups.
Class 6: 126÷42=3 groups.
Class 7: 168÷42=4 groups.
Total number of groups =3+4=7.
So there are 7 groups of 42 students each.