How to calculate buy back token amount on quipuswap v2?

I have a token-tez pool on quipuswap v2 dex.

I want to calculate how much tez should I provide to dex to buy back the x amount of tokens from the dex.

I want to perform this action in a smartpy contract.

What is the formula to calculate this?

How can I do that?