Lets say miner A is working on block X, while miner B works on block Y.
Both miner A and B find the hash for their blocks almost at the same time, and as such, they start broadcasting their results to other miners, so a consensus can be reached.
Are all the miners on the network needed to reach such consensus?
If such, what happens if a group of miners is working to reach a consensus on a block, and some other group is working on some other block (given that the broadcast to every node takes a little bit of time)?
How do they decide what block gets added first to the blockchain?
Note that I'm pretty new to this, and still trying to learn how the blockchain works.