I haven't solved it yet myself, but found the following testcase helpful:
which should yield 44 and
which should yield 40 and
which should yield 124 (I think).
Any additional testcase from your side would be appreciated (especially from the one who posted )