Dimitri has a bag of blue, yellow, and red pencils. There is a total of 27 blue and yellow pencils, a total of 32 red and blue pencils, and a total of 31 yellow and red pencils. What is the total number of pencils he has in the bag?