tourists([petros,dimitris,kostas,maria]).

walks(petros,6).
walks(dimitris,7).
walks(kostas,10).
walks(maria,15).

cost([],0) :- !.
cost([X|L],C) :-
	walks(X,S),
	cost(L,D),
	C is max(S,D).

split(L,[X,Y],M) :-
	member(X,L),
	member(Y,L),
	compare(<,X,Y),
	subtract(L,[X,Y],M).

move(st(l,L1),st(r,L2),r(M),D) :-
	split(L1,M,L2),
	cost(M,D).
move(st(r,L1),st(l,L2),l(X),D) :-
	tourists(T),
	subtract(T,L1,R),
	member(X,R),
	merge_set([X],L1,L2),
	walks(X,D).

trans(st(r,[]),st(r,[]),[],0).
trans(S,U,L,D) :-
	move(S,T,M,X),
	trans(T,U,N,Y),
	append([M],N,L),
	D is X + Y.

cross(M,D) :-
	tourists(T),
	trans(st(l,T),st(r,[]),M,D0),
	D0=<D.