Let X be chickens
Let Y be pigs,
then x+y=13
the animals have 2x+4y legs,
so 2x+4y=40
The system of the equations
[X+y=13
[2x+4y=40
Divide the second equation by 2
[x+y=13
[x+2y=20
subtract the first equation from the second one
y=20-13
y-7 pigs
substitute y=7 into the first equation
x+7=13
x=13-7
x=6 chickens