I think the correct answer in terms of mental health is: as little time as possible.
The actual answer is more complicated. Someone who got locked up for 25 years knowingly committing a significant crime might regret it for the majority of their sentence, at least.
I don't think this is true at all. Living your life ignoring all of your past mistakes can be just as unhealthy as dwelling on them needlessly. Dwelling on past mistakes can lead to hopelessness and depression, but ignoring them entirely can lead to narcissism and repeating damaging patterns.