put([],L,L):-!.
put(S,L,Ans):- select(S,A,B), safe(A,A,L), put(B,[A|L],Ans).

select([A|B],A,B).
select([A|B],S,[A|L]):- select(B,S,L).

safe(_,_,[]):-!.
safe(A,B,[C|D]):- AA is A-1,AA \==C, BB is B+1,BB \==C, safe(AA,BB,D).

qtest(List,First,Vol):- bagof(1,L^put(List,First,L),Ans),length(Ans,Vol).
