On the generalized Ramanujan-Nagell equation I