======================================================
Explanation:
p = number of pupils
If each student gets 6 marbles, then that accounts for 6p marbles so far. Unfortunately in this situation, we're short 12 marbles. That means we really have 6p-12 marbles.
On the other hand, if each student gets 4 marbles each, then that's 4p marbles so far. Then we add on 2 since we have a those extra. In this situation we have 4p+2 marbles.
The expressions 6p-12 and 4p+2 represent the same total marble count. Set them equal to one another and solve for p.
6p-12 = 4p+2
6p-4p = 2+12
2p = 14
p = 14/2
p = 7 is the number of pupils
-------------
Optional Section:
If you want to know the number of marbles, then say either
6p-12 = 6*7-12 = 42-12 = 30
or
4p+2 = 4*7+2 = 28+2 = 30
Either way, we have 30 marbles.