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

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

g = A("g")
j = B("j")
e = B("e")
a = A("a")
f = A("f")
k = B("k")
c = A("c")
b = A("b")
d = A("d")
m = A("m")

g.p = j
j.y = f
e.y = d
a.p = e
f.p = j
k.y = b
c.p = k
b.p = j
d.p = j
m.p = e
assert d.p.y.p.txt == "
class A:
    def __init__(self, txt: str):
        self.p: B = None
        self.txt = txt

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

g = A("g")
j = B("j")
e = B("e")
a = A("a")
f = A("f")
k = B("k")
c = A("c")
b = A("b")
d = A("d")
m = A("m")

g.p = j
j.y = f
e.y = d
a.p = e
f.p = j
k.y = b
c.p = k
b.p = j
d.p = j
m.p = e
assert d.p.y.p.txt == "j"