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

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

h = A("h")
g = B("g")
c = B("c")
d = B("d")
j = A("j")
m = A("m")
a = B("a")

h.r = j
h.z = j
g.w = h
g.v = d
c.w = h
c.v = a
d.w = j
d.v = g
j.r = h
j.z = m
m.r = h
m.z = h
a.w = m
a.v = d
assert h.z.z.z.z.z.txt == "
class A:
    def __init__(self, txt: str):
        self.r: A = None
        self.z: A = None
        self.txt = txt

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

h = A("h")
g = B("g")
c = B("c")
d = B("d")
j = A("j")
m = A("m")
a = B("a")

h.r = j
h.z = j
g.w = h
g.v = d
c.w = h
c.v = a
d.w = j
d.v = g
j.r = h
j.z = m
m.r = h
m.z = h
a.w = m
a.v = d
assert h.z.z.z.z.z.txt == "m"