After painting the red and blue pebbles, Shawn had a remainder of 40 - 9 - 13 = <<40-9-13=18>>18 pebbles.
He painted 18/3 = <<18/3=6>>6 pebbles yellow.
The difference between the number of blue and yellow pebbles is 13 - 6 = <<13-6=7>>7.
#### 7