I have a problem with the $\theta$-term in the QCD-Lagrangian
$$ \mathcal{L} = \overline{q}(x)i\gamma_\mu{D^\mu} q(x)-\overline{q}(x)\mathcal{M}q-\frac{1}{4}F^a_{\mu\nu}F_a^{\mu\nu}+ \theta \frac{g^2}{32 \pi^2} F_{\mu \nu}^a \tilde{F}^{\mu \nu}_a$$
I guess the last term is gauge invariante by itselfe since it is proportional to the trace:
$$tr(F^{\mu\nu}\tilde{F}_{\mu\nu})=2\epsilon_{\mu\nu\rho\sigma}tr[(\partial^\mu A^\nu+igA^\mu A^\nu)(\partial^\rho A^\sigma +igA^\rho A^\sigma)] $$
But on the other hand the $\theta$-term is topological non-trivial and the integral
$$\int d^4x \ \ tr[F^{\mu\nu}\tilde{F}_{\mu\nu}] =: \int d^4x \ \ \partial_\mu K^\mu $$ is proportional to $n \in Z$ (the winding number) and not invariant under large gauge transformations.
Which step is wrong? And if the $\theta$-term is not gauge invariant how can we fix this problem in the Lagranian?