Cuban migrants
Children play at a shelter for Cuban migrants in the border between Panama and Costa Rica in Paso Canoas, Panama, Dec. 23, 2015. REUTERS/Juan Carlos Ulate

Central American countries reached a deal Monday to allow thousands of Cuban migrants stranded in Costa Rica to continue their journey toward the United States next month. The agreement was reportedly put together after multilateral talks in Guatemala City.

An unspecified number of migrants will be airlifted from Costa Rica to El Salvador, where they will continue by bus toward Mexico, Costa Rica’s Foreign Ministry reportedly said, in a statement. The program is due to begin during the first week of January, authorities said.

"The solution emerging is an absolute exception and only for those people who entered national territory legally," Costa Rica's Foreign Minister Manuel Gonzalez said, according to Reuters.

American legislation provides Cuban migrants preferential treatment under which they are allowed to enter the country and apply for residency if they arrive at the U.S. border by land. Those trying to enter the country through sea are sent back under the special immigration policy known as "wet foot, dry foot.”

The present crisis began in mid-November when Nicaragua, a close ally of Cuba, denied access to thousands of migrants arriving from Costa Rica. Currently, an estimated 8,000 Cubans are stranded inside Costa Rica's northern border with Nicaragua.

In early December, Kathya Rodriguez, Costa Rica’s immigration director, said that nearly 5,000 temporary visas were granted to migrants making the trek to the U.S. by land and that it has received requests for another 1,000, the Pan Am Post reported. Costa Rica has reportedly spent more than $222,000 for sheltering the stranded migrants.

Meanwhile, Costa Rican officials have begun deportation proceedings for 56 Cuban migrants who entered the Central American nation after it stopped issuing transit visas on Dec. 18. These 56 people are, however, not among those stranded in Costa Rica.

On Sunday, Pope Francis urged Central American governments to show generosity in dealing with the crisis.

"I invite the countries of the region to renew with generosity all necessary efforts in order to find a rapid solution to this humanitarian drama," the pope told tens of thousands of people at St. Peter's Square in Vatican City, according to BBC, adding that many of the migrants were victims of human trafficking.