The systems for which the algebra of gauge transformations in the lagrangian formalism is closed, are considered. The hamiltonian BRST charge and the BRST-invariant hamiltonian are found explicitly. Their expansions in powers of the ghost variables contain, in general, an infinite number of terms.