Gauge invariance and degree of freedom count