There's 31,757 positions offered. However, if you are applying into a specialty, you apply simultaneously for a PGY1 and PGY2 position, so those people are being double counted.
As a result, you need to subtract 2,677 advanced positions from the 31k positions, yielding 29,080 PGY1 + PGY2. There are 18,539 US MD applicants, but with the merger of the ACGME and COCA, DO applicants must be counted, adding 3,590 to the US graduate pile. That gives 22,129 US graduates competing for 29,080 spots. Yeah we take a lot of "foreigners" but a lot of them are actually American citizens who went to school in other countries and many of whom have US medical education debt, 5,069 in fact (look on page 1, "IMGs"). If you add in the IMGs, that's 27198 US graduates and US citizens applying for 29,080 spots. Only space for about 2000 Foreign Medical Grads.
[1] - There are 5346 osteopathic graduates per year. http://www.osteopathic.org/inside-aoa/about/aoa-annual-stati...