Simple equations
let the number of flags with mateo br x
that means number of flags with sam = 3/5 of x or 3x/5
now mateo gives 3 to sam to make the flags equal
that means now mateo has x-3 flags
and sam has 3x/5 + 3 flags
also these two things are equal now
so we can say
3x/5 + 3 = x-3
3x/5 - x = -3 - 3
-2x/5 = -6
we can cut the negative signs both sides now
2x/5 = 6
2x = 30 and that means x = 15
so mateo had 15 flags in starting and sam had
(3 x 15) / 5 = 9 flags