SAN FRANCISCO — San Francisco Supervisor London Breed emerged victorious Wednesday to become the city’s first black woman mayor after narrowly defeating a rival who was seeking to become the first openly gay man in the position.

It took eight days of counting after Election Day for Breed to build a large enough lead to claim the city’s top job. With about 250,000 ballots tallied, she led former state Sen. Mark Leno by 2,177 votes with only about 6,700 left to count.

Leno called Breed to congratulate her on the victory and later she appeared briefly before reporters and cheering supporters on the steps of City Hall. She said she was humbled, honored and looking forward to serving as mayor.

In particular, she relished the message her election sends to San Francisco’s youth, especially kids like herself who grew up poor.

“No matter where you come from, no matter what you decide to do in life, you can do anything you want to do,” she said. “Never let your circumstances determine your outcome in life.”

Breed, who will take office next month, is the second woman to be elected mayor in San Francisco history. The other was U.S. Sen. Dianne Feinstein.

Breed, 43, vowed to be mayor for all of San Francisco, a message she repeated throughout her bid to lead a city that is economically thriving but mired in homelessness, congestion and unaffordable homes. She has vowed to rid the sidewalks of homeless tent camps within a year of taking office.

Turnout exceeded 50 percent— unusually high for recent mayoral elections — in a contest that was placed on the June 5 ballot after the death of Mayor Ed Lee in December.

Breed will fill the rest of Lee’s term, which ends in early 2020, and will need to run in November 2019 for a full four-year term.

San Francisco has an unusual ranked choice voting system that allows voters to pick their top three candidates for mayor. During the complicated counting process second-place and sometimes third-place choices end up being tallied.

In an effort to block Breed from winning, Leno and Supervisor Jane Kim asked their supporters to pick the other as their No.2, saying that Breed represented the status quo that had made San Francisco so inequitable. All three are Democrats.

But Breed still prevailed, riding her support among the business and political establishment who helped her lead the field in campaign donations.

The portrayals of her as a lackey of big business bugged Breed, who first won a supervisor’s seat in 2012.

“I ask people to not attribute what I’ve done —my success and how hard I’ve worked— to not reduce that or attribute that to someone else,” Breed told the AP in a pre-election interview.

The former executive director of the African American Art & Culture Complex grew up in the historically black Western Addition area, raised by her grandmother in public housing. They drank powdered milk and ate meat from a can labeled “pork,” she said.

At City Hall, she paid homage to her late grandmother and said she probably had a hand in her win.

“She took care of the community, she took care of me even on days when I didn’t deserve it, and so being here in her honor means so much,” she said.