You have to be truly sorry for your sins and make an honest attempt at not doing them again. That being said if you sneak in a really sincere confession right before death, then by the book, you should go to heaven. This is a loooot like Christians last rights, the sacraments they use on the dying.
Fun fact anyone can preform last rights for a Christian should they request it. Reason being that people don’t always get to choose when they need their last rights so holy men may or may not be around.