CVS Health Corp. (NYSE: CVS), the biggest U.S. retailer of prescription drugs, agreed to acquire nursing-home pharmacy Omnicare Inc. for a total enterprise value of $12.7 billion.

CVS will pay $98 per share in cash, the companies said in a statement on Thursday. Omnicare, with a market value of about $9.2 billion, hired advisers to explore a sale earlier this year. The deal includes about $2.3 billion in debt.

Omnicare delivers drugs and helps senior-living facilities manage residents’ medications. CVS is the nation’s second-largest pharmacy benefits manager, handling drug plans for health insurers and employers. In its most recent quarter growth in sales from that business far outpaced the company’s retail division.

Both CVS and Omnicare are big in Medicare Part D, a federal program that subsidizes medicine for retirees, Charles Rhyee, a New York-based analyst for Cowen Group Inc., said in an interview last month. Omnicare also drew interest from industry leader Express Scripts Holding Co., people with knowledge of the matter said last month.

Both CVS and Omnicare are big in Medicare Part D, a federal program that subsidizes medicine for retirees, Charles Rhyee, a New York-based analyst for Cowen Group Inc., said in an interview last month. Omnicare also drew interest from industry leader Express Scripts Holding Co., people with knowledge of the matter said last month.