Consider a Lagrangian of the form
\begin{equation} L(q,\dot{q})=L_1(q,\dot{q})+\frac{d L_2(q,\dot{q})}{dt} \end{equation}
I understand that $\dot{L_2}$ does not modify the equations of motion, however, it modifies the boundary conditions needed to fulfill the variational principle $\delta L=0$.
Now consider the Hamiltonian for this Lagrangian \begin{equation} H=p\dot{q}-L \end{equation} where $\pi=\frac{\partial L}{\partial \dot{q}}$, as usual. Since both formulations share the same dynamics, I expect the Hamilton's equations \begin{eqnarray} \frac{\partial H}{\partial q}=-\dot{\pi} \\ \frac{\partial H}{\partial \pi}=\dot{q} \end{eqnarray} to give the same dynamics. However, I do not have a consistent argument to show why this boundary term will not modify the expression for the momentum. In particular, I have no reason to impose, for example, that $\frac{\partial \dot{L_2}}{\partial q}=0$.
Even further, If I consider this Lagrangian as a higher-derivative Lagrangian and I try to apply Ostrogradsky formalism, I will eventually realize that the non-degeneracy condition fails, i.e, $\frac{\partial^2 L}{\partial \dot{q}^2}= 0$.
So, anyone can instruct me how total derivative terms modify the definitions in the Hamiltonian? Thx in advance.