Pairing socks problem

Question

Hints

Answer