Algorithm for tokens movement