Reference issue

By default, we are using automatic reference in our payments. the last payment reference was 986.

We created a new payment with a reference 987 (let’s call it payment 1)
By mistake one of our users edited the reference from 987 to 8654 to the payment 1

Then we created a new payment with automatic reference but the reference came 8655. (let’s call it payment 2)

I fixed the refenence for payment 1 (remake it 987 ) and payment 2 I edited ti 988

but when we are creating a new payment with an automatic reference , the new reference sequense continue from 8655 and not from 987
1- Can you help me in fixing this ?
2- During this process, I figured that we can have same reference number without any warning in the system

Search for the reference 8654 or just enter 865 in search. There must be a four digit reference.

The issue os when creating a new payment the reference will followu the big number

yes and therefore you need to correct as @shahabb explains search for the wrong number and correct it to what is was supposed to be. You can also use History and remove all entries later and including the wrong one and redo these.