How to stop animation from going past the last frame?

I’m playing an animation in the animator with this script, changing the speed from 1 to -1 through a variable.

        if (Input.GetButtonDown("Forward") {
          animator.Play("myAnimation", -1);
          animator.SetFloat("Speed", 1);
        }
    
     if (Input.GetButtonDown("Backward") {
          animator.Play("myAnimation", -1);
          animator.SetFloat("Speed", -1);
        }

Which works fine. Problem is that it when it reaches the start or ending, it seems to continue in time. I.e. if I hold the forward button 1 second after reaching the end, and then start pressing the backward button it will take one second before the animation reaches the “end”-frame and actually starts playing the animation backwards. How can I make it stop at the last / first frame?

Thanks

Still No reply.???
And no way to get it working.