They arrived in America as children escaping political turmoil in their native Iran. They met as teenagers in Maryland, where, family and friends say, both got caught up in drugs. He sold. She bought.

Yesterday, in a Montgomery County courtroom, Shiva Dayani, 17, was sentenced to 45 years in prison in the slaying of Ali Rabonik, 18, who died of a shotgun blast to his face during a confrontation that left another teenager wounded in a Gaithersburg home. She will be almost 40 before she is eligible for parole.

Pointing out Dayani's and Rabonik's similar backgrounds, defense attorney Paul Stein told the judge, "Perhaps they were both robbed of a childhood."

Dayani, of Olney, was the last of three defendants sentenced in the fatal home invasion. Dayani pleaded guilty to 15 charges, including second-degree murder and attempted second-degree murder. Joshua Friedman, 20, was sentenced in September to 34 years, and the getaway driver, Chelsa Sommer, 19, was sentenced in October to 25 years.

Montgomery County Circuit Court Judge Paul H. Weinstein called Dayani's case "perhaps the most difficult" in his 16 years on the bench in which to impose sentence. He said he was troubled by both the "severity" of the crime and Dayani's youth.

After weighing both, Weinstein told Dayani, "I'm convinced . . . that you knew what you were doing."

Assistant State's Attorney Peter Feeney said Dayani and her two friends broke into the two-story house on Brucar Court armed with a loaded and cocked handgun, a loaded shotgun and a knife. After the defendants taped the mouths, ankles and wrists of some of the five occupants, a struggle broke out, and Dayani stabbed Jamil Numan once in the back, Feeney said.

As Dayani was about to plunge the knife into Numan's chest or stomach, Feeney said, Rabonik came to his friend's aid and struggled with Dayani before the shotgun discharged in Friedman's hands, hitting Rabonik at close range.

Stein argued that Dayani and her two friends had planned only to rob those in the house and brought the weapons "just to scare them." Someone had sold them bad cocaine the week before, Stein said, and, feeling "ripped off," they came to recover their money.

As Rabonik's friends and family dabbed their eyes nearby, Dayani said she wanted to apologize to Rabonik's friends and relatives as well as her own. She showed no emotion until her eyes turned teary when the sheriff's deputies clicked the handcuffs behind her back.

Rabonik's uncle Syd Nasadzadeh said Rabonik had just completed drug treatment and was working for a family Internet company, trying to turn his life around, when he was killed. "He was just there at the wrong time," Nasadzadeh said.