Limitations on quantum measurements. I. Determination of the minimal amount of nonideality and identification of the optimal measuring apparatuses