Discretization of dynamical systems with first integrals