So when D passes the bag to A, it must contain
(27N/8 - 25 - + 1/2(27N/8 - 25) pennies. The story says that when A receives the bag he puts the 2 pennies that he had received into the bag and then walks off, and all the other beggars have nothing. Since they all started with N pennies, the total number of pennies is 5N when A received the bag it also must have contained 5N-2 pennies.
Therefore, (27N/8 - 25 - + 1/2(27N/8 - 25)=5N-2
Reducing terms, 54N/8 + 27N/8 -66 -25 = 10N - 4,
multiplying by 8, 81N - 728 = 80N - 32
combining terms, 81N - 80N = 728 - 32,
and therefore N = 696.