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

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

g = A("g")
f = B("f")
a = A("a")
n = B("n")
i = A("i")
d = A("d")
c = A("c")
j = B("j")
h = B("h")

g.r = i
f.p = h
a.r = d
n.p = h
i.r = a
d.r = g
c.r = d
j.p = h
h.p = f
assert i.r.r.r.r.txt == "
class A:
    def __init__(self, txt: str):
        self.r: A = None
        self.txt = txt

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

g = A("g")
f = B("f")
a = A("a")
n = B("n")
i = A("i")
d = A("d")
c = A("c")
j = B("j")
h = B("h")

g.r = i
f.p = h
a.r = d
n.p = h
i.r = a
d.r = g
c.r = d
j.p = h
h.p = f
assert i.r.r.r.r.txt == "i"