main :-
	read_num(N),
	read_n_num_list(N, Jumps),
        findall(Cost, jump(Jumps, 0, Cost), CostList),
	min_list(CostList, MinCost),
	write(MinCost),
	nl,
	halt.

jump([_], Sum, Sum).
jump([H1, H2|R], Acc, AllCost) :-
        Cost is abs(H1 - H2),
        NewAcc is Cost + Acc,
        jump([H2|R], NewAcc, AllCost).
jump([H1, _H2, H3|R], Acc, AllCost) :-
        Cost is abs(H1 - H3),
        NewAcc is Cost + Acc,
        jump([H3|R], NewAcc, AllCost).


read_n_num_list(Num, List) :-
	read_n_num_list_aux(Num, [], List),
	!.
read_n_num_list_aux(0, List, List).
read_n_num_list_aux(Num, Old, Accum) :-
	read_num(NewItem),
	NewNum is Num - 1,
	append(Old, [NewItem], NewList),
	read_n_num_list_aux(NewNum, NewList, Accum).

read_str(S) :-
        read_string(current_input, ' \n', '', _, S).

read_num(N) :-
	read_str(S),
	number_string(N, S).

:- main.
