How does css take the last specified element?

there are many li,li arranged in 3 rows. Now you need to control the style of the last li (li:last-child). If the last li is 1-3, read style a, and if it is 4-6, read style b. Cycle on 7: 9, read a, 5, 10, 12, read b. How to get pure css?

Css
Jan.12,2022

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title></title>
    <style>
        li:last-child:nth-of-type(6n+1),li:last-child:nth-of-type(6n+2),li:last-child:nth-of-type(6n+3){
            color: red;
        }
        li:last-child:nth-of-type(6n+4),li:last-child:nth-of-type(6n+5),li:last-child:nth-of-type(6n){
            color: yellow;
        }
    </style>
</head>
<body>
<ul>

</ul>
<script>
    var ul=document.querySelector('ul');
    var index=1;
    setInterval(function(){
        ul.insertAdjacentHTML('beforeend',`<li>${indexPP}</li>`)
    },500);
</script>
</body>
</html>

try this

Menu