My first thought would be a Post-Adventure script that calls run_combat() to get the html of the fight page, increments the counter if it sees you lost the fight, and resets it if you won.
Something likeif (run_combat().contains_text("Enter the Underworld")) <increment your counter> else <reset...