def _f(_t): return (_t[0]**2+_t[1])%_t[2] def _repeat(_func, _m, _t): for _i in range(_m): _t = (_func(_t), _t[1], _t[2]) return _t[0] def _gcd(_a, _b): if _a < _b: return _gcd(_b, _a) return _a if _b == 0 else _gcd(_b, _a%_b) def _dif(_a, _b): return _a-_b if _a > _b else _b-_a S = 894488611079900130 A = 1 N = (2**31-1)**2 print(_repeat(_f, 27081, (2, A, N)) == S) T = _repeat(_f, 27081, (S, A, N)) print(_gcd(_dif(T, S), N))