KABUL, Afghanistan (AP) — The top U.S. military officer says the Pentagon has developed plans that will allow American forces to stay in Afghanistan a bit beyond the end of the year if the contested presidential election drags on and no security agreement is signed.

Joint Chiefs Chairman Gen. Martin Dempsey, who flew into Afghanistan Monday, says that under optimum circumstances the U.S. will need about four months to pull all troops and equipment out of Afghanistan if there is no agreement allowing them to stay into 2015.

But Dempsey says the U.S. can act quickly to pull out if needed. He says the military has a plan ready should the current election stalemate continue.

All American forces will leave Afghanistan if no agreement is signed.

Both presidential contenders say they will sign it.