Solved first problem in prolog