At the racebook inside Green Valley Ranch casino, screaming alongside his father, Kenny Gibson leaped in the air and wondered exactly how much money he had just won.

Gibson, a Las Vegas high school physical education teacher and recreational horse player, had identified Maximum Security, the No. 7 horse, as his Kentucky Derby pick. The mucky conditions played to the horse’s strengths, Gibson believed, so he bet it to win and placed it at the top of a bounty of exactas and trifectas, exotic bets on the order of finish for the top two and three horses.

Now Gibson watched the No. 7 horse — his horse — charge down the stretch and cross the line first. He saw Country House, a 65-1 shot, cross next, followed by a blur of horses. Perfect: Country House’s long odds meant he would cash not only his bet to win but also a massive payout for the trifecta and exacta, certainly in the thousands of dollars on a small bet.

“I’m mentally going: ‘What is this going to pay? This is going to be awesome,’ ” Gibson said. “Then I heard someone go, ‘Hang on; there’s an objection.’ ”

Gibson experienced the same waves of emotion felt by scores of bettors across the country, from off-track parlors to the Churchill Downs infield, over the next 22 minutes, which were 22 of the most unusual minutes in the 145-year history of the Derby: elation, confusion, optimism, dread, hope, despair. For the thousands of bettors who picked Maximum Security and watched the horse’s victory overturned on the stewards’ disqualification, woe overtook joy at the wire.

Horse players collect bad beats and tales of near-misses, retelling their misfortune like so many fishing stories. Saturday’s surreal finish gave the general public an insight into that world. Those who bet on horse racing have seen plenty of once-winning tickets torn up by post-race rulings. But the biggest horse race in America attracts hundreds of thousands of novices, and many of them were left wondering what had just happened to their winnings.

“Obviously, I’m thinking I hit my win bet, my trifecta bet, the exacta,” Gibson said. “And instead, I won no money.”

When Churchill Downs stewards ruled Maximum Security had impeded other horses, knocked him down to 17th place and bumped Country House into first, it made a large number of people sad and a small number of people very happy.

Maximum Security went off at 9-2, one of the favorites, with more than $8.9 million bet on him to win, place (finish first or second) or show (finish first, second or third) across all wagering sources, according to Churchill Downs spokesman Darren Rogers. Country House was a 65-1 shot, with roughly $918,000 bet on him to win, place or show.

For the most part, the swap meant nothing financially for the house. Races such as the Derby operate under a pari-mutuel betting system. All legal bets, whether placed in a Vegas casino or at a track’s window, are pooled together in the same pot. Payouts are determined by how much is bet on each horse, not the odds at the time a bet is made. If you bet a horse at 10-1 during the week, those odds could change by start time based on how many other people bet on that horse.

For the Kentucky Derby, not counting the exotic bets, people bet more than $65.6 million. Off the top, 17.5 percent of that goes to vendors and prize purses. The rest is paid out to bettors, regardless of which horse wins. Based on the odds, about 12 times as many people bet on Maximum Security, the unofficial winner, as Country House, the actual winner.

Some sportsbooks offer bets outside the pari-mutuel system, taking money on “future bets” in advance of the race at fixed odds or wagers on horse-vs.-horse matchups. The Westgate SuperBook would have profited in the five figures had Maximum Security remained the winner, said Jay Kornegay, vice president of race and sports operations. When his disqualification elevated Country House, it swung to a five-figure loss for the house, which in the grand scheme of a Vegas sportsbook made small ripples.

“Rams-Saints certainly cost us a lot more money than this thing,” Kornegay said, referring to the controversial ruling near the end of this year’s NFC championship game.

But the disqualification hurt bookmakers indirectly. The perception among many novice bettors is that the house wants long shots to win, so it doesn’t have to pay out as many winners. The opposite is true. Because the house is paying out the same money regardless of the result under the pari-mutuel system, it wants as many winners as possible to create “churn,” said Johnny Avello, a longtime Vegas oddsmaker who recently joined DraftKings to run the company’s sportsbooks. People who cash a ticket are more likely to bet again. Kornegay said the SuperBook took less money than expected on the day’s final two races.

Some bookmakers worried about how such a controversial ruling at the Derby could affect future Triple Crown betting. Regular horse players could shrug off a disqualification, well acquainted with the cruelest 10 words in racing: “The result of the race is unofficial. Hold all tickets.” But casual bettors may be turned off by celebrating, only to wait nearly half an hour to find out they lost.

“The effects of doing something like that first time ever, we’ll have to see how it affects the Preakness, if there’s a lingering effect that we might see in a couple weeks,” Kornegay said.

Backers of Maximum Security should surrender any hope, even as the horse’s handlers contemplate an appeal. Kornegay said bookmakers are protected by an established standard. They do not recognize results reversed on appeal or protest more than 24 hours after an event. So those tickets with Maximum Security as the winner? They’re trash.

“People will probably hold on to it,” Avello said. “Won’t do you any good at this point.”

Gibson bets on horses regularly enough to accept his misfortune and move on, his enthusiasm for the sport undiminished. He plays for small money and for fun, relishing the chance to watch “majestic animals” with pals and a cocktail. (“I’m shooting BBs,” he said, to describe his stakes.) Besides, he heard worse stories than his — a friend needed Maximum Security to complete a Pick 5 (selecting the winner in five consecutive races), and had the original result held, he would have won $10,000.

Still, Gibson had concerns for his favored hobby. The sport was in a dark place entering the weekend, reeling from the death of more than 20 horses at Santa Anita Park in California, and he didn’t want more potential fans to be turned off.

“After everything that’s gone on this year, the last thing you need in the most popular race of the year is a fiasco,” Gibson said. “The last thing I want to do is see the sport get hurt any more. It’s already been a rough year.”

A day later, Gibson had swallowed his financial disappointment. In reviewing the race with his dad, he brought up one point that still bothered him. The bad part is, he told his dad, he still had no clue how much the trifecta would have paid. The payouts on those wagers are only calculated once the race results are official, so Gibson doesn’t know for sure what he would have won.

“It’s probably better,” he said, “that I don’t.”

Chuck Culpepper contributed to this report.

Read more from The Post: