FORT BRAGG, N.C. — Sgt. Bowe Bergdahl, who walked off his base in eastern Afghanistan in 2009, setting off a huge military manhunt and a political furor, pleaded guilty Monday to desertion and to endangering the U.S. troops sent to search for him.

The guilty pleas by Bergdahl, a 31-year-old Idaho native now stationed at an Army base in San Antonio, were not part of any deal with prosecutors.

It will now be up to an Army judge at Fort Bragg to decide the sergeant's punishment, following testimony at a hearing that is scheduled to begin next week. The desertion charge carries a potential five-year sentence, and the charge of endangering troops — formally known as misbehavior before the enemy — carries a potential life sentence.

In a military courtroom on Monday morning, Bergdahl, who was stationed at Joint Base stood before the judge, Col. Jeffery A. Nance, and described his actions as inexcusable.

"You just walked away?" Nance asked him.

"Yes, sir," Bergdahl replied. "Unfortunately, I got lost in my first 20 minutes."

Bergdahl has previously said that he had intended to walk from his unit's outpost to a larger base about 18 miles away, in order to report what he felt were leadership problems in his unit. He said he wanted to cause a major stir, to ensure that he received an audience with a high-ranking officer.

But he told Nance on Monday that he never meant to set off the huge manhunt that followed his disappearance.

"At the time, I had no intention of causing search and recovery operations," said Bergdahl, who was a private first class when he disappeared. "I didn't think they would have any reason to search for one private."

His decision to plead guilty appeared to reflect the impact of a string of victories by Army prosecutors in motions and hearings over the past year. They include rulings by Nance to allow the case to go forward despite inflammatory statements on the campaign trail by President Donald Trump, and to allow prosecutors to introduce evidence of grievous injuries suffered by service members who, the judge ruled, were searching for the sergeant at the time.

Bergdahl was captured by Taliban militants within hours of disappearing from his remote outpost, and was held captive for five years.

He endured torture, including beatings with rubber hoses and copper cables, that his military debriefer later characterized as the most profound abuse sustained by any U.S. service member since the Vietnam War. He was released in May 2014, when the Obama administration freed five detainees from Guantánamo Bay, Cuba, in exchange for him.

His case soon turned into a politically charged referendum in Washington on the Obama administration's foreign policy. After he was freed, President Barack Obama's national security adviser, Susan Rice, said Bergdahl, who was promoted while in captivity, had served with "honor and distinction," and Obama appeared with his parents at the White House.

But Republicans attacked the trade that led to his release, and some critics asserted that he had deserted to the Taliban or that some troops had died searching for him. During the presidential race last year, Trump also described the sergeant as a "dirty rotten traitor" and called for him to be executed, pantomiming a firing squad.

At a preliminary hearing, the Army's chief investigator, Maj. Gen. Kenneth Dahl, testified that he had found no evidence that any soldiers had been killed while specifically searching for Bergdahl. Neither was there evidence that the sergeant intended to desert and join the Taliban, he said.

Dahl — now a lieutenant general — also testified that he found Bergdahl to be truthful, albeit naive and delusional, and that jailing the sergeant would be "inappropriate."

Bergdahl's defense team later argued that Trump's comments made a fair trial impossible, since everyone who could play a role in deciding his fate — including prosecutors, the judge and the general who controls the case — now ultimately reports to Trump as commander-in-chief. But Nance concluded that while Trump's comments were "disturbing," there was no reason to dismiss the charges.

Though the guilty pleas entered Monday are of enormous legal significance, few significant facts were in dispute about what Bergdahl did or why he did it. Several months after he was freed, he spent a day and a half answering every question put to him by Dahl, filling a nearly 400-page transcript.

Much more will be in dispute during the sentencing phase.

Army prosecutors say that Bergdahl's departure from the outpost forced the military to drastically alter its operations and strategy during the manhunt, hurting the war effort. And they will present evidence of the serious wounds suffered by searchers, including Master Sgt. Mark Allen of the Army and Senior Chief Petty Officer Jimmy Hatch, a Navy SEAL.

Allen was shot in the head during a firefight a little more than a week after Bergdahl disappeared. He now uses a wheelchair and is unable to speak or care for himself.

During a separate mission around the same time, Hatch suffered a severe leg wound that required dozens of surgical procedures and an agonizing recovery.

Defense lawyers argue that those injuries cannot be directly tied to Bergdahl. They have cited a military investigation that found grave faults with the planning of the mission that left Allen wounded, and evidence that they say indicates that Hatch was injured on a mission that "targeted a well-known enemy combatant," and thus had a purpose apart from rescuing the sergeant.

The defense is also expected to argue that Bergdahl has already suffered profound consequences — the torture by his captors — for what he did. And they have argued that the Army also bears some responsibility for enlisting Bergdahl to begin with, after he had suffered a psychological breakdown and washed out of Coast Guard basic training in 2006 for having an "adjustment disorder with depression."