Consider, To bark up the wrong tree
Fig. to make the wrong choice; to ask the wrong person; to follow the wrong course. (Alludes to a dog in pursuit of an animal, where the animal is in one tree and the dog is barking at another tree.)
[The Free Dictionary]
In your scenario, A is barking up the wrong tree. Without knowing B themselves have failed multiple subjects, A is trying to share their sorrow to B, who is the wrong person.
Update as per OP's edit
In that case, Person B shares Person A's pain.
To share someone's pain
to understand and sympathize with someone's pain or emotional discomfort. (Said in order to sound sympathetic.)
You can use this with Peter Point's excellent suggestion - "to be in the same boat".
A: I have failed multiple subjects. I feel severely dejected.
B: It's alright. I share your pain. We happen to be in the same boat!