Backtracking solution