If you accept helper columns as per your attachment.
Then, try this,
1] Helper column_1, H3 formula copied down:
=IF(COUNTIFS(B$3:B3,B3,C$3:C3,C3)=1,IF(COUNTIFS(C$3:C3,C3)=1,MAX(H$2:H2)+1,0),"")
2] Helper column_2, I3 formula copied down:
=IF(H3=0,MAX(I$2:I2)+1,0)
3] Formula result, J3...