# Hamiltonian definitions in the presence of boundary term [duplicate]

Consider a Lagrangian of the form

$$L(q,\dot{q})=L_1(q,\dot{q})+\frac{d L_2(q,\dot{q})}{dt}$$

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 $$H=p\dot{q}-L$$ 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.