This is only a very partial answer, looking at the simplest nontrivial case, $D=2$.
Presumably the quantity of interest can be described as the average expenditure per round, where the process is allowed to continue indefinitely (which I take to be the essential meaning of "quadrillion"). With this in mind, we may as well assume that $p+q=1$. Allowing the possibility of randomly remaining stationary only means you're slowing the average expenditure per round down by a factor of $p+q$, whatever stategy is employed.
In general, since the option to remain stationary isn't decided until after the next putative step has been randomly chosen, there is never any need to do so unless you are on the $M$-dollar "precipice" at one of the two ends, in this case at $+1$ or $-1$. At any other point you may as well take the step, incurring no cost, in hopes of reversing it on the next round.
Let's denote by $N$ the average expenditure per step if you Never pay to remain stationary. Under this strategy, you always wind up back at the origin every other round, so it's only on the even numbered rounds, when you're at $\pm1$, that you risk having to pay $M$. The average expenditure is
$$N={p^2+q^2\over2}M$$
Next, let's denote by $B$ the average expenditure per round if you pay to remain stationary at Both $+1$ and $-1$ (if the alternative would take you to $+2$ or $-2$). This gives a Markov process on the states $\{-1,0,1$} with transition matrix
$$\pmatrix{q&q&0\\p&0&q\\0&p&p}$$
and dominant eigenstate
$${1\over1-pq}\pmatrix{q^2\\pq\\p^2}$$
(Note: $q^2+pq+p^2=1-pq$.) It follows that the average expenditure per round (in the limit) is
$$B={q^3+p^3\over1-pq}m$$
For example, if $p=q=1/2$, then
$$N={1\over4}M\quad\text{and}\quad B={1\over3}m$$
so the crossover occurs at $m={3\over4}M$. (Note, if $pq=0$, then $N=M/2$ and $B=m$, with a crossover at $m=M/2=M/D$, as the OP observed.)
There are, of course, two more stategies: pay to remain stationary at $-1$ but not $+1$, and vice versa. Let's denote by $L$ and $R$ the average expenditure per round for these two strategies. If I've done the algebra correctly, the results are
$$L={p^3\over1+p^2}M+{q^2\over1+p^2}m$$
and
$$R={q^3\over1+q^2}M+{p^2\over1+q^2}m$$
For $p=q=1/2$ we get
$$L=R={1\over10}M+{1\over5}m$$
It's not hard to check that, for $p=q=1/2$, we have $600(N-L)(L-B)=(3M-4m)^2$, which means that the $L$ (and $R$) strategy always lies between the $N$ and $B$ strategies, with crossover at $m={3\over4}M$, so for the classic, symmetric drunkard's walk, one should be willing to pay to remain stationary either Never or a Both ends (which makes sense, by symmetry). For general $p+q=1$, however, there can be cases when $L$ or $R$ is optimal.
It might be worth looking carefully at the case $D=3$, but hopefully this gives some indication of how things will go.