The purchase-money to arise from the sale of any such land shall be applied:
First—In payment of the expenses occasioned by such sale:
Secondly—In payment of the moneys which may then be due or owing to the
mortgagee or encumbrancee: Thirdly—In payment of subsequent mortgages or
encumbrances, if any, in the order of priority; and the surplus, if any, shall
be paid to the mortgagor or encumbrancer, as the case may be.