class A:
    def __init__(self, txt: str):
        self.r: A = None
        self.z: B = None
        self.txt = txt

class B:
    def __init__(self, txt: str):
        self.z: A = None
        self.txt = txt

b = A("b")
m = B("m")
n = B("n")
d = A("d")
h = B("h")
j = A("j")

b.r = d
b.z = m
m.z = j
n.z = j
d.r = b
d.z = n
h.z = b
j.r = d
j.z = h
assert h.z.z.z.z.z.z.txt == "
class A:
    def __init__(self, txt: str):
        self.r: A = None
        self.z: B = None
        self.txt = txt

class B:
    def __init__(self, txt: str):
        self.z: A = None
        self.txt = txt

b = A("b")
m = B("m")
n = B("n")
d = A("d")
h = B("h")
j = A("j")

b.r = d
b.z = m
m.z = j
n.z = j
d.r = b
d.z = n
h.z = b
j.r = d
j.z = h
assert h.z.z.z.z.z.z.txt == "m"