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

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

h = A("h")
g = B("g")
a = A("a")
b = A("b")
d = A("d")
l = A("l")
i = B("i")
m = A("m")
c = A("c")
f = B("f")

h.x = g
g.r = i
g.x = m
a.x = g
b.x = f
d.x = f
l.x = i
i.r = f
i.x = a
m.x = g
c.x = f
f.r = i
f.x = h
assert g.x.x.r.r.x.x.x.x.txt == "
class A:
    def __init__(self, txt: str):
        self.x: B = None
        self.txt = txt

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

h = A("h")
g = B("g")
a = A("a")
b = A("b")
d = A("d")
l = A("l")
i = B("i")
m = A("m")
c = A("c")
f = B("f")

h.x = g
g.r = i
g.x = m
a.x = g
b.x = f
d.x = f
l.x = i
i.r = f
i.x = a
m.x = g
c.x = f
f.r = i
f.x = h
assert g.x.x.r.r.x.x.x.x.txt == "g"