print([]):-!.
print([H|T]):-H=1,write('>'),print(T).
print([H|T]):-H= -1,write('<'),print(T).

printp([]):-!.
printp([H|T]):-H=1,printp(T),write('>').
printp([H|T]):-H= -1,printp(T),write('<').

frg(M,W):-not(member(1,M)),not(member(-1,W)),printp(M),write('-'),print(W),!.
frg([1|M],W):-not(stop(M,[1|W])),printp([1|M]),write('_'),print(W),writeln(' '),frg(M,[1|W]).
frg(M,[-1|W]):-not(stop([-1|M],W)),printp(M),write('_'),print([-1|W]),writeln(' '),frg([-1|M],W).
frg([X,1|M],W):-not(stop(M,[X,1|W])),printp([X,1|M]),write('_'),print(W),writeln(' '),frg(M,[X,1|W]).
frg(M,[X,-1|W]):-not(stop([X,-1|M],W)),printp(M),write('_'),print([X,-1|W]),writeln(' '),frg([X,-1|M],W).

stop([-1,1|_],[1,-1|_]).
stop([-1,-1|T],_):-member(1,T).
stop(_,[1,1|T]):-member(-1,T).

womenFrg([],0):-!.
womenFrg([-1|W],C):-Ci is C-1,womenFrg(W,Ci).
menFrg([],0):-!.
menFrg([1|M],C):-Ci is C-1,menFrg(M,Ci).

frog(M,W):-womenFrg(Wl,W),menFrg(Ml,M),frg(Ml,Wl).
