By Categorize do you mean that you want the groups to restart at 1 when then Link changes? i.e. A would have group 1 and B would have Group 1 and Group 2.
If the outcome that you've posted, A has group 1 and B has Group 2 and Group 3, is what you're looking for, @JohnJPS's formula still works.
If @JohnJPS's solution is correct, then consider marking the solution as accepted.