Parent-Child Hierarchy w/ multiple parents

Dear community,

there are several solutions for parent/child hierarchy topics if a child has only one possible parent.

I have a dataset w/ two columns “parent” and “child”.

A “child” can have several “parents” and also several more “children”.

Is there a solution to get the top “parents” and all “children” w/ the respective level?

example data

parent | child

A | B

B | C

B | D

B | E

Z | B

B | C

B | D

B | E

Y | C

Y | D

Y | E

needed result

Top parent | child | level

A | B | 1

A | C | 2

A | D | 2

A | E | 2

Z | B | 1

Z | C | 2

Z | D | 2

Z | E | 2

Y | C | 1

Y | D | 1

Y | E | 1