Walgreens Boots Alliance Inc. WBA, +1.14% said Thursday that it has terminated its merger agreement with Rite Aid Corp. RAD, -8.92% and has instead agreed to buy 2,186 Rite Aid stores and related assets. The consideration for the deal will be $5.175 billion in cash and the option for Rite Aid to become a member of the Walgreens group purchasing organization, an option that can be exercised through May 2019 and is subject to certain conditions. This new agreement replaces the merger agreement announced October 2015 and amended January 2017, along with the agreement to divest some Rite Aid stores to Fred's Inc. fred, which was announced in December 2016. The agreement with Fred's has also been terminated and Walgreens will pay a $325 million termination fee. After the new transaction is approved, Walgreens will acquire the Rite Aid stores and assets over about six months and will eventually convert them to the Walgreens brand. Walgreens expects the transaction to be "modestly accretive" to adjusted earnings per share in the first full year after the initial closing, and expects synergies totaling $400 million that will be fully realized within three to four years. Walgreens shares are up 1.7% in premarket trading, Rite Aid shares are down 6.1%, and Fred's shares are down 27%. Walgreens shares are down nearly 7% for the year so far while Rite Aid shares are down more than 52% and Fred's are down 33.6% for the period. The S&P 500 index SPX, +1.59% is up 9% for 2017 to date.