A tie-up of big pharmacy chains came with a healthy price tag this week, requiring a prescription-strength legal team to push through the $17.2 billion union, including debt, between Ride Aid Corp. and the Walgreens Boots Alliance Inc.

Deerfield, Illinois-based Walgreens announced Tuesday that it had agreed to acquire Camp Hill, Pennsylvania-based Rite Aid in a deal expected to close in the second half of next year. The transaction, which includes $9.4 billion in cash, combines the No. 1 and No. 3 pharmacy chains in the U.S., when measured by number of retail drugstores.