"""Count eight active ticks by cyclic composition of allowed residue triples."""
from collections import defaultdict
from functools import lru_cache
from itertools import combinations,product
import json

@lru_cache(None)
def compatible(start,a,b,c,k):
    visits=sorted((6*turn+(start+side)%6,side) for side,turns in enumerate((a,b,c)) for turn in turns)
    assert len(visits)==4
    boundary=changes=0
    for (last,old),(now,new) in zip(visits,visits[1:]):
        if old!=new:
            changes+=1;boundary=max(boundary+2,last+1)
            if changes>k or boundary>now:return False
    return boundary<=22

def count(sizes,k):
    options=[list(combinations(range(4),n)) for n in sizes]
    total=0
    for first,second in product(options[0],options[1]):
        states={(first,second):1}
        for i,choices in enumerate(options[2:]+[[first],[second]]):
            nxt=defaultdict(int)
            for (a,b),ways in states.items():
                for c in choices:
                    if compatible(i,a,b,c,k):nxt[b,c]+=ways
            states=nxt
        total+=sum(states.values())
    return total
def study():
    rows=[]
    for a in range(5):
        for b in range(5-a):
            sizes=(a,b,4-a-b)*2
            two,three=[count(sizes,k) for k in (2,3)]
            rows.append(dict(counts=sizes,two=two,three=three,sensitive=three-two))
    assert sum(z['sensitive'] for z in rows)==14986
    return rows
if __name__=='__main__':print(json.dumps(study()))
