One of the longest and nastiest presidential election campaigns in memory ends Tuesday night, with Democrat Hillary Clinton and Republican Donald Trump each looking to win a four-year, all-expenses-paid stay at the White House.

But even before the first polls close at 4 p.m. Pacific time, it’s clear what the candidates need to do and what indications to watch for through the night to see whether Trump or Clinton is on track for victory.

For Clinton, the key is her blue firewall of six swing states: Colorado, Michigan, New Hampshire, Pennsylvania, Virginia and Wisconsin. Those states, which have a recent history of voting Democratic, would combine with solidly Democratic states like California and New York to give the former secretary of state just more than the 270 electoral votes she needs to become president.

But if Trump can crack that firewall by stealing one or more of those swing states, election night becomes much messier for Clinton, who then will have to win a toss-up state like Nevada, North Carolina or Florida.

Trump, though, has a narrower path to victory. He has to not only hold all the GOP-leaning states, but also virtually sweep the close races.

“Trump has to win all the battleground states,” said Jack Citrin, who runs UC Berkeley’s Institute of Governmental Studies. “It would help if he could grab a state not thought to be close.”

But if Clinton can win traditionally Republican states like Arizona, Utah and possibly even Texas, “we’re looking at a landslide,” he said.

Viewers should watch for any early signs that either Clinton or Trump is doing worse than expected in their targeted states because, in elections, bad news tends to be contagious.

Here’s what to look for as the polls close across the United States. All times are Pacific Standard Time.

Democratic presidential nominee former Secretary of State Hillary Clinton.

Photo: Justin Sullivan, Getty Images

4 p.m.: Virginia is the first battleground state to close. It was trending Democratic even before Clinton picked Virginia Sen. Tim Kaine as her running mate. Democratic strength is in the cities and the Washington, D.C., suburbs, which tend to report their votes late.

While Indiana and Kentucky are Trump territory, they can give an early look at the evening’s trend, said David A. Caputo, a political science professor at Pace University in New York.

“People should look to see if there’s a surge in support for Trump,” he said. “In Indiana, if (Democratic Senate candidate) Evan Bayh is close or winning, that’s good news for Clinton.”

4:30 p.m.: Ohio and North Carolina are two main battleground states. While Clinton can win without either of them, a loss in either state would be a blow to Trump.

Ohio has been leaning Republican in recent polls but has gone narrowly Democratic in the past two presidential elections. The numbers can change at the end, since heavily Democratic Cuyahoga County, which includes the city of Cleveland, tends to report late.

In North Carolina, the tight battle between GOP Sen. Richard Burr and Democrat Deborah Ross could show which way the state is swinging.

5 p.m.: These states could decide who becomes president. Florida, Pennsylvania and New Hampshire are all battlegrounds. While Clinton could survive the loss of one, or even two, a sweep by Trump would be a disaster for her presidential hopes.

Trump, though, has very few paths to victory if he loses Florida and its 29 electoral votes, third-most in the nation.

Latino voters hold the key in Florida, where Clinton has tried to harness the strong feelings raised by what are seen as Trump’s stands against Latino immigrants. She needs a big win in Miami’s Dade County to offset Trump’s strength elsewhere in the state.

Political junkies also will be watching Maine, which along with Nebraska awards some of its electoral votes by congressional district. Trump has shown surprising support in the state’s more rural Second District.

“If Trump wins the Second, it’s only one (electoral) vote, but it shows he might have legs in similar areas, like parts of Michigan,” Caputo said.

Clinton is making an all-out effort to win Pennsylvania, where her strength is in late-reporting Philadelphia and its middle-class suburbs. If she runs strong or wins in Pittsburgh’s Allegheny County, her chances in the state improve. The state also has a key Senate race, with Republican Sen. Pat Toomey locked in a tight contest with Democrat Katie McGinty.

While Missouri is probably a win for Trump, Democrat Jason Kander is running a surprisingly strong race against GOP Sen. Roy Blunt.

Republican presidential nominee Donald Trump.

Photo: MANDEL NGAN / Mandel Ngan/Getty Images

6 p.m.: The Midwest and mountain states are where Trump has a chance to take the fight to Clinton. In the past week or so, he or his surrogates have spent time in Michigan, Colorado, Wisconsin and even deep-blue Minnesota, hoping to take one of these states from Clinton.

Clinton has been forced to shore up sliding support in Colorado, Michigan and even New Mexico. But she has also been focused on Arizona, a traditionally Republican state where a surge in registration and early voting by Latino voters, unhappy with Trump’s call for a wall on the Mexican border, is giving Democrats hope.

If she wins Arizona, romps in New Mexico and runs strong in Texas, it’s a sign of heavy Latino turnout that could help her across the country.

7 p.m.: Iowa and Nevada are the last two battleground states to close their polls. Iowa is a Democratic state that has been trending for Trump, while Nevada seems to be moving toward Clinton.

If Iowa goes for Clinton, Trump probably won’t be president. In Nevada, Democrats hope to run up enough of a lead in Clark County, home to Las Vegas and most of the state’s population, to offset Trump’s numbers in Reno’s Washoe County and the rural parts of the state.

If Democrats can propel Clinton to a win in Nevada, it could also carry Democrat Catherine Cortez Masto past GOP Rep. Joe Heck in the race to replace retiring Senate Minority Leader Harry Reid.

In Utah, GOP-leaning independent Evan McMullin, a native son, has been running close to Trump and Clinton.

8 p.m.: The evening could be over and the president elected by the time California and four other Western states begin counting their ballots. Idaho is the only state on that list likely to go for Trump.

10 p.m.: The last state to report will be Alaska. While it’s typically a strong Republican state, recent polls have shown Clinton running better than expected.

If the contest is close, there’s no guarantee there will be a winner by the time the last votes are counted on election night. The growing popularity of vote-by-mail means that there may be millions of votes across the country that won’t be counted Tuesday.

And there’s always the specter of the 2000 race, when George W. Bush wasn’t declared president until Dec. 12, when the U.S. Supreme Court, in a 5-4 vote, ended the Florida recount and gave the state to Bush by 537 votes.

John Wildermuth is a native San Franciscan who has worked as a reporter and editor in California for more than 40 years and has been with the San Francisco Chronicle since 1986. For most of his career, he has covered government and politics. He is a former assistant city editor and Peninsula bureau chief with The Chronicle and currently covers politics and San Francisco city government.