BERLIN (Reuters) - Germany’s federal government agreed on Wednesday to distribute 6.85 billion euros ($7.87 billion) to its regions next year to help them cover the costs of housing and integrating migrants, a government source told Reuters.

Germany is trying to integrate over a million migrants and refugees who entered in 2015-2016 after Chancellor Angela Merkel opened its borders, an unpopular decision that catapulted the anti-immigration Alternative for Germany (AfD) into the lower house of parliament after elections last year.

Merkel’s government acted ahead of Sunday’s election in the affluent state of Bavaria, where a backlash over migration is threatening the longtime hold on power of the conservative Christian Social Union (CSU), Merkel’s federal coalition ally.

Legislation drafted by Merkel’s Finance Ministry for migrant aid to federal states in 2019 includes 2.44 billion euros for integration including German-language classes, 1.8 billion euros for accommodation benefits and 500 million for social housing subsidies, the source said.

It provides for a total 15 billion euros to be paid out to states and communes for assisting migrants through 2022.

Germany’s states also received one-off federal cash infusions to cover migration-related costs during the height of the influx by mainly Muslim migrants in 2015 and 2016.