Rangers left wing Alexis Lafrenière celebrates his goal with defenseman...

Rangers left wing Alexis Lafrenière celebrates his goal with defenseman Jacob Trouba and right wing Kaapo Kakko in the third period of an NHL game against the Blues at Madison Square Garden on Monday. Credit: Kathleen Malone-Van Dyke

Gerard Gallant had tried being patient, but his Rangers team was stuck in a funk, and at some point, he had to change something.

He’s been shaking things up more and more of late, trying all kinds of new line combinations.

And then, down a goal to the St. Louis Blues late in the second period Monday night and staring a third straight loss in the face, Gallant made a move that worked out in a big way.

The second-year Rangers coach moved his two Kid Line wingers, Alexis Lafreniere and Kaapo Kakko, up to the first line, flanking center Mika Zibanejad. That group was on the ice for the tying and winning goals, with Lafreniere scoring the deciding goal in a 6-4 victory over the Blues at Madison Square Garden that stopped a two-game losing streak.

“I thought every line played great. All the ‘D’ pairs,’’ a smiling Zibanejad said. “We stepped it up in the third. I feel like we were a little bit, I would say, desperate, and hungry, and really wanted this win. So that was a really good third period and a good comeback.’’

K’Andre Miller’s first goal of the season, at 4:28 of the third period and assisted by Lafreniere, tied the score. Then Lafreniere deflected in a shot by Zibanejad at 8:16 to make it 5-4.

Chris Kreider, who was dropped to the third line as Lafreniere and Kakko moved up, scored a shorthanded goal at 14:04 that provided some necessary insurance for a team that had been 1-4-1 in its previous six games.

“All coaches do different things within the game,’’ Kreider said when asked about the line change. “I mean, John Tortorella would have us constantly changing lines, and it’s just his style. You know, a little switch here, a little switch there, obviously trying to change up the mojo a little bit, and I mean, it works in the third, so we’ve just got to build on this good feeling.’’

Gallant said he decided to switch up the lines late in the second period because “I wasn’t liking what I was seeing.’’

“I just wanted to try and spark something again, and like we talked about, the two kids deserve some opportunities, and I thought they played really well,’’ he said. “And for me, it made [both] lines better.’’

The victory lifted the Rangers to 12-10-5 and sent them off on a road trip against the Vegas Golden Knights, the top team in the Western Conference, on Wednesday, followed by Friday’s game against the Stanley Cup champion Avalanche.

The Rangers started the game well enough, taking the lead on a goal by Braden Schneider at 17:14 of the first period.

Former Ranger Pavel Buchnevich tied the score at 1 at 18:24 with his ninth goal of the season, but a power-play goal by Adam Fox put the Rangers in front at 19:32.

Vladimir Tarasenko scored 12 seconds into the second period to tie it at 2, but Vincent Trocheck tipped in a shot by Artemi Panarin to put the Rangers in front at 1:07.

Lafreniere took a holding penalty and the Blues’ Jordan Kyrou scored on the power play to tie it at 11:04. Just 1:31 later, the Blues’ Ryan O’Reilly got to a rebound and popped it in to make it 4-3.

That’s when Gallant made the change and the Rangers started their comeback.

Igor Shesterkin made 25 saves for the Rangers and Jordan Binnington made 18 for the Blues.

Given how the past two weeks have gone and what awaits them on the upcoming road trip, Kreider was asked if this was a must-win game for the Rangers.

“Our approach right now,’’ he said, “is every game’s a must-win.’’

More Rangers