I am trying to work out an excel formula but dont seem to be able to get it quite right.

My spreadsheet looks like this-
Column A Column B Column C Column D
sales ledger Sales ledger Purchase formula
90days old Ledger result

Value1 Value2 Value3 result
5000 (cellA5) 1000 (cellB5) 10000 (cellC5) 4000 (cellC6)
4000 500 4000 3500
3000 0 2000 2000

Column D contains what the formula result would be in the scenarios shown.
The result/answer is always the lowest amount after making the following comparison-

To arrive at the answer the formula needs to calulate Value1 cellA5 minus Value2 cellB5
The result/balance then needs to be compared to Value3 CellC5 and whichever is the lower amount is returned as the answer in Column D

Thanks in advance for your help