python solution (faster 90%)
\tclass Solution:\n\t\tdef earliestAndLatest(self, n, F, S):\n\t\t\tans = set()\n\t\t\tdef dfs(pos, i):\n\t\t\t\tM, pairs = len(pos), []\n\t\t\t\tif M < 2: retu
\tclass Solution:\n\t\tdef earliestAndLatest(self, n, F, S):\n\t\t\tans = set()\n\t\t\tdef dfs(pos, i):\n\t\t\t\tM, pairs = len(pos), []\n\t\t\t\tif M < 2: return\n\n\t\t\t\tfor j in range(M//2):\n\t\t\t\t\ta, b = pos[j], pos[-1-j]\n\t\t\t\t\tif (a, b) == (F, S):\n\t\t\t\t\t\tans.add(i)\n\t\t\t\t\t\treturn\n\t\t\t\t\tif a != F and b != F and a != S and b != S:\n\t\t\t\t\t\tpairs.append((a, b))\n\n\t\t\t\taddon = (F, S) if M%2 == 0 else tuple(set([F, S, pos[M//2]]))\n\t\t\t\tfor elem in product(*pairs):\n\t\t\t\t\tdfs(sorted(elem + addon), i + 1)\n\n\t\t\tdfs(list(range(1, n+1)), 1)\n\t\t\treturn [min(ans), max(ans)]
[]