Red, 55 49 49 45 4539 bytes
func[n][x: 2 if n[looploop n[x: x * x - x + 1]]x]1]x] - -6 thanks to Galen Ivanov
- -4 thanks to Pedro Maimere
- -6 thanks to 9214
This is the first real thing I wrote in Red. You have been warned.