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

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

j = A("j")
a = B("a")
k = B("k")
m = A("m")
l = B("l")
c = B("c")
e = B("e")
f = B("f")
h = A("h")
g = A("g")

j.r = f
a.s = e
k.s = f
m.r = f
l.s = f
c.s = k
e.s = k
f.s = e
h.r = f
g.r = c
assert k.s.s.s.s.s.s.s.txt == "
class A:
    def __init__(self, txt: str):
        self.r: B = None
        self.txt = txt

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

j = A("j")
a = B("a")
k = B("k")
m = A("m")
l = B("l")
c = B("c")
e = B("e")
f = B("f")
h = A("h")
g = A("g")

j.r = f
a.s = e
k.s = f
m.r = f
l.s = f
c.s = k
e.s = k
f.s = e
h.r = f
g.r = c
assert k.s.s.s.s.s.s.s.txt == "f"