first common terminal search algorithm