Influence of impurities on the stability and electronic states of titanium dioxide in the form of anatase