I know given a Lagrangian $\mathcal{L}$ satisfying the Euler-Lagrange equations, then the Lagrangian $\mathcal{L}'=\mathcal{L}+\frac{d}{dt}f(q,t)$ is also a solution of said equations. Nonetheless, I keep getting that not every function of this form can be added to the original Lagrangian. Introducing $\mathcal{L}'$ in the Euler-Lagrange equations:
$$\frac{\partial \mathcal{L}'}{\partial q}-\frac{d}{dt}\left(\frac{\partial \mathcal{L}'}{\partial \dot q}\right)=\frac{\partial \mathcal{L}}{\partial q}-\frac{d}{dt}\left(\frac{\partial \mathcal{L}}{\partial \dot q}\right)+\frac{\partial}{\partial q}\frac{d}{dt}f(q,t)-\frac{d}{dt}\frac{\partial }{\partial \dot q}\frac{d}{dt}f(q,t)$$
The first two terms on the right hand side add up to zero since we established $\mathcal{L}$ is a solution, and since we know $\mathcal L'$ is also a solution, then this equality must also be zero. Hence:
$$\frac{\partial}{\partial q}\frac{d}{dt}f(q,t)-\frac{d}{dt}\frac{\partial }{\partial \dot q}\frac{d}{dt}f(q,t)=0$$
Rearranging the operators, I end up with:
$$\left(\frac{\partial}{\partial q}-\frac{d}{dt}\frac{\partial}{\partial\dot q}\right)\frac{df}{dt}=0$$
Which is zero in the case of the Lagrangian, but I'm not sure how this is also guaranteed to be zero for any arbitrary $f(q,t)$.